Skip to content

Commit 64df9ca

Browse files
committed
fix: preserve word boundaries around quotes
1 parent 0785751 commit 64df9ca

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

check_hallucinations/main.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
SEMANTICSCHOLAR_DELAY = 1.0
1919
CACHE_DIR = os.path.expanduser("~/.cache/check-hallucinations")
2020

21-
# Quotes are typographical decoration for title matching. Removing them also
22-
# makes BibTeX's TeX-style ``...'' quotes compare equal to straight or curly
23-
# quotes returned by external APIs.
24-
_TITLE_QUOTE_TRANSLATION = str.maketrans("", "", "`'\"\u2018\u2019\u201c\u201d")
21+
# Quotes are typographical decoration for title matching. Treating them as
22+
# word boundaries makes BibTeX's TeX-style ``...'' quotes compare equal to
23+
# straight or curly quotes, including API metadata missing a space after one.
24+
_TITLE_QUOTE_TRANSLATION = str.maketrans(
25+
{quote: " " for quote in "`'\"\u2018\u2019\u201c\u201d"}
26+
)
2527

2628

2729
def _api_key():
@@ -34,7 +36,7 @@ def _strip_diacritics(s):
3436

3537

3638
def _normalize_title(title):
37-
return _strip_diacritics(
39+
normalized = _strip_diacritics(
3840
title.lower()
3941
.strip()
4042
.translate(_TITLE_QUOTE_TRANSLATION)
@@ -46,8 +48,6 @@ def _normalize_title(title):
4648
.replace("\\#", "#")
4749
.replace(": ", ". ")
4850
.replace(",", " ")
49-
.replace(" ", " ")
50-
.replace(" ", " ")
5151
.replace("{", "")
5252
.replace("}", "")
5353
.replace(" ", " ")
@@ -58,6 +58,7 @@ def _normalize_title(title):
5858
.replace("-", " ")
5959
.replace(" ...", "...")
6060
)
61+
return " ".join(normalized.split())
6162

6263

6364
def _cache_path(subdir, key):

test_normalize.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ def test_latex_quotes_match_semantic_scholar_quotes():
3636
)
3737

3838

39+
def test_quote_preserves_word_boundary_for_missing_api_space():
40+
bib = "On LLMs' Internal Representation of Code Correctness"
41+
ss = "On LLMs'Internal Representation of Code Correctness"
42+
assert _title_matches(ss, bib)
43+
44+
3945
def test_double_dash_vs_single_dash():
4046
bib = "DeepSeek-Coder: When the large language model meets programming -- the rise of code intelligence"
4147
ss = "DeepSeek-Coder: When the Large Language Model Meets Programming - The Rise of Code Intelligence"

0 commit comments

Comments
 (0)