physics-stratum / tests /test_lexicon.py
tailot's picture
physics-stratum: the conversational layer over the physics topology
3a0a69b verified
Raw History Blame Contribute Delete
6.8 kB
"""The data files: six varieties must describe the same stratum, in their own words."""
import json
from pathlib import Path
import pytest
from lgphysstratum 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)