コード例 #1
0
        private DictionaryTrie FindLastSubTrieIfExists(string word)
        {
            DictionaryTrie currentTrie = this;

            foreach (var c in word)
            {
                if (!currentTrie._subTries.TryGetValue(c, out currentTrie))
                {
                    return(null);
                }
            }

            return(currentTrie);
        }
コード例 #2
0
        private IEnumerable <ICandidate> CollectCandidatesAtTrie(string prefix, DictionaryTrie trie)
        {
            foreach (var charAndSubTrie in trie._subTries)
            {
                var character = charAndSubTrie.Key;
                var subTrie   = charAndSubTrie.Value;

                if (subTrie._isTerminatedWord)
                {
                    yield return(new Candidate(prefix + character, subTrie._wordOccurenceCount));
                }

                foreach (var candidate in CollectCandidatesAtTrie(prefix + character, subTrie))
                {
                    yield return(candidate);
                }
            }
        }
コード例 #3
0
        public void TrainTrie(IEnumerable <string> words)
        {
            foreach (var word in words)
            {
                DictionaryTrie currentTrie = this;

                foreach (char c in word)
                {
                    if (!currentTrie._subTries.ContainsKey(c))
                    {
                        currentTrie._subTries[c] = new DictionaryTrie();
                    }

                    currentTrie = currentTrie._subTries[c];
                }

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