Ejemplo n.º 1
0
        private IEnumerable <ICandidate> CollectCandidatesAtTrie(string prefix, ListTrie trie)
        {
            var candidates = new List <ICandidate>();
            var trieQueue  = new Queue <Tuple <string, ListTrie> >();

            trieQueue.Enqueue(new Tuple <string, ListTrie>(prefix, trie));

            while (trieQueue.Count != 0)
            {
                var nextTrieTuple = trieQueue.Dequeue();
                var nextPrefix    = nextTrieTuple.Item1;
                var nextTrie      = nextTrieTuple.Item2;

                foreach (ListTrie subTrie in nextTrie._subTries)
                {
                    if (subTrie._isTerminatedWord)
                    {
                        candidates.Add(new Candidate(nextPrefix + subTrie._character, subTrie._wordOccurenceCount));
                    }

                    trieQueue.Enqueue(new Tuple <string, ListTrie>(nextPrefix + subTrie._character, subTrie));
                }
            }

            return(candidates);
        }
Ejemplo n.º 2
0
        private ListTrie FindLastSubTrieIfExists(string word)
        {
            ListTrie currentTrie = this;

            foreach (var c in word)
            {
                var sub = currentTrie._subTries.FirstOrDefault(x => x._character == c);
                if (sub != null)
                {
                    currentTrie = sub;
                }
                else
                {
                    return(null);
                }
            }

            return(currentTrie);
        }
Ejemplo n.º 3
0
        public void TrainTrie(IEnumerable <string> words)
        {
            foreach (var word in words)
            {
                ListTrie currentTrie = this;

                foreach (char c in word)
                {
                    var sub = currentTrie._subTries.FirstOrDefault(x => x._character == c);
                    if (sub == null)
                    {
                        sub            = new ListTrie();
                        sub._character = c;
                        currentTrie._subTries.Add(sub);
                    }

                    currentTrie = sub;
                }

                currentTrie._isTerminatedWord = true;
                currentTrie._wordOccurenceCount++;
            }
        }