File size: 3,873 Bytes
6f2ed01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
"""Unit tests for scoring_full.py. Run before trusting any K_m."""
import os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from scoring_full import score

CANBERRA = ["Canberra", "Canberra, ACT"]
DUTCH = ["Dutch", "Nederlands", "dutch language"]
YEAR = ["1987"]
# "can" is a real Wikidata alias of Canada, carried by 67 facts in this pool
CANADA = ["Canada", "can", "CAN", "Dominion of Canada"]

CASES = [
    (" Canberra",               CANBERRA, "city", None, "correct", "bare answer"),
    (" Canberra.",              CANBERRA, "city", None, "correct", "trailing period"),
    ("The answer is Canberra.", CANBERRA, "city", None, "correct", "lead-in"),
    ("  canberra  ",            CANBERRA, "city", None, "correct", "case/space"),
    ("Canberra, ACT",           CANBERRA, "city", None, "correct", "alias with comma"),
    ("Sydney",                  CANBERRA, "city", None, "incorrect", "wrong entity"),
    ("It is not Canberra.",     CANBERRA, "city", None, "ambiguous", "negation"),
    ("Canberra or Sydney",      CANBERRA, "city", None, "ambiguous", "conflicting list"),
    ("Some sources say Canberra, but the answer is Melbourne.",
                                CANBERRA, "city", None, "ambiguous", "self-correction"),
    ("",                        CANBERRA, "city", None, "unparseable", "empty"),
    ("\n\n",                    CANBERRA, "city", None, "unparseable", "whitespace only"),
    ("...",                     CANBERRA, "city", None, "unparseable", "punctuation only"),

    ("I don't know.",           CANBERRA, "city", None, "abstain", "explicit refusal"),
    ("I'm not sure, maybe Sydney.", CANBERRA, "city", None, "abstain", "hedged refusal"),
    ("Unknown",                 CANBERRA, "city", None, "abstain", "unknown"),

    (" Dutch",                  DUTCH, "language", None, "correct", "primary alias"),
    (" Nederlands",             DUTCH, "language", None, "correct", "non-primary alias"),
    ("The Dutch language",      DUTCH, "language", None, "correct", "article + alias"),
    ("German",                  DUTCH, "language", None, "incorrect", "wrong language"),
    ("Amsterdam, which is in the Netherlands, where people speak Dutch and",
                                DUTCH, "language", None, "ambiguous",
                                "gold buried in prose is NOT a silent correct"),

    ("1987",                    YEAR, "year", "year", "correct", "exact year"),
    ("He was born in 1987",     YEAR, "year", "year", "correct", "year in short prose"),
    ("12 March 1987",           YEAR, "year", "year", "correct", "full date, year gold"),
    ("1988",                    YEAR, "year", "year", "incorrect", "wrong year"),
    ("1987 or 1988",            YEAR, "year", "year", "ambiguous", "two years"),
    ("1987",       ["12 March 1987"], "date", "date", "ambiguous", "gold wants full date"),
    ("12 March 1987", ["12 March 1987"], "date", "date", "correct", "full date matches"),

    ("It can be Germany",       CANADA, "country", None, "incorrect",
     "stopword alias must not match by containment"),
    ("Canada",                  CANADA, "country", None, "correct", "primary alias"),
    ("CAN",                     CANADA, "country", None, "correct", "short alias, EXACT only"),
    ("the Dominion of Canada is", CANADA, "country", None, "correct",
     "long alias still matches by containment"),
]


def main():
    bad = 0
    for gen, al, at, gran, want, note in CASES:
        got = score(gen, al, answer_type=at, granularity=gran)
        ok = got["label"] == want
        bad += 0 if ok else 1
        print(f"{'ok ' if ok else 'FAIL'} {want:12s} got={got['label']:12s} "
              f"{got['scorer']:34s} {note}")
    print(f"\n{len(CASES) - bad}/{len(CASES)} passed")
    return 1 if bad else 0


if __name__ == "__main__":
    sys.exit(main())