public void TestEscapeChars() { PatriciaTrie <string> trie = new PatriciaTrie <string>(); trie["new"] = "no error"; Assert.IsFalse(trie.ContainsKeyPrefix("new\na")); Assert.IsFalse(trie.ContainsKeyPrefix("\n")); Assert.IsFalse(trie.ContainsKeyPrefix("\t")); }
public void TestEscapeChars() { var trie = new PatriciaTrie <string>(); trie.Add("new", "no error"); trie.ContainsKeyPrefix("new\na").IsFalse(); trie.ContainsKeyPrefix("\n").IsFalse(); trie.ContainsKeyPrefix("\t").IsFalse(); }
public void TestMultiThreadedTrie() { int numThreads = 10; int perThreadRuns = 50000; int keySetSize = 1000; List <Task> tasks = new List <Task>(); List <string> randoms = new List <string>(); PatriciaTrie <int?> trie = new PatriciaTrie <int?>(); for (int i = 0; i < keySetSize; i++) { String random = System.Guid.NewGuid().ToString(); randoms.Add(random); trie[random] = i; } for (int i = 0; i < numThreads; i++) { var task = new Task(() => { for (int run = 0; run < perThreadRuns; run++) { var rand = new Random(); int randomIndex = rand.Next(randoms.Count - 1); string random = randoms[randomIndex]; // Test retrieve Assert.AreEqual(randomIndex, (int)trie[random]); int randomPrefixLength = rand.Next(random.Length - 1); // Test random prefix length prefix match Assert.IsTrue(trie.ContainsKeyPrefix(random.Substring(0, randomPrefixLength))); } }); tasks.Add(task); task.Start(); } foreach (var task in tasks) { task.Wait(); } Assert.IsTrue(true); }
public void TestTextScan() { PatriciaTrie <string> trie = new PatriciaTrie <string>(); String[] terms = new String[] { "お寿司", "sushi", "美味しい", "tasty", "日本", "japan", "だと思います", "i think", "料理", "food", "日本料理", "japanese food", "一番", "first and foremost", }; for (int i = 0; i < terms.Length; i += 2) { trie[terms[i]] = terms[i + 1]; } string text = "日本料理の中で、一番美味しいのはお寿司だと思います。すぐ日本に帰りたいです。"; StringBuilder builder = new StringBuilder(); int startIndex = 0; while (startIndex < text.Length) { int matchLength = 0; while (trie.ContainsKeyPrefix(text.Substring(startIndex, matchLength + 1))) { matchLength++; } if (matchLength > 0) { String match = text.Substring(startIndex, matchLength); builder.Append("["); builder.Append(match); builder.Append("|"); builder.Append(trie[match]); builder.Append("]"); startIndex += matchLength; } else { builder.Append(text[startIndex]); startIndex++; } } Assert.AreEqual("[日本料理|japanese food]の中で、[一番|first and foremost][美味しい|tasty]のは[お寿司|sushi][だと思います|i think]。すぐ[日本|japan]に帰りたいです。", builder.ToString()); }
public void TestMultiThreadedTrie() { var numThreads = 10; var perThreadRuns = 50000; var keySetSize = 1000; var threads = new List <Task>(); var randoms = Enumerable.Range(0, keySetSize).Select(_ => Guid.NewGuid().ToString()).ToArray(); var trie = new PatriciaTrie <int>(); for (var i = 0; i < keySetSize; i++) { trie.Add(randoms[i], i); } for (int i = 0; i < numThreads; i++) { var thread = Task.Run(() => { var rand = new Random(); for (int run = 0; run < perThreadRuns; run++) { int randomIndex = rand.Next(randoms.Length); var random = randoms[randomIndex]; // Test retrieve trie[random].Is(randomIndex); int randomPrefixLength = rand.Next(random.Length); // Test random prefix length prefix match trie.ContainsKeyPrefix(random.Substring(0, randomPrefixLength)).IsTrue(); } }); threads.Add(thread); } Task.WaitAll(threads.ToArray()); true.IsTrue(); }
public void TestPrefix() { PatriciaTrie <string> trie = new PatriciaTrie <string>(); string[] tokyoPlaces = new string[] { "Hachiōji", "Tachikawa", "Musashino", "Mitaka", "Ōme", "Fuchū", "Akishima", "Chōfu", "Machida", "Koganei", "Kodaira", "Hino", "Higashimurayama", "Kokubunji", "Kunitachi", "Fussa", "Komae", "Higashiyamato", "Kiyose", "Higashikurume", "Musashimurayama", "Tama", "Inagi", "Hamura", "Akiruno", "Nishitōkyō" }; for (int i = 0; i < tokyoPlaces.Length; i++) { trie[tokyoPlaces[i]] = tokyoPlaces[i]; } // Prefixes of Kodaira Assert.IsTrue(trie.ContainsKeyPrefix("K")); Assert.IsTrue(trie.ContainsKeyPrefix("Ko")); Assert.IsTrue(trie.ContainsKeyPrefix("Kod")); Assert.IsTrue(trie.ContainsKeyPrefix("Koda")); Assert.IsTrue(trie.ContainsKeyPrefix("Kodai")); Assert.IsTrue(trie.ContainsKeyPrefix("Kodair")); Assert.IsTrue(trie.ContainsKeyPrefix("Kodaira")); Assert.IsFalse(trie.ContainsKeyPrefix("Kodaira ")); Assert.IsFalse(trie.ContainsKeyPrefix("Kodaira ")); Assert.IsTrue(trie["Kodaira"] != null); // Prefixes of Fussa Assert.IsFalse(trie.ContainsKeyPrefix("fu")); Assert.IsTrue(trie.ContainsKeyPrefix("Fu")); Assert.IsTrue(trie.ContainsKeyPrefix("Fus")); }
public void TestPrefix() { var trie = new PatriciaTrie <string>(); var tokyoPlaces = new string[] { "Hachiōji", "Tachikawa", "Musashino", "Mitaka", "Ōme", "Fuchū", "Akishima", "Chōfu", "Machida", "Koganei", "Kodaira", "Hino", "Higashimurayama", "Kokubunji", "Kunitachi", "Fussa", "Komae", "Higashiyamato", "Kiyose", "Higashikurume", "Musashimurayama", "Tama", "Inagi", "Hamura", "Akiruno", "Nishitōkyō" }; foreach (var place in tokyoPlaces) { trie.Add(place, place); } // Prefixes of Kodaira trie.ContainsKeyPrefix("K").IsTrue(); trie.ContainsKeyPrefix("Ko").IsTrue(); trie.ContainsKeyPrefix("Kod").IsTrue(); trie.ContainsKeyPrefix("Koda").IsTrue(); trie.ContainsKeyPrefix("Kodai").IsTrue(); trie.ContainsKeyPrefix("Kodair").IsTrue(); trie.ContainsKeyPrefix("Kodaira").IsTrue(); trie.ContainsKeyPrefix("Kodaira ").IsFalse(); trie.ContainsKeyPrefix("Kodaira ").IsFalse(); trie["Kodaira"].IsNotNull(); // Prefixes of Fussa trie.ContainsKeyPrefix("fu").IsFalse(); trie.ContainsKeyPrefix("Fu").IsTrue(); trie.ContainsKeyPrefix("Fus").IsTrue(); }