Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/SIL.Machine.Morphology.HermitCrab/AnalysisStratumRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ internal IEnumerable<Word> Apply(Word input, ref int alternativeCount)

_prulesRule.Apply(input);
input.Freeze();
IDictionary<Shape, Word> shapeWord = null;
IDictionary<AnalysisStateKey, Word> wordCache = null;
// Don't merge if tracing because it messes up the tracing.
bool mergeEquivalentAnalyses = _morpher.MergeEquivalentAnalyses && !_morpher.TraceManager.IsTracing;
if (mergeEquivalentAnalyses)
shapeWord = new Dictionary<Shape, Word>(FreezableEqualityComparer<Shape>.Default);
wordCache = new Dictionary<AnalysisStateKey, Word>();

// AnalysisStratumRule.Apply should cover the inverse of SynthesisStratumRule.Apply.
IEnumerable<Word> mruleOutWords = ApplyTemplates(input).Concat(ApplyMorphologicalRules(input));
Expand All @@ -152,14 +152,13 @@ internal IEnumerable<Word> Apply(Word input, ref int alternativeCount)
mruleOutWord.Source = origInput;
if (mergeEquivalentAnalyses)
{
Shape shape = mruleOutWord.Shape;
Word canonicalWord;
if (shapeWord.TryGetValue(shape, out canonicalWord))
var key = AnalysisStateKey.PinAndKey(mruleOutWord);
if (wordCache.TryGetValue(key, out Word canonicalWord))
{
canonicalWord.Alternatives.Add(mruleOutWord);
continue;
}
shapeWord[shape] = mruleOutWord;
wordCache[key] = mruleOutWord;
}
output.Add(mruleOutWord);
if (_morpher.TraceManager.IsTracing)
Expand Down
Loading