Repository navigation
Fix a dropped morpheme ID when a zero-width rule wraps one ending at the same node #500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
johnml1135
wants to merge
2
commits into
fix-506-directional-boundaries
Choose a base branch
from
fix/signature-drops-id-under-identity-rule
base: fix-506-directional-boundaries
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
tests/SIL.Machine.Morphology.HermitCrab.Tests/IdentityRuleSignatureMorphDropTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| using NUnit.Framework; | ||
|
|
||
| namespace SIL.Machine.Morphology.HermitCrab; | ||
|
|
||
| /// <summary> | ||
| /// Regression coverage for a dropped morpheme ID when a zero-width rule wraps one ending at the | ||
| /// same shape node. | ||
| /// </summary> | ||
| [TestFixture] | ||
| public class IdentityRuleSignatureMorphDropTests | ||
| { | ||
| private const string GrammarXml = """ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: F5. |
||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!DOCTYPE HermitCrabInput SYSTEM "HermitCrabInput.dtd"> | ||
| <HermitCrabInput> | ||
| <Language> | ||
| <Name>IdentityRuleSignatureMorphDropTest</Name> | ||
| <PartsOfSpeech> | ||
| <PartOfSpeech id="posX"><Name>x</Name></PartOfSpeech> | ||
| </PartsOfSpeech> | ||
| <HeadFeatures> | ||
| <SymbolicFeature id="featGrp"> | ||
| <Name>grp</Name> | ||
| <Symbols> | ||
| <Symbol id="symP">p</Symbol> | ||
| <Symbol id="symQ">q</Symbol> | ||
| </Symbols> | ||
| </SymbolicFeature> | ||
| </HeadFeatures> | ||
| <CharacterDefinitionTable id="tbl"> | ||
| <Name>Main</Name> | ||
| <SegmentDefinitions> | ||
| <SegmentDefinition id="cF"><Representations><Representation>f</Representation></Representations></SegmentDefinition> | ||
| <SegmentDefinition id="cE"><Representations><Representation>e</Representation></Representations></SegmentDefinition> | ||
| <SegmentDefinition id="cN"><Representations><Representation>n</Representation></Representations></SegmentDefinition> | ||
| <SegmentDefinition id="cO"><Representations><Representation>o</Representation></Representations></SegmentDefinition> | ||
| <SegmentDefinition id="cS"><Representations><Representation>s</Representation></Representations></SegmentDefinition> | ||
| </SegmentDefinitions> | ||
| </CharacterDefinitionTable> | ||
| <NaturalClasses> | ||
| <FeatureNaturalClass id="ncAny"><Name>Any</Name></FeatureNaturalClass> | ||
| </NaturalClasses> | ||
| <Strata> | ||
| <Stratum characterDefinitionTable="tbl" morphologicalRules="mrPtoQ mrQtoP mrIdentity"> | ||
| <Name>Main</Name> | ||
| <MorphologicalRuleDefinitions> | ||
| <MorphologicalRule id="mrPtoQ"> | ||
| <Name>ptoq</Name> | ||
| <MorphologicalSubrules> | ||
| <MorphologicalSubrule id="subPtoQ"> | ||
| <MorphologicalInput><PhoneticSequence id="stemPtoQ"><OptionalSegmentSequence min="1" max="-1"><SimpleContext naturalClass="ncAny" /></OptionalSegmentSequence></PhoneticSequence></MorphologicalInput> | ||
| <MorphologicalOutput><CopyFromInput index="stemPtoQ" /><InsertSegments><PhoneticShape>o</PhoneticShape></InsertSegments></MorphologicalOutput> | ||
| </MorphologicalSubrule> | ||
| </MorphologicalSubrules> | ||
| <OutputHeadFeatures><FeatureValue feature="featGrp" symbolValues="symQ" /></OutputHeadFeatures> | ||
| <RequiredHeadFeatures><FeatureValue feature="featGrp" symbolValues="symP" /></RequiredHeadFeatures> | ||
| <MorphemeId>PTOQ</MorphemeId> | ||
| <Gloss>PTOQ</Gloss> | ||
| </MorphologicalRule> | ||
| <MorphologicalRule id="mrQtoP"> | ||
| <Name>qtop</Name> | ||
| <MorphologicalSubrules> | ||
| <MorphologicalSubrule id="subQtoP"> | ||
| <MorphologicalInput><PhoneticSequence id="stemQtoP"><OptionalSegmentSequence min="1" max="-1"><SimpleContext naturalClass="ncAny" /></OptionalSegmentSequence></PhoneticSequence></MorphologicalInput> | ||
| <MorphologicalOutput><CopyFromInput index="stemQtoP" /><InsertSegments><PhoneticShape>s</PhoneticShape></InsertSegments></MorphologicalOutput> | ||
| </MorphologicalSubrule> | ||
| </MorphologicalSubrules> | ||
| <OutputHeadFeatures><FeatureValue feature="featGrp" symbolValues="symP" /></OutputHeadFeatures> | ||
| <RequiredHeadFeatures><FeatureValue feature="featGrp" symbolValues="symQ" /></RequiredHeadFeatures> | ||
| <MorphemeId>QTOP</MorphemeId> | ||
| <Gloss>QTOP</Gloss> | ||
| </MorphologicalRule> | ||
| <MorphologicalRule id="mrIdentity"> | ||
| <Name>identity</Name> | ||
| <MorphologicalSubrules> | ||
| <MorphologicalSubrule id="subIdentity"> | ||
| <MorphologicalInput><PhoneticSequence id="stemIdentity"><OptionalSegmentSequence min="1" max="-1"><SimpleContext naturalClass="ncAny" /></OptionalSegmentSequence></PhoneticSequence></MorphologicalInput> | ||
| <MorphologicalOutput><CopyFromInput index="stemIdentity" /></MorphologicalOutput> | ||
| </MorphologicalSubrule> | ||
| </MorphologicalSubrules> | ||
| <OutputHeadFeatures><FeatureValue feature="featGrp" symbolValues="symQ" /></OutputHeadFeatures> | ||
| <RequiredHeadFeatures><FeatureValue feature="featGrp" symbolValues="symP" /></RequiredHeadFeatures> | ||
| <MorphemeId>IDENT</MorphemeId> | ||
| <Gloss>IDENT</Gloss> | ||
| </MorphologicalRule> | ||
| </MorphologicalRuleDefinitions> | ||
| <LexicalEntries> | ||
| <LexicalEntry id="eFen" partOfSpeech="posX"> | ||
| <Allomorphs><Allomorph id="aFen"><PhoneticShape>fen</PhoneticShape></Allomorph></Allomorphs> | ||
| <MorphemeId>FEN</MorphemeId> | ||
| <Gloss>fen</Gloss> | ||
| </LexicalEntry> | ||
| </LexicalEntries> | ||
| </Stratum> | ||
| </Strata> | ||
| </Language> | ||
| </HermitCrabInput> | ||
| """; | ||
|
|
||
| private static string WriteTempGrammar() | ||
| { | ||
| string path = Path.Combine( | ||
| Path.GetTempPath(), | ||
| "hc-identity-signature-drop-" + Guid.NewGuid().ToString("N") + ".xml" | ||
| ); | ||
| File.WriteAllText(path, GrammarXml); | ||
| return path; | ||
| } | ||
|
|
||
| private static string Signature(IEnumerable<Word> results) | ||
| { | ||
| List<string> signatures = results | ||
| .Select(w => | ||
| string.Join("+", w.AllomorphsInMorphOrder.Select(a => a.Morpheme.Id)) | ||
| + "|" | ||
| + w.Shape.ToRegexString(w.Stratum.CharacterDefinitionTable, true) | ||
| ) | ||
| .OrderBy(s => s, StringComparer.Ordinal) | ||
| .ToList(); | ||
| return signatures.Count == 0 ? "-" : string.Join(";", signatures); | ||
| } | ||
|
|
||
| private static string ParseWithFreshMorpher(Language language, string word) | ||
| { | ||
| var morpher = new Morpher(new TraceManager(), language, maxDegreeOfParallelism: 1); | ||
| return Signature(morpher.ParseWord(word, out _, false).ToList()); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Fen_BareRootControl_IdentityWrapsRootDirectly_StableAndCorrect() | ||
| { | ||
| // Control: mrIdentity wraps the root directly, so no two rules share an affix boundary. | ||
| string path = WriteTempGrammar(); | ||
| try | ||
| { | ||
| Language language = XmlLanguageLoader.Load(path); | ||
| for (int trial = 0; trial < 10; trial++) | ||
| Assert.That(ParseWithFreshMorpher(language, "fen"), Is.EqualTo("FEN+IDENT|fen;FEN|fen")); | ||
| } | ||
| finally | ||
| { | ||
| File.Delete(path); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void Feno_NoIdentityRuleApplies_StableAndCorrect() | ||
| { | ||
| // Control: after mrPtoQ alone the value is q, so mrIdentity (requires p) never applies. | ||
| string path = WriteTempGrammar(); | ||
| try | ||
| { | ||
| Language language = XmlLanguageLoader.Load(path); | ||
| for (int trial = 0; trial < 10; trial++) | ||
| Assert.That(ParseWithFreshMorpher(language, "feno"), Is.EqualTo("FEN+PTOQ|feno")); | ||
| } | ||
| finally | ||
| { | ||
| File.Delete(path); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void Fenos_IdentityWrapsOutermost_EveryFreshLoadHasCorrectSignature() | ||
| { | ||
| string path = WriteTempGrammar(); | ||
| try | ||
| { | ||
| const string CorrectSig = "FEN+PTOQ+QTOP+IDENT|fenos;FEN+PTOQ+QTOP|fenos"; | ||
| for (int load = 1; load <= 20; load++) | ||
| { | ||
| Language language = XmlLanguageLoader.Load(path); | ||
| string signature = ParseWithFreshMorpher(language, "fenos"); | ||
| Assert.That(signature, Is.EqualTo(CorrectSig), $"Fresh load {load} produced {signature}"); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| File.Delete(path); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.