"""What the two readers do, taken from the questions the corpus already carries. A question of this stratum is often several sentences long: the person states the figures and then asks about them. One reading of such a question is a reading of each of its sentences, so the tests below look at the readings the pipeline itself put in the dossier, the place where a compound reading is assembled, rather than at the output of one reader over the whole text. """ import pytest from lggeostratum import canonical, compare, corpus, pipeline, read_a, read_b ITEMS = {item["id"]: item for item in corpus.load()} AGREED = [item_id for item_id, item in ITEMS.items() if (item.get("expect") or {}).get("state") == "AGREED"] SINGLE = [item_id for item_id, item in ITEMS.items() if (item.get("expect") or {}).get("state") == "SINGLE"] NEGATIVE = [item_id for item_id, item in ITEMS.items() if (item.get("expect") or {}).get("state") == "NOT COVERED"] PASSTHROUGH = [item_id for item_id, item in ITEMS.items() if (item.get("expect") or {}).get("state") == "PASSTHROUGH"] _SESSIONS = {} def _item(item_id): item = ITEMS[item_id] return str(item["lang"]), str(item["question"]) def _readings(item_id): """The two readings of the item, as the reading layer assembled them, without a geometry session.""" if item_id not in _SESSIONS: lang, question = _item(item_id) _SESSIONS[item_id] = pipeline.ask(question, lang, call_geometry=False)["dossier"] dossier = _SESSIONS[item_id] return dossier.get("reading_a") or {}, dossier.get("reading_b") or {} def test_the_readers_are_read_from_the_data_not_from_a_fork(): # Two readers that share nothing but the data files are the whole point: one matches the shapes # written in the frames, the other follows the joints of the geometry language. If they became the # same procedure, the stratum would be reading a question twice with one pair of eyes. assert read_a.read.__module__ != read_b.read.__module__ import inspect assert "gap_markers" not in inspect.getsource(read_a.read) assert "compile_pattern" not in inspect.getsource(read_b.read) @pytest.mark.parametrize("item_id", AGREED) def test_both_readers_reach_the_same_sentence(item_id): a, b = _readings(item_id) assert a.get("status") == "READ", (item_id, a.get("notes")) assert b.get("status") == "READ", (item_id, b.get("notes")) assert a.get("kind") == b.get("kind"), (item_id, a.get("kind"), b.get("kind")) assert compare.equivalence(a, b)["same"] is True, (item_id, compare.equivalence(a, b).get("why")) expected = canonical.tidy(str((ITEMS[item_id].get("expect") or {}).get("sentence", ""))) if expected: assert canonical.tidy(str(a.get("math", ""))) == expected, item_id @pytest.mark.parametrize("item_id", AGREED) def test_a_reader_keeps_every_number_the_question_wrote(item_id): reading, _other = _readings(item_id) _lang, question = _item(item_id) assert compare.keeps(reading, question)["ok"] is True, (item_id, compare.keeps(reading, question)) @pytest.mark.parametrize("item_id", SINGLE) def test_one_reader_may_abstain_where_the_other_reads(item_id): # The varieties that put the joints after the slot, rather than before it, are read by the strict # reader and not by the frame reader. That disagreement is reported, never hidden, and it is what # keeps the assurance of those questions at one. a, b = _readings(item_id) assert a.get("status") == "READ", (item_id, a.get("notes")) assert b.get("status") != "READ", (item_id, b.get("notes")) assert compare.equivalence(a, b)["same"] is False, item_id @pytest.mark.parametrize("item_id", NEGATIVE) def test_a_question_this_variety_does_not_write_is_not_read(item_id): for reading in _readings(item_id): assert reading.get("status") != "READ", (item_id, reading.get("kind"), reading.get("slots")) assert not reading.get("math"), (item_id, reading.get("math")) @pytest.mark.parametrize("item_id", PASSTHROUGH) def test_the_geometry_language_is_handed_on_without_being_read(item_id): _lang, question = _item(item_id) direct = canonical.direct(question) assert direct is not None, item_id assert canonical.tidy(str(direct.get("math", ""))) == canonical.tidy( str((ITEMS[item_id].get("expect") or {}).get("sentence", ""))), item_id