WolfDavid commited on
Commit
706670a
·
1 Parent(s): f7e73d2

test(02-02): add failing reading-override tests

Browse files

- test_hitoke_override, test_override_table_is_well_formed, test_apply_overrides_is_idempotent

Files changed (1) hide show
  1. tests/test_units.py +73 -0
tests/test_units.py CHANGED
@@ -288,3 +288,76 @@ def test_build_units_is_pure_over_dicts():
288
  assert build_units(ms) == units # deterministic
289
  assert build_units([]) == []
290
  assert all(type(u) is dict for u in units)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
  assert build_units(ms) == units # deterministic
289
  assert build_units([]) == []
290
  assert all(type(u) is dict for u in units)
291
+
292
+
293
+ # --- Task 3: the committed reading-override table -------------------------------------------
294
+
295
+
296
+ def test_hitoke_override():
297
+ from japanese_avatar.nlp.overrides import OVERRIDES_PATH, apply_overrides
298
+
299
+ assert OVERRIDES_PATH.name == "reading_overrides.json"
300
+ assert OVERRIDES_PATH.is_file()
301
+
302
+ units = units_of("人気のない通り")
303
+ assert surfaces(units) == ["人気", "の", "ない", "通り"]
304
+ assert units[0]["reading"] == "にんき" # what Sudachi core gives (measured)
305
+ before = [dict(u) for u in units]
306
+
307
+ replaced = apply_overrides(units)
308
+ assert replaced == 1
309
+ assert units[0]["reading"] == "ひとけ"
310
+ # Only the reading of the matched unit changes; everything else is untouched.
311
+ assert {k: v for k, v in units[0].items() if k != "reading"} == {
312
+ k: v for k, v in before[0].items() if k != "reading"
313
+ }
314
+ assert units[1:] == before[1:]
315
+
316
+ units = units_of("人気がある")
317
+ assert apply_overrides(units) == 0
318
+ assert units[0]["reading"] == "にんき"
319
+
320
+ units = units_of("この店は人気だ")
321
+ assert apply_overrides(units) == 0
322
+ assert [u["reading"] for u in units if u["surface"] == "人気"] == ["にんき"]
323
+
324
+ # 人気 at the very end of the text has no following unit: next_surface cannot match.
325
+ units = units_of("人気")
326
+ assert apply_overrides(units) == 0
327
+ assert units[0]["reading"] == "にんき"
328
+
329
+
330
+ def test_override_table_is_well_formed():
331
+ import json
332
+ import re
333
+
334
+ from japanese_avatar.nlp.overrides import OVERRIDES_PATH, load_overrides
335
+
336
+ rows = load_overrides()
337
+ assert isinstance(rows, tuple) and rows
338
+ hiragana = re.compile(r"^[ぁ-ゖー]+$")
339
+ for row in rows:
340
+ assert set(row) == {"surface", "reading_from_sudachi", "next_surface", "reading", "why"}
341
+ assert hiragana.match(row["reading"]), row
342
+ assert hiragana.match(row["reading_from_sudachi"]), row
343
+ assert row["reading"] != row["reading_from_sudachi"], row
344
+ assert row["next_surface"] is None or isinstance(row["next_surface"], str)
345
+ assert isinstance(row["why"], str) and row["why"].strip(), row
346
+ assert rows[0]["surface"] == "人気"
347
+ assert rows[0]["reading"] == "ひとけ"
348
+ assert rows[0]["next_surface"] == "の"
349
+ # The committed file is what load_overrides serves, and it is plain JSON.
350
+ raw = json.loads(OVERRIDES_PATH.read_text(encoding="utf-8"))
351
+ assert tuple(raw) == rows
352
+ assert load_overrides() is rows # cached: loaded once
353
+
354
+
355
+ def test_apply_overrides_is_idempotent():
356
+ from japanese_avatar.nlp.overrides import apply_overrides
357
+
358
+ once = units_of("人気のない通り")
359
+ assert apply_overrides(once) == 1
360
+ twice = [dict(u) for u in once]
361
+ assert apply_overrides(twice) == 0
362
+ assert twice == once
363
+ assert apply_overrides([]) == 0