#!/usr/bin/env python3 """Tests for the extractability classifier. Pure python, no deps.""" import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from src.extractability import classify, normalise, is_subsequence, PAIRS PASS = FAIL = 0 def chk(c, l, d=""): global PASS, FAIL if c: PASS += 1; print(f" ok {l}") else: FAIL += 1; print(f" FAIL {l} {d}") print("[exact]") chk(classify("$9,438.", "the average cost was $9,438. in 1980") == "exact", "literal substring") chk(classify("Average low of 73.3 degrees", "Caribbean: Average low of 73.3 degrees to a high of 82.9") == "exact", "longer span") chk(classify("$9,438", "the cost was $ 9,438 that year") in ("subseq", "exact"), "punctuation-insensitive") print("\n[subsequence]") chk(classify("infections of the oil glands", "Styes are caused by infections of the eyelid oil glands") == "subseq", "tokens in order with insertion") print("\n[overlap80]") chk(classify("staphylococcus bacteria infections eyelid", "eyelid infections from bacteria such as staphylococcus") == "overlap80", "reordered tokens") print("\n[none = abstractive]") chk(classify("Yes, it improves sleep quality.", "Participants reported feeling more rested after the trial.") == "none", "paraphrase is not extractive") print("\n[unicode / indic]") chk(classify("७३.३ डिग्री", "औसत ७३.३ डिग्री तापमान") == "exact", "devanagari substring") chk(normalise(" A B C ") == "a b c", "NFKC + whitespace collapse") print("\n[edge cases]") chk(classify("", "anything") == "none", "empty answer") chk(classify("x", "") == "none", "empty passage") chk(is_subsequence(["a", "c"], ["a", "b", "c"]), "subsequence helper") chk(not is_subsequence(["c", "a"], ["a", "b", "c"]), "order matters") print("\n[pair selection]") chk(set(PAIRS) == {"english", "translated"}, "both pairs registered") chk(PAIRS["english"] == ("answer_en", "text_en"), "english pair maps to the English fields") chk(PAIRS["translated"] == ("answer", "text"), "translated pair maps to what the reader actually reads") # The whole point of --pair translated: a paraphrased translation destroys the # verbatim span even when the English original had one. chk(classify("73.3 degrees", "Average low of 73.3 degrees to a high of 82.9") == "exact", "English original is a clean span") chk(classify("तापमान ७३.३ डिग्री तक गिर जाता है", "कैरिबियन में न्यूनतम औसत ७३.३ डिग्री और अधिकतम ८२.९ डिग्री रहता है") == "none", "same fact, reworded translation -> not extractive") print(f"\n{'='*50}\n {PASS} passed, {FAIL} failed\n{'='*50}") sys.exit(1 if FAIL else 0)