File size: 2,778 Bytes
4bc63c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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()