Esempio n. 1
0
        void AddWord(string word, int index)
        {
            WordHint hint = new WordHint(word, index);

            m_root.AddChild(hint);

            string text  = hint.Text;
            int    space = text.IndexOf(' ');

            while (space > 0)
            {
                string candidate = text.Substring(space + 1);
                if (!string.IsNullOrWhiteSpace(candidate))
                {
                    hint = new WordHint(candidate, index);
                    m_root.AddChild(hint);
                    //Console.WriteLine("TRIE candidate [{0}] voice {1}", candidate, m_voice);
                }
                if (text.Length < space + 1)
                {
                    break;
                }
                space = text.IndexOf(' ', space + 1);
            }
        }
Esempio n. 2
0
        public bool AddChild(WordHint wordHint)
        {
            if (!string.IsNullOrEmpty(m_name) &&
                !wordHint.Text.StartsWith(m_name, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            int newLevel = Level + 1;

            bool lastChild = newLevel >= wordHint.Text.Length;

            string newName = lastChild ? wordHint.Text :
                             wordHint.Text.Substring(0, newLevel);

            TrieCell oldChild = null;

            if (m_children.TryGetValue(newName, out oldChild))
            {
                return(oldChild.AddChild(wordHint));
            }

            TrieCell newChild = new TrieCell(newName, wordHint, newLevel);

            m_children[newName] = newChild;

            if (newLevel < wordHint.Text.Length)
            { // if there are still chars left, add a grandchild recursively.
                newChild.AddChild(wordHint);
            }

            return(true);
        }