File size: 4,935 Bytes
e9dac18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""The data files: six varieties must describe the same stratum, in their own words."""

import json
from pathlib import Path

import pytest

from lgstratum import canonical, decide, lexicon, segment

ROOT = Path(__file__).resolve().parents[1]
LANGS = lexicon.codes()


def _raw(lang):
    return json.loads((ROOT / "lexicon" / ("%s.json" % lang)).read_text(encoding="utf-8"))


def test_all_six_varieties_are_present_and_named():
    assert sorted(LANGS) == ["ar", "en", "es", "hi", "it", "zh"]
    assert len(LANGS) == len(set(LANGS))
    for lang in LANGS:
        assert lexicon.language_name(lang), lang


def test_every_variety_carries_the_same_sections():
    shapes = {lang: sorted(_raw(lang)) for lang in LANGS}
    first = shapes[LANGS[0]]
    for lang in LANGS[1:]:
        assert shapes[lang] == first, lang


def test_every_variety_names_every_reading_state():
    states = set(decide.CEILING)
    assert len(states) == 8
    for lang in LANGS:
        assert set(lexicon.reading_labels(lang)) == states, lang


def test_every_variety_translates_the_labels_the_reply_needs():
    needed = {"status", "understood", "answer", "assurance", "notes", "math_layer", "check", "examples"}
    reference = sorted(lexicon.labels(LANGS[0]))
    for lang in LANGS:
        labels = lexicon.labels(lang)
        assert sorted(labels) == reference, lang
        assert needed <= set(labels), lang
        assert "{n}" in labels["assurance"], lang


def test_every_variety_translates_every_reason():
    reference = sorted(lexicon.reason_texts(LANGS[0]))
    assert len(reference) == 18
    for lang in LANGS:
        assert sorted(lexicon.reason_texts(lang)) == reference, lang
        for key in reference:
            assert lexicon.translate_reason(lang, key).strip(), (lang, key)


def test_every_variety_covers_the_same_kinds():
    kinds = set(canonical.KINDS)
    for lang in LANGS:
        assert set(lexicon.patterns(lang)) <= kinds, lang
        assert set(lexicon.patterns(lang)), lang


def _atoms(text):
    """The atoms of a shape, folded the way the two readers fold them when they scan a sentence."""
    return [segment.fold(atom) for _kind, atom in segment.atomize(text)]


def _consecutive(needle, hay):
    return any(hay[start:start + len(needle)] == needle for start in range(len(hay) - len(needle) + 1))


def test_every_pattern_is_announced_by_a_verb_of_its_own_kind():
    # A shape that promises one operation must carry the word that names it: otherwise a reader
    # could match a frame whose operation it never heard announced. The comparison runs over the
    # folded atom sequences of the readers, because a written accent or a joined article is not a
    # different word, and a verb phrase may span several atoms.
    for lang in LANGS:
        verbs = lexicon.verbs(lang)
        for kind, shapes in lexicon.patterns(lang).items():
            announced = [_atoms(phrase) for phrase, kinds in verbs.items() if kind in kinds]
            assert announced, "%s has no verb for %s" % (lang, kind)
            for shape in shapes:
                atoms = _atoms(shape)
                assert any(_consecutive(phrase, atoms) for phrase in announced), "%s %r" % (lang, shape)


def test_words_written_inside_a_pattern_are_known_words():
    # The filler of a frame may be a word of the variety -- the Italian frame for an inverse matrix
    # writes the word for matrix itself. A reader must know it, or it reports an unknown word in a
    # sentence that this bundle is supposed to read.
    for lang in LANGS:
        index = lexicon.phrase_index(lang)
        for word in lexicon.pattern_literals(lang):
            assert word in index, "%s writes %r in a pattern but does not know it" % (lang, word)


def test_the_examples_of_each_variety_are_written_in_that_variety():
    # The example lines show what the stratum can be asked; they are data, so they stay in the
    # language under study, and they must not be copies of another variety.
    for lang in LANGS:
        examples = _raw(lang).get("examples") or []
        assert examples, lang
        for other in LANGS:
            if other == lang:
                continue
            assert set(examples).isdisjoint(set(_raw(other).get("examples") or [])), (lang, other)


def test_the_reasons_of_all_varieties_ask_for_the_same_fillers():
    # A reason template may name what it wants filled in -- the word, the kind, the detail -- but
    # those names are identifiers, not prose: every variety must ask for the same ones, so that the
    # renderer has one set of fields to supply and never leaves a brace in front of a reader.
    import re
    pattern = re.compile(r"\{([a-z_]+)\}")
    for key in lexicon.reason_texts(LANGS[0]):
        reference = set(pattern.findall(lexicon.reason_texts(LANGS[0])[key]))
        for lang in LANGS[1:]:
            assert set(pattern.findall(lexicon.reason_texts(lang)[key])) == reference, (lang, key)