private void WithinEditDistanceDepthFirst( string word, string state, List <Word> words, int depth, int maxEdits, IDistanceResolver distanceResolver, bool stop = false) { var reachedMin = maxEdits == 0 || depth >= word.Length - 1 - maxEdits; var reachedDepth = depth >= word.Length - 1; var reachedMax = depth >= word.Length + maxEdits; if (!reachedMax && !stop) { string test; if (depth == state.Length) { test = state + Value; } else { test = state.ReplaceOrAppend(depth, Value); } if (reachedMin) { if (distanceResolver.IsValid(Value, depth)) { if (EndOfWord) { if (distanceResolver.GetDistance(word, test) <= maxEdits) { words.Add(new Word(test, PostingsAddress)); } } } else { stop = true; } } else { distanceResolver.Put(Value, depth); } // Go left (deep) if (LeftChild != null) { LeftChild.WithinEditDistanceDepthFirst(word, test, words, depth + 1, maxEdits, distanceResolver, stop); } // Go right (wide) if (RightSibling != null) { RightSibling.WithinEditDistanceDepthFirst(word, state, words, depth, maxEdits, distanceResolver); } } }
private void WithinEditDistanceDepthFirst( string word, string state, IList <Word> words, int depth, int maxEdits, IDistanceResolver distanceResolver, bool stop = false) { var reachedMin = maxEdits == 0 || depth >= word.Length - 1 - maxEdits; var reachedMax = depth >= word.Length + maxEdits; var node = Step(); if (node == LcrsNode.MinValue) { return; } else if (node.Value == Serializer.SegmentDelimiter) { return; } if (reachedMax || stop) { Skip(node.Weight - 1); } else { string test; if (depth == state.Length) { test = state + node.Value; } else { test = state.ReplaceOrAppend(depth, node.Value); } if (reachedMin) { if (distanceResolver.IsValid(node.Value, depth)) { if (node.EndOfWord) { if (distanceResolver.GetDistance(word, test) <= maxEdits) { words.Add(new Word(test, 1, node.PostingsAddress)); } } } else { stop = true; } } // Go left (deep) if (node.HaveChild) { WithinEditDistanceDepthFirst(word, test, words, depth + 1, maxEdits, distanceResolver, stop); } // Go right (wide) if (node.HaveSibling) { WithinEditDistanceDepthFirst(word, state, words, depth, maxEdits, distanceResolver); } } }