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: Eliminate redundancy in replace_source_word methods

LearningCircuit3e704cdc777a
diff --git a/src/local_deep_research/text_optimization/citation_formatter.py b/src/local_deep_research/text_optimization/citation_formatter.py
index eb16d7b75a..162a93e122 100644
--- a/src/local_deep_research/text_optimization/citation_formatter.py
+++ b/src/local_deep_research/text_optimization/citation_formatter.py
@@ -35,6 +35,22 @@ class CitationFormatter:
re.MULTILINE,
)
+ def _create_source_word_replacer(self, formatter_func):
+ """Create a replacement function for 'Source X' patterns.
+
+ Args:
+ formatter_func: A function that takes citation_num and returns formatted text
+
+ Returns:
+ A replacement function for use with regex sub
+ """
+
+ def replace_source_word(match):
+ citation_num = match.group(1)
+ return formatter_func(citation_num)
+
+ return replace_source_word
+
def format_document(self, content: str) -> str:
"""
Format citations in the document according to the selected mode.
@@ -161,14 +177,15 @@ class CitationFormatter:
content = self.citation_pattern.sub(replace_citation, content)
# Also handle "Source X" patterns
- def replace_source_word(match):
- citation_num = match.group(1)
+ def source_formatter(citation_num):
if citation_num in sources and sources[citation_num][1]:
url = sources[citation_num][1]
return f"[[{citation_num}]]({url})"
return f"[{citation_num}]"
- return self.source_word_pattern.sub(replace_source_word, content)
+ return self.source_word_pattern.sub(
+ self._create_source_word_replacer(source_formatter), content
+ )
def _format_domain_hyperlinks(
self, content: str, sources: Dict[str, Tuple[str, str]]
@@ -208,15 +225,16 @@ class CitationFormatter:
content = self.citation_pattern.sub(replace_citation, content)
# Also handle "Source X" patterns
- def replace_source_word(match):
- citation_num = match.group(1)
+ def source_formatter(citation_num):
if citation_num in sources and sources[citation_num][1]:
url = sources[citation_num][1]
domain = self._extract_domain(url)
return f"[[{domain}]]({url})"
return f"[{citation_num}]"
- return self.source_word_pattern.sub(replace_source_word, content)
+ return self.source_word_pattern.sub(
+ self._create_source_word_replacer(source_formatter), content
+ )
def _format_domain_id_hyperlinks(
self, content: str, sources: Dict[str, Tuple[str, str]]
@@ -277,14 +295,15 @@ class CitationFormatter:
content = self.citation_pattern.sub(replace_citation, content)
# Also handle "Source X" patterns
- def replace_source_word(match):
- citation_num = match.group(1)
+ def source_formatter(citation_num):
if citation_num in citation_to_domain_id:
domain_id, url = citation_to_domain_id[citation_num]
return f"[[{domain_id}]]({url})"
return f"[{citation_num}]"
- return self.source_word_pattern.sub(replace_source_word, content)
+ return self.source_word_pattern.sub(
+ self._create_source_word_replacer(source_formatter), content
+ )
def _format_domain_id_always_hyperlinks(
self, content: str, sources: Dict[str, Tuple[str, str]]
@@ -337,14 +356,15 @@ class CitationFormatter:
content = self.citation_pattern.sub(replace_citation, content)
# Also handle "Source X" patterns
- def replace_source_word(match):
- citation_num = match.group(1)
+ def source_formatter(citation_num):
if citation_num in citation_to_domain_id:
domain_id, url = citation_to_domain_id[citation_num]
return f"[[{domain_id}]]({url})"
return f"[{citation_num}]"
- return self.source_word_pattern.sub(replace_source_word, content)
+ return self.source_word_pattern.sub(
+ self._create_source_word_replacer(source_formatter), content
+ )
def _to_superscript(self, text: str) -> str:
"""Convert text to Unicode superscript."""