Exemple #1
0
        public IEnumerable <ITrieMatch> EditLookup(string s, int maxEdit, int maxMatches = int.MaxValue)
        {
            var matches = new List <TrieMatch>();

            if (s.Length > 0)
            {
                m_AN1.Clear();
                m_AN2.Clear();

                BeginAC(maxEdit);

                for (int i = 0; i < s.Length; i++)
                {
                    AppendChar(s[i]);
                }

                int curState;
                while ((curState = m_AN1.GetNext()) != -1)
                {
                    var entity = Output(curState);
                    if (entity != -1)
                    {
                        matches.Add(new TrieMatch(this, entity, m_AN1.Distance(curState)));
                    }
                }
                matches = (from match in matches
                           orderby match.Distance ascending
                           select match).Take(maxMatches).ToList();
                m_AN1.ClearDistances();
            }
            return((IEnumerable <ITrieMatch>)matches);
        }
Exemple #2
0
        public void AppendChar(char c)
        {
            m_prefix += c;
            int prefixlen = m_prefix.Length;

            int currState;
            int idx = prefixlen - 1;

            while ((currState = m_AN1.GetNext()) != -1)
            {
                int dist = m_AN1.Distance(currState);
                if (dist < m_CurrentLookupThreshold)
                {
                    m_AN2.AddNode(currState, dist + 1);
                }

                int childState_inputChar = Goto(currState, c);
                if (childState_inputChar != -1 && childState_inputChar != 0)
                {
                    m_AN2.AddNode(childState_inputChar, dist);
                }

                int newdist = m_AN2.Distance(currState);
                int mindist = (dist < newdist) ? dist : newdist;
                if (mindist < m_CurrentLookupThreshold)
                {
                    m_childState.BeginTarget(currState);

                    int childState;
                    while ((childState = m_childState.GetNextTarget()) != -1)
                    {
                        if (childState != childState_inputChar)
                        {
                            m_AN2.AddNode(childState, mindist + 1);
                        }
                    }
                }
            }
            m_AN1.ClearDistances();
            Swap(ref m_AN1, ref m_AN2);
            m_totalValidNodes += m_AN1.Count;
#if DEBUG
            //Console.WriteLine("No. of active nodes after character {1}: {0}", AN1.Count, i + 1);
            //ActiveNodeAggregator.Add(i + 1, AN1.Count);
#endif
        }