示例#1
0
        private void WithinEditDistanceDepthFirst(string word, string state, List <Word> compressed, int depth, int maxEdits, IDistanceResolver distanceResolver)
        {
            string test;

            if (depth == state.Length)
            {
                test = state + Value;
            }
            else
            {
                test = new string(state.ReplaceOrAppend(depth, Value).ToArray());
            }

            var edits = distanceResolver.Distance(word, test);

            if (edits <= maxEdits)
            {
                if (EndOfWord)
                {
                    compressed.Add(new Word(test, WordCount, PostingsAddress, Postings));
                }
            }

            if (edits <= maxEdits || test.Length < word.Length)
            {
                if (LeftChild != null)
                {
                    LeftChild.WithinEditDistanceDepthFirst(word, test, compressed, depth + 1, maxEdits, distanceResolver);
                }

                if (RightSibling != null)
                {
                    RightSibling.WithinEditDistanceDepthFirst(word, test, compressed, depth, maxEdits, distanceResolver);
                }
            }
        }
示例#2
0
        private void WithinEditDistanceDepthFirst(string word, string state, IList <Word> compressed, int depth, int maxErrors, IDistanceResolver distanceResolver, bool stop = false)
        {
            var reachedMin   = maxErrors == 0 || depth >= word.Length - 1 - maxErrors;
            var reachedDepth = depth >= word.Length - 1;
            var reachedMax   = depth >= word.Length + maxErrors;

            var node = Step();

            if (node == LcrsNode.MinValue)
            {
                return;
            }

            if (reachedMax || stop)
            {
                Skip(node.Weight - 1);
            }
            else
            {
                string test;

                if (depth == state.Length)
                {
                    test = state + node.Value;
                }
                else
                {
                    test = new string(state.ReplaceOrAppend(depth, node.Value).ToArray());
                }

                if (reachedMin)
                {
                    var edits = distanceResolver.Distance(word, test);

                    if (edits <= maxErrors)
                    {
                        if (node.EndOfWord)
                        {
                            compressed.Add(new Word(test, 1, node.PostingsAddress));
                        }
                    }
                    else if (edits > maxErrors && reachedDepth)
                    {
                        stop = true;
                    }
                    else if (reachedDepth)
                    {
                        stop = true;
                    }
                }

                // Go left (deep)
                if (node.HaveChild)
                {
                    WithinEditDistanceDepthFirst(word, test, compressed, depth + 1, maxErrors, distanceResolver, stop);
                }

                // Go right (wide)
                if (node.HaveSibling)
                {
                    WithinEditDistanceDepthFirst(word, state, compressed, depth, maxErrors, distanceResolver);
                }
            }
        }