text
stringlengths 1
93.6k
|
|---|
# "requests",
|
# "beautifulsoup4",
|
# "html2text",
|
# "networkx",
|
# "pyvis",
|
# ]
|
# ///
|
"""
|
Search Graph Generator Script
|
This script takes a question as input and generates a knowledge graph by:
|
1. Understanding the question and setting up assumptions
|
2. Generating search queries
|
3. Running searches and processing results
|
4. Building and displaying a graph structure
|
Usage:
|
./search_graph.py -h
|
./search_graph.py -q "What is machine learning?" -v # For INFO logging
|
./search_graph.py -q "What is machine learning?" -vv # For DEBUG logging
|
"""
|
import logging
|
import time
|
from argparse import ArgumentParser, RawDescriptionHelpFormatter
|
from concurrent.futures import ThreadPoolExecutor
|
from threading import Lock
|
from urllib.parse import quote_plus
|
import networkx as nx
|
import requests
|
from bs4 import BeautifulSoup
|
from html2text import HTML2Text
|
from pyvis.network import Network
|
class SearchGraph:
|
def __init__(self, question):
|
self.question = question
|
self.graph = nx.Graph(title=question)
|
self.search_queries = []
|
self.graph_lock = Lock()
|
def generate_search_queries(self):
|
"""Generate 10 search queries based on the input question"""
|
logging.debug(f"Generating search queries for question: {self.question}")
|
base_queries = [
|
self.question,
|
f"how to {self.question}",
|
f"what is {self.question}",
|
f"explain {self.question}",
|
f"{self.question} tutorial",
|
f"{self.question} guide",
|
f"{self.question} examples",
|
f"{self.question} best practices",
|
f"{self.question} overview",
|
f"{self.question} detailed explanation",
|
]
|
self.search_queries = base_queries
|
return base_queries
|
def visualize_graph(self, output_file="search_graph.html"):
|
"""
|
Create an interactive visualization of the graph
|
"""
|
logging.info(f"Generating interactive visualization: {output_file}")
|
net = Network(
|
height="750px",
|
width="100%",
|
bgcolor="#ffffff",
|
font_color="#000000",
|
notebook=False,
|
)
|
net.force_atlas_2based(
|
gravity=-50,
|
central_gravity=0.01,
|
spring_length=100,
|
spring_strength=0.08,
|
damping=0.4,
|
overlap=0,
|
)
|
color_map = {"question": "#ff7675", "query": "#74b9ff", "result": "#55efc4"}
|
net.add_node(
|
self.question,
|
label=self.question[:30] + "..."
|
if len(self.question) > 30
|
else self.question,
|
color=color_map["question"],
|
size=20,
|
title=self.question,
|
)
|
for node, data in self.graph.nodes(data=True):
|
if node == self.question:
|
continue
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.