Skip to content

Commit

Permalink
Try ignoring duplicate shapes to fix LT-21988
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmaxwell3 committed Nov 14, 2024
1 parent 9720b46 commit c434699
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public IEnumerable<Word> Apply(Word input)

HashSet<Word> outputSet = tempSet;
outputSet.Clear();
AnalysisStratumRule stratumRule = _rules[i] as AnalysisStratumRule;
stratumRule?.InitializeDuplicationDetection();

foreach (Word inData in inputSet)
{
Expand All @@ -41,9 +43,10 @@ public IEnumerable<Word> Apply(Word input)
}
}

stratumRule?.ClearDuplicationDetection();
tempSet = inputSet;
inputSet = outputSet;
}
}

return results;
}
Expand Down
23 changes: 23 additions & 0 deletions src/SIL.Machine.Morphology.HermitCrab/AnalysisStratumRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal class AnalysisStratumRule : IRule<Word, ShapeNode>
private readonly IRule<Word, ShapeNode> _templatesRule;
private readonly Stratum _stratum;
private readonly Morpher _morpher;
private HashSet<Shape> _shapeSet;

public AnalysisStratumRule(Morpher morpher, Stratum stratum)
{
Expand Down Expand Up @@ -58,6 +59,16 @@ public AnalysisStratumRule(Morpher morpher, Stratum stratum)
}
}

public void InitializeDuplicationDetection()
{
_shapeSet = new HashSet<Shape>(FreezableEqualityComparer<Shape>.Default);
}

public void ClearDuplicationDetection()
{
_shapeSet = null;
}

public IEnumerable<Word> Apply(Word input)
{
if (_morpher.TraceManager.IsTracing)
Expand Down Expand Up @@ -117,6 +128,18 @@ private IEnumerable<Word> ApplyMorphologicalRules(Word input)

private IEnumerable<Word> ApplyTemplates(Word input)
{
if (_shapeSet != null)
{
// Ignore shapes that we have already tried at this level of the stratum.
// ApplyTemplates is called at the very beginning of applying the analysis stratum.
// It is also called recursively by ApplyMorphologicalRules if the rules are unordered.
// In this case, we can still use the same shapeSet since the templates and morphological
// rules continue to be applied until there are no more changes, and so
// trying this shape again won't change the results.
if (_shapeSet.Contains(input.Shape))
yield break;
_shapeSet.Add(input.Shape);
}
foreach (Word tempOutWord in _templatesRule.Apply(input).Distinct(FreezableEqualityComparer<Word>.Default))
{
switch (_stratum.MorphologicalRuleOrder)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using System.Diagnostics;

namespace SIL.Machine.Morphology.HermitCrab;

Expand Down Expand Up @@ -27,4 +28,14 @@ public void RoundTripXml()
File.Delete(TempXmlFileName);
}
}

[Test]
public void TestParseWord()
{
Language language = XmlLanguageLoader.Load("C:\\Users\\PC\\AppData\\Local\\Temp\\Mbugwe LizzieHC practice.xml");
Morpher morpher = new Morpher(new TraceManager(), language);
var output = morpher.ParseWord("wa", out _);
Debug.WriteLine(output);
}

}

0 comments on commit c434699

Please sign in to comment.