lingologos-thinkingpython / tests /test_language_free_plan.py
tailot's picture
thinking-python: reference implementation, corpus and gates
4bc63c7 verified
Raw History Blame Contribute Delete
2.78 kB
"""The plan reaches executor and gates with no trace of the surface request."""
import json
import os
import sys
import unittest
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)
from lingologos_thinkingpython import intent # noqa: E402
FRAMES = {"guard-clause": ["early return", "nested if"]}
REQUESTS_FILE = os.path.join(ROOT, "catalog", "smoke_requests.json")
def language_free_rows():
with open(REQUESTS_FILE, "r", encoding="utf-8") as handle:
return [(r["lang"], r["text"]) for r in json.load(handle)["language_free"]]
class LanguageFreePlan(unittest.TestCase):
def setUp(self):
self.utterances = language_free_rows()
self.assertTrue(self.utterances)
def test_plan_carries_no_surface_text(self):
from lingologos_thinkingpython import lexicon
for lang, text in self.utterances:
frames = {"guard-clause": lexicon.load(lang)["frames"]["guard-clause"]}
ir = intent.route(text, lang, frames, known_symbols=["parse"])
self.assertEqual(ir["frame"], "guard-clause", lang)
self.assertTrue(intent.is_language_free(ir), lang)
self.assertNotIn(text, repr(ir))
def test_plan_keys_are_the_same_whatever_the_variety(self):
keys = set()
for lang, text in self.utterances:
ir = intent.route(text, lang, self.frames(lang), known_symbols=["parse"])
keys.add(tuple(sorted(k for k in ir if k != "reason")))
self.assertEqual(len(keys), 1)
def frames(self, lang):
from lingologos_thinkingpython import lexicon
return {"guard-clause": lexicon.load(lang)["frames"]["guard-clause"]}
def test_plan_hash_is_identical_across_varieties(self):
"""The plan proper -- frame, anchor, ordered candidate ids -- is one hash.
Scores stay out: they measure how well the surface form matched, and that
number is allowed to differ between varieties.
"""
import hashlib
hashes = set()
for lang, text in self.utterances:
ir = intent.route(text, lang, self.frames(lang), known_symbols=["parse"])
plan = {"frame": ir["frame"], "anchor": ir["anchor"],
"candidates": [name for name, _ in ir["candidates"]]}
hashes.add(hashlib.sha256(json.dumps(plan, sort_keys=True).encode("utf-8")).hexdigest())
self.assertEqual(len(hashes), 1, hashes)
def test_varieties_are_all_admissible(self):
from lingologos_thinkingpython import lexicon
for code in lexicon.LANGS:
data = lexicon.load(code)
self.assertEqual(data["lang"], code)
self.assertTrue(data["frames"])
if __name__ == "__main__":
unittest.main()