File size: 6,809 Bytes
3a0a69b
 
 
 
 
 
 
cda539d
3a0a69b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""The data files: six varieties must describe the same stratum, in their own words."""

import json
from pathlib import Path

import pytest

from lingologos_physstratum import canonical, compare, 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", "physics_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():
    # The reason keys are the vocabulary of the abstentions: a key without a text in one variety would
    # leave that variety answering part of an abstention in the language of the bundle. The keys are
    # written out here rather than counted, so a key dropped from a data file shows up as a missing
    # translation instead of as a changed number.
    expected = {
        # what a reader could not do with the words it was given
        "unknown_word", "no_verb", "two_verbs", "no_pattern", "no_frame", "multiple", "markers",
        "bad_number", "bad_unit", "bad_formula", "bad_symbol", "unbalanced",
        # what the checking of the two readings, and the physics layer, found
        "disagree", "literal_count", "no_math", "physics_unknown", "referee", "boundary", "other",
    }
    reference = sorted(lexicon.reason_texts(LANGS[0]))
    assert set(reference) == expected, reference
    assert len(reference) == len(expected)
    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 a dimensional
    # analysis writes the word for analysis 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_every_unit_and_quantity_a_variety_names_fits_a_sentence_of_the_physics_language():
    # A lexicon may only point at a word the physics language can carry: a spelling this fragment
    # does not have would make a reader produce a sentence the layer stops on, and the fault would
    # look like a coverage gap instead of a mistake in a data file. The check is not made against a
    # list kept here, but with the parser of the physics bundle itself, which owns that language: a
    # unit is put in a convert sentence, a quantity in a dimensional analysis one.
    for lang in LANGS:
        for phrase, token in lexicon.tokens(lang).items():
            if token in canonical.PHYSICS_SYMBOLS and token not in canonical.PHYSICS_UNITS:
                sentence = "pi %s from mass, time" % token
            else:
                sentence = "convert 3 %s to m" % token
            assert compare.formalize(sentence) is not None, (lang, " ".join(phrase), sentence)


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)