Coverage for rdgai/constraints.py: 95.12%

41 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-24 23:53 +0000

1""" 

2Constraints a category can declare on the computed change between two readings, checked by generic code. 

3 

4A file's categories may declare constraints in the TEI (``AnnotationStore.set_category_constraints``), e.g. a category 

5for added words declares ``adds-only``. When the pipeline assigns a category whose declared constraint the computed 

6change breaks, it asks the model once to reconsider; the model may keep its answer (the check cannot tell every 

7difference of spelling from one of form). rdgai itself knows nothing about any category scheme: without declared 

8constraints nothing is checked. 

9 

10Words are compared after normalization (``search.normalize``). A removed and an added word count as the same word in 

11another spelling or form if they are similar enough (``similar_words``), or are trivially respelled by the rules for 

12the file's language (``languages.trivially_respelled``). 

13""" 

14from difflib import SequenceMatcher 

15 

16from .diff import describe_change 

17from .search import normalize 

18from .languages import trivially_respelled 

19 

20CONSTRAINTS = { 

21 "adds-only": "the second reading only adds words (any word of the first reading kept, at most in another spelling)", 

22 "omits-only": "the second reading only omits words (any word it keeps, at most in another spelling)", 

23 "keeps-word-count": "both readings have the same number of words (words change form or order, none added or omitted)", 

24 "not-only-adds-or-omits": "the change is not a bare addition or omission: something is replaced or reordered", 

25 "rules-only": "assigned only by the subreading rules, never to a pair the pipeline is asked about", 

26} 

27CATEGORY_DECLARATIONS = ("rules-only",) # declarations about the category, not checks on the change 

28 

29SIMILARITY = 0.6 

30SHORT_WORD_SIMILARITY = 0.8 # a word of 3 letters or fewer must match more closely 

31SHORT_WORD = 3 

32 

33 

34def similar_words(first:str, second:str) -> bool: 

35 """ Whether two words look like spellings or forms of one word (normalized similarity, stricter for short words). """ 

36 first, second = normalize(first), normalize(second) 

37 threshold = SHORT_WORD_SIMILARITY if min(len(first), len(second)) <= SHORT_WORD else SIMILARITY 

38 return SequenceMatcher(None, first, second).ratio() >= threshold 

39 

40 

41def constraint_problem(constraint:str, active_text:str, passive_text:str, category:str="", language:str="") -> str|None: 

42 """ How the change from the active to the passive reading breaks the constraint, as a message (None if it fits). """ 

43 if constraint in CATEGORY_DECLARATIONS: 

44 return None 

45 change = describe_change(active_text, passive_text) 

46 label = f"{category} is declared '{constraint}' ({CONSTRAINTS[constraint]})" if category else f"'{constraint}' ({CONSTRAINTS[constraint]})" 

47 if constraint in ("adds-only", "omits-only"): 

48 adds = constraint == "adds-only" 

49 stray, counterpart = (change.removed, change.added) if adds else (change.added, change.removed) 

50 if change.reordered and change.kind != "reordering" and not stray: 

51 return f"{label}, but the words are also reordered" 

52 extra = [word for word in stray if not any(similar_words(word, other) for other in counterpart)] 

53 if extra: 

54 return (f"{label}, but the {'first' if adds else 'second'} reading also has {', '.join(extra)}, which is not " 

55 f"another spelling of any word {'added' if adds else 'omitted'}: reconsider which definition fits") 

56 differing = [(word, other) if adds else (other, word) for word in stray for other in counterpart 

57 if similar_words(word, other) and not trivially_respelled(word, other, language)] 

58 if differing: 

59 changed = ", ".join(f"{first} → {second}" for first, second in differing) 

60 return (f"{label}; besides the {'added' if adds else 'omitted'} words, {changed} differs: keep it only if the " 

61 f"definitions count that difference as spelling (or the apparatus marks it as a subreading of that type)") 

62 return None 

63 if constraint == "keeps-word-count": 

64 if len(change.removed) == len(change.added): 

65 return None 

66 return (f"{label}, but the readings have a different number of words " 

67 f"({', '.join(change.removed) or 'nothing'} → {', '.join(change.added) or 'nothing'}): reconsider which definition fits") 

68 if constraint == "not-only-adds-or-omits": 

69 if change.reordered or change.kind not in ("addition", "omission"): 

70 return None 

71 verb = "only adds" if change.kind == "addition" else "only omits" 

72 return (f"{label}, but the computed change shows that the second reading {verb} words " 

73 f"({', '.join(change.added or change.removed)}), with nothing replaced or reordered: reconsider which definition fits") 

74 raise ValueError(f"Unknown constraint '{constraint}'")