|
| 1 | +import re |
| 2 | +from functools import lru_cache |
| 3 | + |
| 4 | +from ..contradiction import find_contradiction |
| 5 | +from ..models import NLICheck |
| 6 | +from ..text_processing import extract_numbers, has_negation_mismatch, lemmatize_tokens |
| 7 | + |
| 8 | + |
| 9 | +@lru_cache(maxsize=8192) |
| 10 | +def _tokens(text): |
| 11 | + return frozenset(t.lower() for t in re.findall(r"\b[a-zA-Z][a-zA-Z'-]+\b", text or "") if len(t) > 2) |
| 12 | + |
| 13 | + |
| 14 | +@lru_cache(maxsize=8192) |
| 15 | +def _content_lemmas(text): |
| 16 | + return frozenset(lemma for lemma in lemmatize_tokens(text) if len(lemma) > 2) |
| 17 | + |
| 18 | + |
| 19 | +class NLIModel: |
| 20 | + model_quality = 0.75 |
| 21 | + |
| 22 | + def predict(self, premise, hypothesis): |
| 23 | + return self.predict_batch([premise], [hypothesis])[0] |
| 24 | + |
| 25 | + def predict_batch(self, premises, hypotheses): |
| 26 | + return [rule_nli(premise, hypothesis) for premise, hypothesis in zip(premises, hypotheses)] |
| 27 | + |
| 28 | + |
| 29 | +def rule_nli(premise, hypothesis): |
| 30 | + chunk = {"text": premise or "", "numbers": extract_numbers(premise)} |
| 31 | + issue = find_contradiction( |
| 32 | + claim=hypothesis, |
| 33 | + chunk=chunk, |
| 34 | + extract_numbers=extract_numbers, |
| 35 | + has_negation_mismatch=has_negation_mismatch, |
| 36 | + score=1.0, |
| 37 | + threshold=0.0, |
| 38 | + ) |
| 39 | + if issue: |
| 40 | + return NLICheck("CONTRADICTION", 0.84, issue.get("reason", "Contradiction"), model_quality=0.75) |
| 41 | + |
| 42 | + premise_tokens = _tokens(premise) |
| 43 | + hypothesis_tokens = _tokens(hypothesis) |
| 44 | + premise_numbers = set(extract_numbers(premise)) |
| 45 | + hypothesis_numbers = set(extract_numbers(hypothesis)) |
| 46 | + if hypothesis_numbers and not premise_numbers: |
| 47 | + return NLICheck("NEUTRAL", 0.35, "Missing number evidence", model_quality=0.75) |
| 48 | + if hypothesis_numbers and premise_numbers and not hypothesis_numbers.issubset(premise_numbers): |
| 49 | + return NLICheck("NEUTRAL", 0.42, "Number evidence differs", model_quality=0.75) |
| 50 | + if hypothesis_tokens: |
| 51 | + token_overlap = len(premise_tokens & hypothesis_tokens) / len(hypothesis_tokens) |
| 52 | + premise_lemmas = _content_lemmas(premise) |
| 53 | + hypothesis_lemmas = _content_lemmas(hypothesis) |
| 54 | + lemma_overlap = len(premise_lemmas & hypothesis_lemmas) / len(hypothesis_lemmas) if hypothesis_lemmas else 0.0 |
| 55 | + overlap = max(token_overlap, lemma_overlap) |
| 56 | + if overlap >= 0.70: |
| 57 | + return NLICheck("ENTAILMENT", min(0.60 + overlap * 0.30, 0.92), model_quality=0.75) |
| 58 | + return NLICheck("NEUTRAL", 0.50, model_quality=0.75) |
| 59 | + |
| 60 | + |
| 61 | +def sentence_nli(processed_sentence, document=None, nli_model=None, hits=None): |
| 62 | + model = nli_model or NLIModel() |
| 63 | + claim = getattr(processed_sentence, "resolved_text", processed_sentence) |
| 64 | + claim = claim if isinstance(claim, str) else str(claim) |
| 65 | + relevant_hits = hits or [] |
| 66 | + if not relevant_hits and document is not None: |
| 67 | + relevant_hits = [{"sentence": s.resolved_text, "score": 1.0} for s in document[:1]] |
| 68 | + |
| 69 | + best_hit_score = max( |
| 70 | + ( |
| 71 | + (hit.get("score", 0.0) if isinstance(hit, dict) else getattr(hit, "score", 0.0)) |
| 72 | + for hit in relevant_hits |
| 73 | + ), |
| 74 | + default=0.0, |
| 75 | + ) |
| 76 | + min_hit_score = max(0.30, best_hit_score * 0.80) |
| 77 | + premises = [] |
| 78 | + hypotheses = [] |
| 79 | + hit_scores = [] |
| 80 | + for hit in relevant_hits[:5]: |
| 81 | + premise = hit.get("sentence") if isinstance(hit, dict) else getattr(hit, "sentence", str(hit)) |
| 82 | + hit_score = hit.get("score", 0.0) if isinstance(hit, dict) else getattr(hit, "score", 0.0) |
| 83 | + if hit_score < min_hit_score: |
| 84 | + continue |
| 85 | + premises.append(premise) |
| 86 | + hypotheses.append(claim) |
| 87 | + hit_scores.append(hit_score) |
| 88 | + if not premises: |
| 89 | + return NLICheck("NEUTRAL", 0.50, model_quality=getattr(model, "model_quality", 1.0)) |
| 90 | + |
| 91 | + results = model.predict_batch(premises, hypotheses) |
| 92 | + entailments = [r for r in results if r.label == "ENTAILMENT"] |
| 93 | + strong_entailment = max(entailments, key=lambda r: r.score) if entailments else None |
| 94 | + contradictions = [r for r in results if r.label == "CONTRADICTION"] |
| 95 | + if strong_entailment and strong_entailment.score >= 0.82: |
| 96 | + return strong_entailment |
| 97 | + if contradictions: |
| 98 | + return max(contradictions, key=lambda r: r.score) |
| 99 | + if entailments: |
| 100 | + return max(entailments, key=lambda r: r.score) |
| 101 | + return max(results, key=lambda r: r.score) |
0 commit comments