|
6 | 6 | import re |
7 | 7 | import sys |
8 | 8 | import xml.etree.ElementTree as ET |
| 9 | +from typing import Any |
9 | 10 |
|
10 | 11 | import pytest |
11 | 12 |
|
12 | 13 | sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) |
13 | 14 |
|
14 | 15 | from ai.chat_tools import ParseChatTools # noqa: E402 (import first; avoids export_tools load cycle) |
| 16 | +from ai.workflow_tools import WorkflowTools # noqa: E402 |
15 | 17 |
|
16 | 18 |
|
17 | 19 | def _seed_workspace(root: pathlib.Path) -> None: |
@@ -53,6 +55,73 @@ def write(speaker: str, concept_id: str, ipa: str) -> None: |
53 | 55 | write("Spk3", "150", "kalan") # tagged under the duplicate JBIL id |
54 | 56 |
|
55 | 57 |
|
| 58 | +def _write_annotation(root: pathlib.Path, speaker: str, intervals: list[dict[str, Any]]) -> None: |
| 59 | + ann = root / "annotations" |
| 60 | + ann.mkdir(exist_ok=True) |
| 61 | + concept_intervals = [] |
| 62 | + ipa_intervals = [] |
| 63 | + for index, item in enumerate(intervals): |
| 64 | + start = float(index) |
| 65 | + end = start + 0.5 |
| 66 | + concept_intervals.append( |
| 67 | + {"text": item.get("label", ""), "concept_id": item["concept_id"], "start": start, "end": end} |
| 68 | + ) |
| 69 | + ipa_intervals.append({"text": item["ipa"], "start": start, "end": end}) |
| 70 | + (ann / f"{speaker}.parse.json").write_text( |
| 71 | + json.dumps({"speaker": speaker, "tiers": {"concept": {"intervals": concept_intervals}, "ipa": {"intervals": ipa_intervals}}}), |
| 72 | + encoding="utf-8", |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +def _seed_roundtrip_workspace(root: pathlib.Path) -> None: |
| 77 | + (root / "concepts.csv").write_text( |
| 78 | + "id,concept_en,source_survey,source_item\n" |
| 79 | + "101,hand,KLQ,1.1\n" |
| 80 | + "201,hand,JBIL,101\n" |
| 81 | + "102,foot,KLQ,1.2\n" |
| 82 | + "202,foot,JBIL,102\n" |
| 83 | + "103,salt,KLQ,1.3\n" |
| 84 | + "203,salt,JBIL,103\n", |
| 85 | + encoding="utf-8", |
| 86 | + ) |
| 87 | + groups = {"A": ["Spk1", "Spk2"], "B": ["Spk3"]} |
| 88 | + (root / "parse-enrichments.json").write_text( |
| 89 | + json.dumps({ |
| 90 | + "manual_overrides": { |
| 91 | + "cognate_sets": { |
| 92 | + "101": groups, |
| 93 | + "201": dict(groups), |
| 94 | + "102": {"A": ["Spk1"], "B": ["Spk2", "Spk3"]}, |
| 95 | + "202": {"A": ["Spk1"], "B": ["Spk2", "Spk3"]}, |
| 96 | + # Disagreement safety: this overlapping gloss must stay split. |
| 97 | + "103": {"A": ["Spk1", "Spk2"], "B": ["Spk3"]}, |
| 98 | + "203": {"A": ["Spk1"], "B": ["Spk2", "Spk3"]}, |
| 99 | + }, |
| 100 | + # Spk1 has both survey forms for hand; the explicit canonical |
| 101 | + # choice should select row 201 rather than the earlier 101 form. |
| 102 | + "canonical_lexemes": {"bundle:hand": {"Spk1": {"csv_row_id": "201", "source": "manual"}}}, |
| 103 | + } |
| 104 | + }), |
| 105 | + encoding="utf-8", |
| 106 | + ) |
| 107 | + (root / "project.json").write_text( |
| 108 | + json.dumps({"speakers": {"Spk1": {}, "Spk2": {}, "Spk3": {}}}), encoding="utf-8" |
| 109 | + ) |
| 110 | + _write_annotation( |
| 111 | + root, |
| 112 | + "Spk1", |
| 113 | + [ |
| 114 | + {"concept_id": "101", "label": "hand", "ipa": "old-hand"}, |
| 115 | + {"concept_id": "201", "label": "hand", "ipa": "chosen-hand"}, |
| 116 | + {"concept_id": "102", "label": "foot", "ipa": "foot-one"}, |
| 117 | + {"concept_id": "103", "label": "salt", "ipa": "salt-one"}, |
| 118 | + {"concept_id": "203", "label": "salt", "ipa": "salt-two"}, |
| 119 | + ], |
| 120 | + ) |
| 121 | + _write_annotation(root, "Spk2", [{"concept_id": "201", "label": "hand", "ipa": "hand-two"}, {"concept_id": "202", "label": "foot", "ipa": "foot-two"}]) |
| 122 | + _write_annotation(root, "Spk3", [{"concept_id": "101", "label": "hand", "ipa": "hand-three"}, {"concept_id": "202", "label": "foot", "ipa": "foot-three"}]) |
| 123 | + |
| 124 | + |
56 | 125 | def test_export_nexus_consolidates_and_tag_filters(tmp_path: pathlib.Path) -> None: |
57 | 126 | _seed_workspace(tmp_path) |
58 | 127 | tools = ParseChatTools(project_root=tmp_path) |
@@ -115,3 +184,35 @@ def test_export_beast2_xml_rejects_bad_chain_length(tmp_path: pathlib.Path) -> N |
115 | 184 | tools = ParseChatTools(project_root=tmp_path) |
116 | 185 | with pytest.raises(Exception): |
117 | 186 | tools._tool_export_beast2_xml({"consolidate": True, "chainLength": 0, "dryRun": True}) |
| 187 | + |
| 188 | + |
| 189 | +def test_complete_export_consolidate_roundtrip_collapses_n_not_2n(tmp_path: pathlib.Path) -> None: |
| 190 | + _seed_roundtrip_workspace(tmp_path) |
| 191 | + workflow = WorkflowTools(project_root=tmp_path) |
| 192 | + |
| 193 | + payload = workflow.execute( |
| 194 | + "export_complete_lingpy_dataset", |
| 195 | + {"with_contact_lexemes": False, "consolidate": True, "outputDir": "exports/roundtrip", "dryRun": False}, |
| 196 | + )["result"] |
| 197 | + |
| 198 | + nexus_text = (tmp_path / "exports" / "roundtrip" / "dataset.nex").read_text(encoding="utf-8") |
| 199 | + wordlist_text = (tmp_path / "exports" / "roundtrip" / "wordlist.tsv").read_text(encoding="utf-8") |
| 200 | + nchar = int(re.search(r"NCHAR=(\d+)", nexus_text).group(1)) |
| 201 | + concept_values = [line.split("\t")[1] for line in wordlist_text.splitlines()[1:]] |
| 202 | + hand_rows = [line.split("\t") for line in wordlist_text.splitlines()[1:] if line.split("\t")[1] == "hand"] |
| 203 | + |
| 204 | + assert payload["consolidated"] is True |
| 205 | + assert payload["stages"][0]["payload"]["consolidation"]["collapsed_groups"] == 2 |
| 206 | + assert payload["stages"][0]["payload"]["consolidation"]["concept_count"] == 4 |
| 207 | + assert payload["stages"][0]["payload"]["consolidation"]["needs_recluster_groups"] == 1 |
| 208 | + assert any("kept separate" in warning for warning in payload["warnings"]) |
| 209 | + # N=2 safe overlapping meanings (hand + foot) produce N consolidated concept |
| 210 | + # blocks, not 2N per-survey duplicates; disagreeing salt remains split. |
| 211 | + assert "hand#" not in nexus_text |
| 212 | + assert "foot#" not in nexus_text |
| 213 | + assert "salt#103" in nexus_text and "salt#203" in nexus_text |
| 214 | + assert nchar == 8 # hand(2) + foot(2) + safe split salt#103(2) + salt#203(2) |
| 215 | + assert concept_values.count("hand") == 3 |
| 216 | + assert len({row[2] for row in hand_rows}) == len(hand_rows) |
| 217 | + assert any(row[3] == "chosen-hand" for row in hand_rows) |
| 218 | + assert all(row[3] != "old-hand" for row in hand_rows) |
0 commit comments