fix(summarizer): stamp ticker and map urls from the article, not the model
The reduce model returns url="" and save_batch was persisting that blank. Match each headline to a selected article title and store that article's url. A null article url or an ambiguous match stores "" — never a guessed or model-supplied URL.
This commit is contained in:
parent
d04ddc9afa
commit
653e02b09f
3 changed files with 298 additions and 4 deletions
|
|
@ -291,6 +291,69 @@ def normalize_title(title) -> str:
|
|||
return " ".join(_NON_ALNUM_RE.sub(" ", text).split())
|
||||
|
||||
|
||||
# Headline must clear this to take an article URL. Above the
|
||||
# "storm hits Texas" vs "storm hits Tennessee" band (~0.67) so a near-miss
|
||||
# cannot steal another story's URL. A tie stores "" — a wrong URL is worse
|
||||
# than a blank one.
|
||||
ARTICLE_URL_MIN_SIMILARITY = 0.85
|
||||
# Word-sequence containment only counts when the shorter side is specific.
|
||||
_URL_CONTAIN_MIN_WORDS = 4
|
||||
|
||||
|
||||
def _contains_word_sequence(outer: str, inner: str) -> bool:
|
||||
outer_words = outer.split()
|
||||
inner_words = inner.split()
|
||||
if len(inner_words) < _URL_CONTAIN_MIN_WORDS or len(inner_words) > len(outer_words):
|
||||
return False
|
||||
width = len(inner_words)
|
||||
return any(
|
||||
outer_words[i : i + width] == inner_words
|
||||
for i in range(len(outer_words) - width + 1)
|
||||
)
|
||||
|
||||
|
||||
def article_url_for_row(row: dict, articles: list | None) -> str:
|
||||
"""URL of the selected article this ticker/map row refers to, else ``""``.
|
||||
|
||||
Matches the row headline to an article title. Never reads the model's
|
||||
``url`` field. A null, missing, or non-string article url stores ``""``.
|
||||
Two articles that score the same (or no article that clears the bar)
|
||||
store ``""`` so a headline cannot be wired to the wrong story.
|
||||
"""
|
||||
if not isinstance(row, dict):
|
||||
return ""
|
||||
headline = normalize_title(row.get("headline"))
|
||||
if not headline:
|
||||
return ""
|
||||
best_score = 0.0
|
||||
best: list[dict] = []
|
||||
for article in articles or []:
|
||||
if not isinstance(article, dict):
|
||||
continue
|
||||
title = normalize_title(article.get("title"))
|
||||
if not title:
|
||||
continue
|
||||
if headline == title:
|
||||
score = 1.0
|
||||
elif _contains_word_sequence(headline, title) or _contains_word_sequence(title, headline):
|
||||
score = 0.95
|
||||
else:
|
||||
score = title_similarity(headline, title)
|
||||
if score + 1e-9 < ARTICLE_URL_MIN_SIMILARITY:
|
||||
continue
|
||||
if score > best_score + 1e-9:
|
||||
best_score = score
|
||||
best = [article]
|
||||
elif abs(score - best_score) <= 1e-9:
|
||||
best.append(article)
|
||||
if len(best) != 1:
|
||||
return ""
|
||||
url = best[0].get("url")
|
||||
if not isinstance(url, str):
|
||||
return ""
|
||||
return url.strip()
|
||||
|
||||
|
||||
def title_similarity(normalised_left: str, normalised_right: str) -> float:
|
||||
"""0..1 story-similarity of two already-normalised titles.
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue