Repository navigation
Add hc-whynot, a standalone why-not tool for the parser conformance suite - #651
johnml1135 wants to merge 1 commit into
Conversation
…an intended analysis, with whynot.v1 JSON Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #651 +/- ##
==========================================
+ Coverage 75.53% 75.75% +0.21%
==========================================
Files 455 463 +8
Lines 38933 39633 +700
Branches 5428 5527 +99
==========================================
+ Hits 29407 30022 +615
- Misses 8376 8433 +57
- Partials 1150 1178 +28 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| if (!TryCompile(language, out WhyNotSession session)) | ||
| return 1; | ||
| using var output = new StreamWriter(outputPath, false, new UTF8Encoding(false)); | ||
| foreach (string line in File.ReadLines(queriesPath).Where(l => l.Trim().Length > 0)) |
There was a problem hiding this comment.
Minor: F1. A missing queries file wipes the output file and then crashes: the StreamWriter on line 94 truncates out.jsonl before File.ReadLines throws FileNotFoundException, which nothing catches. If queries and output are the same path, the queries are erased before they are read. Open the queries file first, and return a documented exit code on IOException/UnauthorizedAccessException.
| var expected = new List<ExpectedMorph>(); | ||
| foreach ((string morpheme, string allomorph) in expect) | ||
| { | ||
| int? index = allomorph == null ? null : ids.AllomorphIndex(morpheme, allomorph); |
There was a problem hiding this comment.
Minor: F2. One bad query line crashes the whole batch, even though batch mode promises bad queries become unknown rows. A query whose expect entry has "morpheme": null and an allomorph passes a null key to AllomorphIndex, so Dictionary.TryGetValue(null) throws ArgumentNullException outside the try in Answer, and the filter in AnswerLine does not catch it. Reject null morpheme ids while parsing.
| ids = GrammarIds.Load(grammar, language); | ||
| return true; | ||
| } | ||
| catch (Exception e) |
There was a problem hiding this comment.
Minor: F3. Common grammar errors crash the tool instead of printing cannot load grammar and exiting 1 as the usage text says. XmlLanguageLoader.Load also throws InvalidShapeException (derives from Exception), KeyNotFoundException for a dangling stem-name or class id, and FormatException from int.Parse(multipleApplication); none pass this filter. Catch non-fatal exceptions here, as HermitCrab.Tool/Program.cs:81 does.
| foreach (Morpheme morpheme in morphemes.Distinct()) | ||
| { | ||
| string id = MorphemeIds.Of(morpheme); | ||
| counts[id] = counts.ContainsKey(id) ? -1 : morpheme.AllomorphCount; |
There was a problem hiding this comment.
Minor: F4. A valid grammar crashes GrammarIds.Load: MorphemeId and Gloss are both optional in HermitCrabInput.dtd:280, so for a morpheme with neither, MorphemeIds.Of returns null and counts[id] throws ArgumentNullException, which TryLoad does not catch. Skip morphemes with a null id.
| partners = others; | ||
| string verb = type == ConstraintType.Exclude ? "excludes" : "requires"; | ||
| string plain = | ||
| $"{Label(culprit)} {verb} {string.Join(" or ", others.Select(Label))} ({adjacency}) in this word."; |
There was a problem hiding this comment.
Minor: F5. The explanation states the opposite logic for a rule with several partners. MorphCoOccurrenceRule.CoOccurs checks that all partners are present, so Require {X, Y} fails when the word has X but not Y, yet the text says "requires X or Y", which the word already satisfies. Join with " and ", for example "requires all of X and Y" or "excludes the combination X and Y".
| public static IReadOnlyList<MorphRef> MorphsOf(Word word) | ||
| { | ||
| return word | ||
| .AllomorphsInMorphOrder.Select(a => new MorphRef(MorphemeIds.Of(a.Morpheme), a.Index, a.Morpheme.Gloss)) |
There was a problem hiding this comment.
Minor: F6. A word with an affix applied twice gets the wrong verdict: AllomorphsInMorphOrder ends in .Distinct() (Word.cs:127), so two applications collapse to one morph, and Matches compares counts. With multipleApplication=2, expect [root, X, X] never matches and expect [root, X] is reported accepted. Build the list from word.Morphs through GetAllomorph. Unverified: from reading the code, not reproduced.
| } | ||
|
|
||
| var stemNameCounts = new Dictionary<string, int>(StringComparer.Ordinal); | ||
| foreach (XElement stemName in doc.Descendants().Where(e => e.Attribute("partsOfSpeech") != null && IsLive(e))) |
There was a problem hiding this comment.
FYI: F7. Inactive stem names are left out of the duplicate check, but XmlLanguageLoader.cs:280 loads every StemName whatever its isActive. If active snA and inactive snB are both named first, a failure caused by snB reports snA. Count stem names the way the loader does.
| Array.Empty<MorphRef>(), | ||
| null, | ||
| null, | ||
| new[] { new FeatureMismatch(feature.ID, "specified", actual?.ToString()) } |
There was a problem hiding this comment.
FYI: F8. This feature path is the bare feature.ID, while FeatureStructDiff writes every other path as head/... or foot/.... A consumer cannot match tense here against head/tense from a stem-name diff on the same feature.
|
|
||
| public class WhyNotTests | ||
| { | ||
| private sealed class Grammar |
There was a problem hiding this comment.
FYI: F9. AGENTS.md says the HermitCrab configuration "has no Grammar type - prefer Language or Stratum", but this test helper is a class named Grammar. Rename it, for example to LanguageFixture.
Public API, target framework, package: none verified changed in the published libraries. The PR adds a new executable project ( Findings: F1 new, F2 new, F3 new, F4 new, F5 new, F6 new (unverified), F7 new, F8 new, F9 new. Reviewed at 48c7dac |
Start here:
src/SIL.Machine.Morphology.HermitCrab.WhyNot/addshc-whynot, a standalone console tool that answers "why does HermitCrab reject this intended analysis?" for the why-not rows of the FieldWorks parser conformance suite (fieldworks-parser-conformance PR #51, PROTOCOL.md section 11,schema/whynot.schema.json). It uses only HermitCrab's public API and makes no HermitCrab source change.What it does.
hc-whynot why-not <grammar.xml> <queries.jsonl> <output.jsonl>reads one query per line (word plus the intended morphs, by morpheme id and allomorph id) and writes one answer per line. A single-word form,hc-whynot <grammar.xml> <word> --expect m[:allo],..., is there for humans. A capturingITraceManagerrecords the allomorph and failure object of eachFailedevent, which the stockTraceManagerdrops. The first failure matching the intended morphs is classified into the suite's kinds (adhoc_prohibition,stem_name,environment_unmet,feature_clash) with the HC reason and morphs. Output schema iswhynot.v1, engine-neutral and compatible withpangloss.whynot.v1. Ids are the grammar.xml ids, recovered by cross-checking the XML against the loadedLanguage; FieldWorks and internal GUIDs are never written. A failure that cannot fill a schema-required field (for example an unmapped allomorph id) becomesresult: unknownwith a stop reason and a note, never an invalid row. A bad query yields anunknownrow, not a crash.Why standalone. The existing
hcTool is an interactive REPL that loads one grammar through-i; the suite contract is a one-shot batch command. Folding it in would also touch the Tool'sProgram, which #615 (hc-runner) is already editing.HC limits found (no HC change made).
environment.index:Allomorph.Environmentsis an unordered set and the check fails only when every alternative fails.MorphologicalRuleNotApplied, not as per-derivationFailedevents, so they report asother_gateorunknown.headandfootin feature paths.unknown.Evidence
WhyNotTests: 16 tests (adhoc, stem name, environment, feature clash, ids, unknown rows, CLI batch on a real grammar.xml copied from an fpc fixture). Full HermitCrab test project: 250 passed, 0 failed.csharpier check .andscripts/comment-hygiene.ps1clean.Machine.slngains 7 lines only.adhoc-prohibition-affix-environment-copiesfixture validated againstwhynot.schema.json.local_check.sh --agent-strictwas not run to completion: cmake is missing, so the SentencePiece step fails.Next: wire the suite runner to
hc-whynotonce fpc PR #51 lands.🤖 Generated with Claude Code
This change is