File size: 5,161 Bytes
152f558 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | """Per-claim grounding features. Deliberately classical so they re-implement cleanly in JS.
The claim is scored against the source using lexical support signals:
overlap, fuzzy match, number/entity support, novel-content ratio, and a contradiction signal.
"""
from __future__ import annotations
import re
STOP = set("a an the of to in on at for and or but with by from as is are was were be been being "
"it its this that these those they them their he she his her you your we our i me my "
"has have had do does did will would can could should may might must not no "
"about over under into out up down off than then so such also just only very more most "
"which who whom whose what when where why how".split())
NEG = set("not no never none cannot can't don't doesn't didn't won't isn't aren't wasn't weren't "
"without fell dropped declined fewer less lower nothing failed".split())
FEATURE_NAMES = [
"tok_jaccard_doc", "tok_recall_doc", "best_sent_jaccard", "best_sent_recall",
"fuzzy_best", "fuzzy_doc", "number_support", "entity_support", "novel_content_ratio",
"contradiction_signal", "has_number", "has_entity", "claim_len_tokens", "n_source_sents",
]
def sentences(text: str):
return [s.strip() for s in re.split(r"(?<=[.!?])\s+", text.strip()) if s.strip()]
def content_tokens(text: str):
toks = re.findall(r"[a-z0-9]+", text.lower())
return [t for t in toks if t not in STOP and len(t) > 1]
def numbers(text: str):
# standalone integers / decimals, ignoring those glued inside words
return set(re.findall(r"(?<![A-Za-z])\d+(?:\.\d+)?", text))
NUM_WORDS = {"one": "1", "two": "2", "three": "3", "four": "4", "five": "5", "six": "6",
"seven": "7", "eight": "8", "nine": "9", "ten": "10"}
def numbers_norm(text: str):
nums = set(numbers(text))
for w, d in NUM_WORDS.items():
if re.search(rf"\b{w}\b", text.lower()):
nums.add(d)
return nums
def entities(text: str):
# capitalized spans not at sentence start only — approx proper nouns; keep multiword + single
spans = re.findall(r"\b([A-Z][a-zA-Z]+(?:\s+[A-Z][a-zA-Z]+)*)\b", text)
out = set()
for s in spans:
for w in s.split():
if w.lower() not in STOP:
out.add(w.lower())
return out
def _jaccard(a: set, b: set) -> float:
if not a and not b:
return 0.0
return len(a & b) / len(a | b) if (a | b) else 0.0
def _recall(a: set, b: set) -> float:
# fraction of a's tokens present in b
return len(a & b) / len(a) if a else 0.0
def _bigrams(s: str):
s = s.lower()
return [s[i:i+2] for i in range(len(s) - 1)]
def _fuzzy(a: str, b: str) -> float:
# char-bigram Dice coefficient; deterministic and trivially portable to JS
A, B = _bigrams(a), _bigrams(b)
if not A or not B:
return 0.0
from collections import Counter
ca, cb = Counter(A), Counter(B)
inter = sum((ca & cb).values())
return 2 * inter / (len(A) + len(B))
def features(claim: str, source: str) -> list[float]:
src_sents = sentences(source)
claim_tok = set(content_tokens(claim))
doc_tok = set(content_tokens(source))
f = []
f.append(_jaccard(claim_tok, doc_tok)) # tok_jaccard_doc
f.append(_recall(claim_tok, doc_tok)) # tok_recall_doc
best_j = best_r = 0.0
best_fuzzy = 0.0
best_sent = ""
for s in src_sents:
st = set(content_tokens(s))
j = _jaccard(claim_tok, st)
r = _recall(claim_tok, st)
if r > best_r or (r == best_r and j > best_j):
best_r, best_j, best_sent = r, j, s
best_fuzzy = max(best_fuzzy, _fuzzy(claim, s))
f.append(best_j) # best_sent_jaccard
f.append(best_r) # best_sent_recall
f.append(best_fuzzy) # fuzzy_best
f.append(_fuzzy(claim, source)) # fuzzy_doc
cnums, dnums = numbers_norm(claim), numbers_norm(source)
f.append(1.0 if not cnums else len(cnums & dnums) / len(cnums)) # number_support
cent, dent = entities(claim), entities(source)
f.append(1.0 if not cent else len(cent & dent) / len(cent)) # entity_support
f.append(1.0 - _recall(claim_tok, doc_tok)) # novel_content_ratio
# contradiction: claim has a negation token that its best-match source sentence lacks (or vice-versa)
claim_neg = bool(set(re.findall(r"[a-z']+", claim.lower())) & NEG)
best_neg = bool(set(re.findall(r"[a-z']+", best_sent.lower())) & NEG)
f.append(1.0 if claim_neg != best_neg else 0.0) # contradiction_signal
f.append(1.0 if cnums else 0.0) # has_number
f.append(1.0 if cent else 0.0) # has_entity
f.append(float(len(claim_tok))) # claim_len_tokens
f.append(float(len(src_sents))) # n_source_sents
return f
|