bool ProcessIndex(ViterbiLattice lattice, int startIndex, string suffix) { var found = false; for (var endIndex = 1; endIndex < suffix.Length + 1; endIndex++) { var prefix = suffix.Substring(0, endIndex); var result = DoubleArrayTrie.Lookup(prefix); if (result > 0) { found = true; // Don't produce unknown word starting from this index foreach (var wordId in Dictionary.LookupWordIds(result)) { var node = new ViterbiNode(wordId, prefix, Dictionary, startIndex, ViterbiNode.NodeType.Known); lattice.AddNode(node, startIndex + 1, startIndex + 1 + endIndex); } } else if (result < 0) { // If result is less than zero, continue to next position break; } } return(found); }
void TestSimpleTrie(bool compact) { var trie = MakeTrie(); var doubleArrayTrie = new DoubleArrayTrie(compact); doubleArrayTrie.Build(trie); using (var ms = new MemoryStream()) { doubleArrayTrie.Write(ms); ms.Seek(0, SeekOrigin.Begin); doubleArrayTrie = DoubleArrayTrie.Read(ms); } doubleArrayTrie.Lookup("a").Is(0); (doubleArrayTrie.Lookup("abc") > 0).IsTrue(); (doubleArrayTrie.Lookup("あいう") > 0).IsTrue(); (doubleArrayTrie.Lookup("xyz") < 0).IsTrue(); }