File size: 3,525 Bytes
1521ce5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations
import re


def check_answer_in_evidence(answer: str, evidence_sessions: list[dict]) -> bool:
    evidence_text = ""
    for sess in evidence_sessions:
        for turn in sess.get("turns", []):
            evidence_text += " " + turn["content"]
    answer_lower = answer.lower().strip()
    return answer_lower in evidence_text.lower()


def check_question_no_answer_leak(question: str, answer: str) -> bool:
    """Heuristic answer-leak guard.

    Counts only "content" tokens — strips common stopwords AND time/measurement
    units that are routinely shared between question and answer for two_hop /
    temp_reasoning ("How many DAYS passed..." → "5 DAYS"). Without unit
    stripping, ~all duration / count answers got flagged.

    Threshold: leak_ratio >= 0.6 is rejected. We bumped from 0.5 → 0.6 so that
    a single shared content token in a 2-token answer ("25 minutes") doesn't
    auto-fail.
    """
    stopwords = {
        "the", "a", "an", "is", "was", "are", "were", "i", "my", "me", "do",
        "what", "which", "how", "when", "where", "that", "this", "it",
        "of", "to", "in", "on", "at", "for", "with", "from", "by", "and", "or",
    }
    unit_words = {
        "day", "days", "week", "weeks", "month", "months", "year", "years",
        "hour", "hours", "minute", "minutes", "second", "seconds",
        "dollar", "dollars", "$", "cent", "cents",
        "mile", "miles", "kilometer", "kilometers", "km", "meter", "meters",
        "pound", "pounds", "kg", "kilogram", "kilograms",
        "time", "times", "ago", "before", "after", "between",
    }
    skip = stopwords | unit_words

    def tokens(text):
        return {t.strip(".,!?;:'\"()") for t in text.lower().split()}

    answer_tokens = tokens(answer) - skip
    question_tokens = tokens(question) - skip
    if not answer_tokens:
        return True
    overlap = answer_tokens & question_tokens
    leak_ratio = len(overlap) / len(answer_tokens)
    return leak_ratio < 0.6


def check_rewrite_no_answer_leak(rewrite_query: str, answer: str) -> bool:
    if not rewrite_query or rewrite_query == "none":
        return True
    return check_question_no_answer_leak(rewrite_query, answer)


def check_entity_in_predictions(rewrite_query: str, predictions: list[str]) -> bool:
    if not rewrite_query or rewrite_query == "none":
        return True
    pred_text = " ".join(predictions).lower()
    rewrite_tokens = rewrite_query.lower().split()
    stopwords = {"the", "a", "an", "is", "was", "are", "were", "i", "my", "me",
                 "what", "which", "how", "when", "where", "for", "to", "of", "in",
                 "and", "or", "on", "at", "by", "with", "from"}
    meaningful_tokens = [t for t in rewrite_tokens if t not in stopwords and len(t) > 2]
    if not meaningful_tokens:
        return True
    found = sum(1 for t in meaningful_tokens if t in pred_text)
    return found / len(meaningful_tokens) >= 0.5


def check_no_user_specific_info(rewrite_query: str, original_query: str) -> bool:
    if not rewrite_query or rewrite_query == "none":
        return True
    original_tokens = set(original_query.lower().split())
    rewrite_tokens = set(rewrite_query.lower().split())
    new_tokens = rewrite_tokens - original_tokens
    stopwords = {"the", "a", "an", "is", "was", "are", "were", "i", "my", "me",
                 "what", "which", "how", "when", "where", "for", "to", "of", "in"}
    meaningful_new = new_tokens - stopwords
    return len(meaningful_new) <= 3