Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

refactor: remove unused topic clustering implementation

LearningCircuit47c7c1fb62a6
diff --git a/src/local_deep_research/advanced_search_system/strategies/topic_clustering.py b/src/local_deep_research/advanced_search_system/strategies/topic_clustering.py
deleted file mode 100644
index 5c43a79d0d..0000000000
--- a/src/local_deep_research/advanced_search_system/strategies/topic_clustering.py
+++ /dev/null
@@ -1,341 +0,0 @@
-import re
-from typing import List, Dict, Tuple, Any
-from collections import Counter
-
-
-class TopicClusterer:
- """
- Handles clustering of sources into topics using lightweight methods
- suitable for local models.
- """
-
- def __init__(self, similarity_threshold: float = 0.3):
- self.similarity_threshold = similarity_threshold
-
- def extract_keywords(self, text: str, top_n: int = 10) -> List[str]:
- """
- Extract keywords from text using simple frequency analysis.
- """
- # Simple tokenization and normalization
- text = text.lower()
- # Remove URLs
- text = re.sub(
- r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+",
- "",
- text,
- )
- # Keep only alphanumeric and spaces
- text = re.sub(r"[^a-zA-Z0-9\s]", " ", text)
-
- # Split into words
- words = text.split()
-
- # Filter out common stop words (minimal set for efficiency)
- stop_words = {
- "the",
- "be",
- "to",
- "of",
- "and",
- "a",
- "in",
- "that",
- "have",
- "i",
- "it",
- "for",
- "not",
- "on",
- "with",
- "he",
- "as",
- "you",
- "do",
- "at",
- "this",
- "but",
- "his",
- "by",
- "from",
- "they",
- "we",
- "say",
- "her",
- "she",
- "or",
- "an",
- "will",
- "my",
- "one",
- "all",
- "would",
- "there",
- "their",
- "what",
- "so",
- "up",
- "out",
- "if",
- "about",
- "who",
- "get",
- "which",
- "go",
- "me",
- "when",
- "make",
- "can",
- "like",
- "time",
- "no",
- "just",
- "him",
- "know",
- "take",
- "people",
- "into",
- "year",
- "your",
- "good",
- "some",
- "could",
- "them",
- "see",
- "other",
- "than",
- "then",
- "now",
- "look",
- "only",
- "come",
- "its",
- "over",
- "think",
- "also",
- "back",
- "after",
- "use",
- "two",
- "how",
- "our",
- "work",
- "first",
- "well",
- "way",
- "even",
- "new",
- "want",
- "because",
- "any",
- "these",
- "give",
- "day",
- "most",
- "us",
- "is",
- "are",
- "was",
- "were",
- "been",
- "has",
- "had",
- "may",
- "might",
- "must",
- "shall",
- "should",
- "could",
- "would",
- "am",
- "does",
- "did",
- "doing",
- }
-
- # Filter words
- filtered_words = [
- word for word in words if len(word) > 2 and word not in stop_words
- ]
-
- # Count frequencies
- word_freq = Counter(filtered_words)
-
- # Get top keywords
- return [word for word, _ in word_freq.most_common(top_n)]
-
- def calculate_similarity(
- self, source1: Dict[str, Any], source2: Dict[str, Any]
- ) -> float:
- """
- Calculate similarity between two sources based on title and snippet.
- """
- # Extract text from sources
- text1 = f"{source1.get('title', '')} {source1.get('snippet', '')}"
- text2 = f"{source2.get('title', '')} {source2.get('snippet', '')}"
-
- # Extract keywords
- keywords1 = set(self.extract_keywords(text1, 20))
- keywords2 = set(self.extract_keywords(text2, 20))
-
- if not keywords1 or not keywords2:
- return 0.0
-
- # Jaccard similarity
- intersection = len(keywords1 & keywords2)
- union = len(keywords1 | keywords2)
-
- if union == 0:
- return 0.0
-
- return intersection / union
-
- def cluster_sources(
- self, sources: List[Dict[str, Any]], max_clusters: int = 5
- ) -> List[List[int]]:
- """
- Cluster sources into groups based on similarity.
- Returns a list of clusters, where each cluster is a list of source indices.
- """
- if not sources:
- return []
-
- n = len(sources)
- if n == 1:
- return [[0]]
-
- # Build similarity matrix
- similarities = {}
- for i in range(n):
- for j in range(i + 1, n):
- sim = self.calculate_similarity(sources[i], sources[j])
- if sim >= self.similarity_threshold:
- similarities[(i, j)] = sim
-
- # Greedy clustering
- clusters = []
- unclustered = set(range(n))
-
- while unclustered and len(clusters) < max_clusters:
- # Start a new cluster with an unclustered source
- seed = unclustered.pop()
- cluster = [seed]
- cluster_members = {seed}
-
- # Find similar sources
- changed = True
- while changed:
- changed = False
- for idx in list(unclustered):
- # Check similarity with cluster members
- max_sim = 0.0
- for member in cluster_members:
- pair = (min(idx, member), max(idx, member))
- if pair in similarities:
- max_sim = max(max_sim, similarities[pair])
-
- if max_sim >= self.similarity_threshold:
- cluster.append(idx)
- cluster_members.add(idx)
- unclustered.remove(idx)
- changed = True
-
- clusters.append(cluster)
-
- # Add remaining unclustered sources as single-item clusters
- for idx in unclustered:
- clusters.append([idx])
-
- return clusters
-
- def select_lead_source(
- self, sources: List[Dict[str, Any]], indices: List[int]
- ) -> int:
- """
- Select the best source to be the lead for a cluster.
- Returns the index of the lead source.
- """
- if not indices:
- return -1
-
- if len(indices) == 1:
- return indices[0]
-
- # Score each source based on:
- # 1. Length of snippet (more informative)
- # 2. Title quality (not too short, not too long)
- # 3. Centrality (similarity to other sources in cluster)
-
- best_score = -1
- best_idx = indices[0]
-
- for idx in indices:
- source = sources[idx]
- score = 0.0
-
- # Snippet length score
- snippet_len = len(source.get("snippet", ""))
- score += min(snippet_len / 500, 1.0) # Normalize to [0, 1]
-
- # Title quality score
- title = source.get("title", "")
- title_len = len(title)
- if 20 <= title_len <= 100:
- score += 1.0
- elif title_len < 20:
- score += title_len / 20
- else:
- score += max(0, 1 - (title_len - 100) / 100)
-
- # Centrality score
- total_similarity = 0.0
- for other_idx in indices:
- if other_idx != idx:
- sim = self.calculate_similarity(
- sources[idx], sources[other_idx]
- )
- total_similarity += sim
-
- if len(indices) > 1:
- avg_similarity = total_similarity / (len(indices) - 1)
- score += avg_similarity
-
- if score > best_score:
- best_score = score
- best_idx = idx
-
- return best_idx
-
- def generate_cluster_title(
- self, sources: List[Dict[str, Any]], indices: List[int]
- ) -> Tuple[str, List[str]]:
- """
- Generate a title and keywords for a cluster of sources.
- Returns (title, keywords).
- """
- if not indices:
- return ("Unknown Topic", [])
-
- # Combine text from all sources in cluster
- combined_text = ""
- titles = []
- for idx in indices:
- source = sources[idx]
- title = source.get("title", "")
- snippet = source.get("snippet", "")
- combined_text += f" {title} {snippet}"
- if title:
- titles.append(title)
-
- # Extract keywords from combined text
- keywords = self.extract_keywords(combined_text, 15)
-
- # Generate title from most common keywords
- if keywords:
- # Use top 3-5 keywords for title
- title_words = keywords[: min(5, len(keywords))]
- title = " ".join([word.capitalize() for word in title_words[:3]])
- else:
- # Fallback to shortest title
- title = min(titles, key=len) if titles else "Unknown Topic"
-
- return (title, keywords[:10]) # Return top 10 keywords