Repository navigation
Match each affix rule once per shape in HermitCrab analysis - #654
johnml1135 wants to merge 1 commit into
Conversation
Matching an affix or realizational rule allomorph reads only the input shape, yet the analysis search matched it again for every path that reached the same shape. Each parse now keeps a table keyed by (rule, allomorph, frozen shape) holding the output shapes, and every path still runs its own gates and builds its own output words from the stored shape. Compounding rules are not stored, because their match also reads the non-head root. Refs #653 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #654 +/- ##
==========================================
+ Coverage 75.64% 75.74% +0.09%
==========================================
Files 456 457 +1
Lines 38985 39039 +54
Branches 5435 5442 +7
==========================================
+ Hits 29492 29570 +78
+ Misses 8347 8326 -21
+ Partials 1146 1143 -3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| _finalTemplateState = word._finalTemplateState; | ||
| _isPartial = word._isPartial; | ||
| CurrentTrace = word.CurrentTrace; | ||
| MatchTable = word.MatchTable; |
There was a problem hiding this comment.
Minor: F1. Parse results keep the whole table alive, so it is not "dropped with" the parse as the description says. This line copies MatchTable into every clone, and lexical lookup builds results with input.Clone() (Morpher.cs:580, :652); Source (Word.cs:85, never cleared) also reaches back to the input word. So every Word that ParseWords returns holds every row its parse stored, and a caller keeping results over a corpus keeps all of them. Since ParseWord is eager and synthesis never reads the table, clearing its rows right after _analysisRule.Apply(input) would release them.
| return Compute(rule, patternRule, input, matched); | ||
|
|
||
| var key = new Key(rule, allomorph, input.Shape); | ||
| if (table._rows.TryGetValue(key, out Shape[] shapes)) |
There was a problem hiding this comment.
FYI: F2. Two threads can match the same (rule, allomorph, shape), so "computed once per parse" (line 13) does not hold under the default maxDegreeOfParallelism: 0. This TryGetValue and the TryAdd at line 71 are not atomic, and AnalysisAffixTemplateRule.ParallelApplySlots (:186) runs slots in parallel inside one parse. Results stay correct; only the saving and the ShapeMatched count change. GetOrAdd with a Lazy<Shape[]> would make it once; otherwise reword the doc.
|
Approve with fixes. The main issue is F1 at Findings: 0 Critical, 1 Important (F1), 1 Low (F2). Ran Not verified: how much memory the retained rows cost on a real corpus; the conformance replay and timings in the description, which I did not rerun; and whether Public API, target framework, package, and F1 new, F2 new. Reviewed at 277435b |
Quick summary
HermitCrab analysis now matches each affix or realizational rule allomorph once per shape in a parse, instead of once per path that reaches the shape. Results, their order and every trace event are unchanged; each path still runs its own gates and builds its own output words. Compounding rules and the public API are untouched.
Ports PanGloss PR #488 (sillsdev/PanGloss) to C#. Spec: #653 (comment). Refs #653 (not closing it).
Where to look
MorphologicalRules/ShapeMatchTable.cs-- the table, keyed by (grammar rule, allomorph index, frozenShapeby value). A row is the output shapes afterRemoveDuplicates; an empty row is "no match".AnalysisAffixProcessRule.ApplyandAnalysisRealizationalAffixProcessRule.Apply-- only the_rules[i].Apply(input).RemoveDuplicates()call changed. Selector, unapplication count,_outputFS, edge prefilter, Restrict/Unify, template state,MorphologicalRuleUnappliedand trace calls run before and after it exactly as before.Word.CloneWithShapebuilds each hit's word from the input and the stored shape. A later clone still copies the shape, so nothing mutates a stored one (StoredShapesAreFrozenAndNotMutatedByTheWordsBuiltFromThem).Apply, so every slot gate stays outside the row (TemplateSlotsWithRequiredAndExcludedFeatures_AreUnchanged).AnalysisCompoundingRulenever reads the table (CompoundingRules_AreNotReadFromTheTable).Wordand is copied byClone, so it is perParseWord, dropped with it, and safe undermaxDegreeOfParallelismabove 1 (ConcurrentDictionary; shapes are frozen).Deliberately not included
Morpher.ShapeMatchTableEnabled(default on) andShapeMatchedareinternal, for tests.Validation
./local_check.sh --agent-strict-- PASSED (csharpier, Release build, all tests, comment hygiene clean).dotnet test tests/SIL.Machine.Morphology.HermitCrab.Tests-- 275 passed, 0 failed (10 new).mainat 1fc36fe (has all 8convergent-*): 268 fixtures (252 conformance + 16 stress), 17,729 words, each run with and without tracing,hcbuilt from master c73ca66 versus this branch, both withmaxDegreeOfParallelism: 1. 264 fixtures byte-identical (trace text included).simultaneous-epenthesis-cascadeis a pinned crash with identical output except the source path in the stack trace.truncate-morphotacticandtruncate-morphotactic-identitygive different output from run to run on master itself (7 and 10 distinct outputs in 12 runs); over 40 runs per branch every per-word parse set from this branch also appears on master.-copy-uand-subsumptionvaried the same way earlier but agreed here. All 8convergent-*fixtures are identical.Rule-allomorph matches per parse (probe grammars,
khomophonous template suffixes; counted at_rules[i].Apply, trace events are unchanged by design):Timings are small and noisy, because
hcre-parses in a few milliseconds: 300 parses of g4 took 2.1 s before and 1.5 s after (medians of 5); g2 and g8 were within noise. On conformance fixtures,deep-optional-affix-nesting4.8 s -> 4.2 s,syncope-redup-optional-vowel-lattice3.5 s -> 2.7 s, others within noise.Decisions, and why
Paths not taken
Sharing whole subtrees by state, carrying label sets per expanded shape, and a lexicon reachability prune were rejected in the #653 comment: they change visiting order or belong to the FST prefilter.
🤖 Generated with Claude Code
This change is