private DictionaryTrie FindLastSubTrieIfExists(string word) { DictionaryTrie currentTrie = this; foreach (var c in word) { if (!currentTrie._subTries.TryGetValue(c, out currentTrie)) { return(null); } } return(currentTrie); }
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); } } }
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++; } }