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.

fix: extend citation regex to support Unicode lenticular brackets【】 (#1834)

LearningCircuitb19cd87eb68e
diff --git a/src/local_deep_research/text_optimization/citation_formatter.py b/src/local_deep_research/text_optimization/citation_formatter.py
index e08f14ffb0..c6eeb50dc3 100644
--- a/src/local_deep_research/text_optimization/citation_formatter.py
+++ b/src/local_deep_research/text_optimization/citation_formatter.py
@@ -26,8 +26,13 @@ class CitationFormatter:
def __init__(self, mode: CitationMode = CitationMode.NUMBER_HYPERLINKS):
self.mode = mode
# Use negative lookbehind and lookahead to avoid matching already formatted citations
- self.citation_pattern = re.compile(r"(?<!\[)\[(\d+)\](?!\])")
- self.comma_citation_pattern = re.compile(r"\[(\d+(?:,\s*\d+)+)\]")
+ # Also match Unicode lenticular brackets 【】 (U+3010 and U+3011) that LLMs sometimes generate
+ self.citation_pattern = re.compile(
+ r"(?<![\[【])[\[【](\d+)[\]】](?![\]】])"
+ )
+ self.comma_citation_pattern = re.compile(
+ r"[\[【](\d+(?:,\s*\d+)+)[\]】]"
+ )
# Also match "Source X" or "source X" patterns
self.source_word_pattern = re.compile(r"\b[Ss]ource\s+(\d+)\b")
self.sources_pattern = re.compile(
@@ -458,8 +463,13 @@ class QuartoExporter:
"""Export markdown documents to Quarto (.qmd) format."""
def __init__(self):
- self.citation_pattern = re.compile(r"(?<!\[)\[(\d+)\](?!\])")
- self.comma_citation_pattern = re.compile(r"\[(\d+(?:,\s*\d+)+)\]")
+ # Also match Unicode lenticular brackets 【】 (U+3010 and U+3011) that LLMs sometimes generate
+ self.citation_pattern = re.compile(
+ r"(?<![\[【])[\[【](\d+)[\]】](?![\]】])"
+ )
+ self.comma_citation_pattern = re.compile(
+ r"[\[【](\d+(?:,\s*\d+)+)[\]】]"
+ )
def export_to_quarto(self, content: str, title: str = None) -> str:
"""
@@ -813,7 +823,8 @@ class LaTeXExporter:
"""Export markdown documents to LaTeX format."""
def __init__(self):
- self.citation_pattern = re.compile(r"\[(\d+)\]")
+ # Also match Unicode lenticular brackets 【】 (U+3010 and U+3011) that LLMs sometimes generate
+ self.citation_pattern = re.compile(r"[\[【](\d+)[\]】]")
self.heading_patterns = [
(re.compile(r"^# (.+)$", re.MULTILINE), r"\\section{\1}"),
(re.compile(r"^## (.+)$", re.MULTILINE), r"\\subsection{\1}"),
diff --git a/tests/text_optimization/test_citation_formatter.py b/tests/text_optimization/test_citation_formatter.py
index 8fe863d0f9..33e686792a 100644
--- a/tests/text_optimization/test_citation_formatter.py
+++ b/tests/text_optimization/test_citation_formatter.py
@@ -489,6 +489,311 @@ A Nature article [5] and OpenAI research [6].
)
assert "[[openai.com-1]](https://openai.com/research/gpt4)" in result
+ def test_unicode_lenticular_bracket_citations(self):
+ """Test that Unicode lenticular brackets【】are recognized and converted."""
+ content = """# Research Report
+
+Research shows findings 【1】 and more evidence 【2】.
+Multiple citations 【1】【2】 are also used.
+
+## Sources
+
+[1] First Source
+ URL: https://example.com/1
+
+[2] Second Source
+ URL: https://example.com/2
+"""
+ formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS)
+ result = formatter.format_document(content)
+
+ # Check lenticular brackets are converted to hyperlinks
+ assert "[[1]](https://example.com/1)" in result
+ assert "[[2]](https://example.com/2)" in result
+
+ def test_unicode_lenticular_comma_citations(self):
+ """Test comma-separated lenticular citations【1, 2, 3】."""
+ content = """# Report
+
+Multiple sources 【1, 2】 confirm this.
+
+## Sources
+
+[1] Source One
+ URL: https://example.com/1
+
+[2] Source Two
+ URL: https://example.com/2
+"""
+ formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS)
+ result = formatter.format_document(content)
+
+ # Check comma-separated lenticular citations are expanded
+ assert (
+ "[[1]](https://example.com/1)[[2]](https://example.com/2)" in result
+ )
+
+ def test_mixed_bracket_styles(self):
+ """Test documents with both standard [1] and lenticular【2】brackets."""
+ content = """# Report
+
+Standard citation [1] and lenticular 【2】 in same doc.
+
+## Sources
+
+[1] First Source
+ URL: https://example.com/1
+
+[2] Second Source
+ URL: https://example.com/2
+"""
+ formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS)
+ result = formatter.format_document(content)
+
+ # Both bracket styles should be converted
+ assert "[[1]](https://example.com/1)" in result
+ assert "[[2]](https://example.com/2)" in result
+
+ def test_lenticular_multi_digit_citations(self):
+ """Test lenticular brackets with multi-digit citation numbers."""
+ content = """# Report
+
+Citations with higher numbers 【10】 and 【99】 work correctly.
+
+## Sources
+
+[10] Tenth Source
+ URL: https://example.com/10
+
+[99] Ninety-ninth Source
+ URL: https://example.com/99
+"""
+ formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS)
+ result = formatter.format_document(content)
+
+ assert "[[10]](https://example.com/10)" in result
+ assert "[[99]](https://example.com/99)" in result
+
+ def test_lenticular_triple_comma_citations(self):
+ """Test lenticular brackets with three or more comma-separated numbers."""
+ content = """# Report
+
+Many sources 【1, 2, 3】 and even more 【1,2,3,4】 confirm this.
+
+## Sources
+
+[1] Source One
+ URL: https://example.com/1
+
+[2] Source Two
+ URL: https://example.com/2
+
+[3] Source Three
+ URL: https://example.com/3
+
+[4] Source Four
+ URL: https://example.com/4
+"""
+ formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS)
+ result = formatter.format_document(content)
+
+ # Three comma-separated should expand
+ assert (
+ "[[1]](https://example.com/1)[[2]](https://example.com/2)"
+ "[[3]](https://example.com/3)" in result
+ )
+ # Four comma-separated (no spaces) should also expand
+ assert (
+ "[[1]](https://example.com/1)[[2]](https://example.com/2)"
+ "[[3]](https://example.com/3)[[4]](https://example.com/4)" in result
+ )
+
+ def test_lenticular_consecutive_mixed(self):
+ """Test alternating standard and lenticular consecutive citations."""
+ content = """# Report
+
+Mixed consecutive [1]【2】[3] citations work.
+Also reversed 【1】[2]【3】 order.
+
+## Sources
+
+[1] First Source
+ URL: https://example.com/1
+
+[2] Second Source
+ URL: https://example.com/2
+
+[3] Third Source
+ URL: https://example.com/3
+"""
+ formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS)
+ result = formatter.format_document(content)
+
+ # All citations should be converted regardless of bracket style
+ assert "[[1]](https://example.com/1)" in result
+ assert "[[2]](https://example.com/2)" in result
+ assert "[[3]](https://example.com/3)" in result
+
+ def test_lenticular_domain_hyperlinks_mode(self):
+ """Test lenticular brackets with domain hyperlinks mode."""
+ content = """# Report
+
+Research from 【1】 and 【2】 shows results.
+
+## Sources
+
+[1] ArXiv Paper
+ URL: https://arxiv.org/abs/2024.1234
+
+[2] Nature Article
+ URL: https://www.nature.com/articles/s41586-024-5678
+"""
+ formatter = CitationFormatter(CitationMode.DOMAIN_HYPERLINKS)
+ result = formatter.format_document(content)
+
+ assert "[[arxiv.org]](https://arxiv.org/abs/2024.1234)" in result
+ assert (
+ "[[nature.com]](https://www.nature.com/articles/s41586-024-5678)"
+ in result
+ )
+
+ def test_lenticular_domain_id_hyperlinks_mode(self):
+ """Test lenticular brackets with domain ID hyperlinks mode."""
+ content = """# Report
+
+Multiple GitHub sources 【1】 and 【2】 referenced.
+
+## Sources
+
+[1] First GitHub Repo
+ URL: https://github.com/user/repo1
+
+[2] Second GitHub Repo
+ URL: https://github.com/user/repo2
+"""
+ formatter = CitationFormatter(CitationMode.DOMAIN_ID_HYPERLINKS)
+ result = formatter.format_document(content)
+
+ assert "[[github.com-1]](https://github.com/user/repo1)" in result
+ assert "[[github.com-2]](https://github.com/user/repo2)" in result
+
+ def test_lenticular_in_bullet_list(self):
+ """Test lenticular brackets within bullet point lists."""
+ content = """# Report
+
+Key findings:
+- First point 【1】
+- Second point 【2】
+- Combined evidence 【1, 2】
+
+## Sources
+
+[1] First Source
+ URL: https://example.com/1
+
+[2] Second Source
+ URL: https://example.com/2
+"""
+ formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS)
+ result = formatter.format_document(content)
+
+ assert "- First point [[1]](https://example.com/1)" in result
+ assert "- Second point [[2]](https://example.com/2)" in result
+ assert (
+ "[[1]](https://example.com/1)[[2]](https://example.com/2)" in result
+ )
+
+ def test_lenticular_without_matching_source(self):
+ """Test lenticular brackets referencing non-existent sources."""
+ content = """# Report
+
+Valid citation 【1】 and invalid 【99】 reference.
+
+## Sources
+
+[1] Only Source
+ URL: https://example.com/1
+"""
+ formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS)
+ result = formatter.format_document(content)
+
+ # Valid citation should be hyperlinked
+ assert "[[1]](https://example.com/1)" in result
+ # Invalid citation should remain as plain text (not hyperlinked)
+ assert "[[99]]" not in result
+ assert "【99】" in result or "[99]" in result
+
+ def test_lenticular_no_space_before_or_after(self):
+ """Test lenticular brackets without spaces before or after."""
+ content = """# Report
+
+Text immediately before【1】and after without spaces.
+Also works at end of sentence【2】.
+And word【1】word with citations embedded.
+Multiple【1】【2】consecutive without spaces.
+Standard brackets work too: word[1]word and end[2].
+
+## Sources
+
+[1] First Source
+ URL: https://example.com/1
+
+[2] Second Source
+ URL: https://example.com/2
+"""
+ formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS)
+ result = formatter.format_document(content)
+
+ # Lenticular brackets should be converted despite no spaces
+ assert "before[[1]](https://example.com/1)and" in result
+ assert "sentence[[2]](https://example.com/2)." in result
+ assert "word[[1]](https://example.com/1)word" in result
+ assert (
+ "[[1]](https://example.com/1)[[2]](https://example.com/2)consecutive"
+ in result
+ )
+ # Standard brackets should work the same way
+ assert "word[[1]](https://example.com/1)word" in result
+ assert "end[[2]](https://example.com/2)." in result
+
+ def test_lenticular_real_world_mixed_example(self):
+ """Test real-world scenario with mixed bracket styles throughout."""
+ content = """# AI Safety Research Summary
+
+Query: AI alignment techniques
+
+Recent research【1】has explored various approaches to AI safety. The RLHF
+method [2] has shown promising results. Constitutional AI【3】builds on
+these foundations [1, 2]. Multiple studies【1】【2】【3】confirm the
+effectiveness of these techniques. A comprehensive survey [1, 2, 3] covers
+all major approaches, while recent work【2, 3】focuses on scalability.
+
+## Sources
+
+[1] RLHF: Training Language Models
+ URL: https://arxiv.org/abs/2024.rlhf
+
+[2] Constitutional AI: Harmlessness from Feedback
+ URL: https://anthropic.com/constitutional-ai
+
+[3] Scalable Oversight of AI Systems
+ URL: https://openai.com/research/scalable-oversight
+"""
+ formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS)
+ result = formatter.format_document(content)
+
+ # All citations should be converted
+ assert "[[1]](https://arxiv.org/abs/2024.rlhf)" in result
+ assert "[[2]](https://anthropic.com/constitutional-ai)" in result
+ assert "[[3]](https://openai.com/research/scalable-oversight)" in result
+
+ # Consecutive lenticular citations should work
+ assert (
+ "[[1]](https://arxiv.org/abs/2024.rlhf)"
+ "[[2]](https://anthropic.com/constitutional-ai)"
+ "[[3]](https://openai.com/research/scalable-oversight)" in result
+ )
+
class TestLaTeXExporter:
"""Test cases for LaTeX export functionality."""
@@ -576,6 +881,71 @@ Text with citations [1] and [2].
# Should not include bibliography
assert r"\begin{thebibliography}" not in result
+ def test_unicode_lenticular_citations(self):
+ """Test LaTeX export converts lenticular citations to \\cite{N} format."""
+ content = """# Report
+
+Research with lenticular citations 【1】 and 【2】.
+
+## Sources
+
+[1] First Source
+ URL: https://example.com/1
+
+[2] Second Source
+ URL: https://example.com/2
+"""
+ exporter = LaTeXExporter()
+ result = exporter.export_to_latex(content)
+
+ # Check lenticular citations are converted to LaTeX cite format
+ assert r"\cite{1}" in result
+ assert r"\cite{2}" in result
+
+ def test_unicode_lenticular_mixed_with_standard(self):
+ """Test LaTeX with mixed standard and lenticular brackets."""
+ content = """# Report
+
+Standard [1] and lenticular 【2】 and consecutive【3】[4] citations.
+
+## Sources
+
+[1] First
+ URL: https://example.com/1
+[2] Second
+ URL: https://example.com/2
+[3] Third
+ URL: https://example.com/3
+[4] Fourth
+ URL: https://example.com/4
+"""
+ exporter = LaTeXExporter()
+ result = exporter.export_to_latex(content)
+
+ assert r"\cite{1}" in result
+ assert r"\cite{2}" in result
+ assert r"\cite{3}" in result
+ assert r"\cite{4}" in result
+
+ def test_unicode_lenticular_multi_digit(self):
+ """Test LaTeX export with multi-digit lenticular citations."""
+ content = """# Report
+
+Higher numbers 【10】 and 【25】 work too.
+
+## Sources
+
+[10] Tenth Source
+ URL: https://example.com/10
+[25] Twenty-fifth Source
+ URL: https://example.com/25
+"""
+ exporter = LaTeXExporter()
+ result = exporter.export_to_latex(content)
+
+ assert r"\cite{10}" in result
+ assert r"\cite{25}" in result
+
class TestRISExporter:
"""Test cases for RIS export functionality."""
@@ -765,3 +1135,90 @@ Multiple studies [1, 2, 3] have shown promising results.
# Bibliography note should still appear but be empty
assert "Bibliography File Required" in result
+
+ def test_unicode_lenticular_citations(self):
+ """Test Quarto export converts lenticular citations to [@refN] format."""
+ content = """# Report
+
+Single lenticular 【1】 and comma-separated 【1, 2】 citations.
+
+## Sources
+
+[1] First Source
+ URL: https://example.com/1
+
+[2] Second Source
+ URL: https://example.com/2
+"""
+ exporter = QuartoExporter()
+ result = exporter.export_to_quarto(content)
+
+ # Check lenticular citations are converted to Quarto format
+ assert "[@ref1]" in result
+ assert "[@ref1, @ref2]" in result
+
+ def test_unicode_lenticular_triple_comma(self):
+ """Test Quarto export with three comma-separated lenticular citations."""
+ content = """# Report
+
+Multiple sources 【1, 2, 3】 referenced together.
+
+## Sources
+
+[1] First
+ URL: https://example.com/1
+[2] Second
+ URL: https://example.com/2
+[3] Third
+ URL: https://example.com/3
+"""
+ exporter = QuartoExporter()
+ result = exporter.export_to_quarto(content)
+
+ assert "[@ref1, @ref2, @ref3]" in result
+
+ def test_unicode_lenticular_mixed_with_standard(self):
+ """Test Quarto export with mixed bracket styles."""
+ content = """# Report
+
+Standard [1] and lenticular 【2】 and mixed comma [1, 2] and 【2, 3】.
+
+## Sources
+
+[1] First
+ URL: https://example.com/1
+[2] Second
+ URL: https://example.com/2
+[3] Third
+ URL: https://example.com/3
+"""
+ exporter = QuartoExporter()
+ result = exporter.export_to_quarto(content)
+
+ assert "[@ref1]" in result
+ assert "[@ref2]" in result
+ assert "[@ref1, @ref2]" in result
+ assert "[@ref2, @ref3]" in result
+
+ def test_unicode_lenticular_consecutive(self):
+ """Test Quarto export with consecutive lenticular citations."""
+ content = """# Report
+
+Consecutive lenticular【1】【2】【3】citations.
+
+## Sources
+
+[1] First
+ URL: https://example.com/1
+[2] Second
+ URL: https://example.com/2
+[3] Third
+ URL: https://example.com/3
+"""
+ exporter = QuartoExporter()
+ result = exporter.export_to_quarto(content)
+
+ # Each should be converted individually
+ assert "[@ref1]" in result
+ assert "[@ref2]" in result
+ assert "[@ref3]" in result