public virtual IList <TrieMapMatcher.PartialApproxMatch <K, V> > ToSortedList() { IList <TrieMapMatcher.PartialApproxMatch <K, V> > res = queue.ValuesList(); res.Sort(TrieMapMatcher.PartialMatchComparator <K, V>()); return(res); }
public virtual void TestTrieFindClosest() { TrieMap <string, bool> trieMap = new TrieMap <string, bool>(); trieMap.Put(new string[] { "a", "white", "cat" }, true); trieMap.Put(new string[] { "a", "white", "hat" }, true); trieMap.Put(new string[] { "a", "black", "cat" }, true); trieMap.Put(new string[] { "a", "black", "hat" }, true); trieMap.Put(new string[] { "a", "colored", "hat" }, true); TrieMapMatcher <string, bool> matcher = new TrieMapMatcher <string, bool>(trieMap); IList <ApproxMatch <string, bool> > matches = matcher.FindClosestMatches(new string[] { "the", "black", "hat" }, 2); IList <ApproxMatch <string, bool> > expected = new List <ApproxMatch <string, bool> >(); expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "black", "hat"), true, 0, 3, 1.0)); expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "black", "cat"), true, 0, 3, 2.0)); NUnit.Framework.Assert.AreEqual("\nExpecting " + expected + ",\n got " + matches, expected, matches); //System.out.println(matches); // TODO: ordering of results with same score matches = matcher.FindClosestMatches(new string[] { "the", "black" }, 5); expected = new List <ApproxMatch <string, bool> >(); expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "black", "cat"), true, 0, 2, 2.0)); expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "black", "hat"), true, 0, 2, 2.0)); expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "colored", "hat"), true, 0, 2, 3.0)); expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "white", "cat"), true, 0, 2, 3.0)); expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "white", "hat"), true, 0, 2, 3.0)); NUnit.Framework.Assert.AreEqual("\nExpecting " + StringUtils.Join(expected, "\n") + ",\ngot " + StringUtils.Join(matches, "\n"), expected, matches); //System.out.println(matches); matches = matcher.FindClosestMatches(new string[] { "the", "black", "cat", "is", "wearing", "a", "white", "hat" }, 5); expected = new List <ApproxMatch <string, bool> >(); expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "white", "hat"), true, 0, 8, 5.0)); expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "black", "cat"), true, 0, 8, 6.0)); expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "black", "hat"), true, 0, 8, 6.0)); expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "colored", "hat"), true, 0, 8, 6.0)); expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "white", "cat"), true, 0, 8, 6.0)); NUnit.Framework.Assert.AreEqual("Expecting " + StringUtils.Join(expected, "\n") + ",\ngot " + StringUtils.Join(matches, "\n"), expected, matches); //System.out.println(matches); matches = matcher.FindClosestMatches(new string[] { "the", "black", "cat", "is", "wearing", "a", "white", "hat" }, 6, true, true); // [([[a, black, cat]-[a, white, hat]] -> true-true at (0,8),3.0), expected = new List <ApproxMatch <string, bool> >(); expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "black", "cat", "a", "white", "hat"), true, 0, 8, Arrays.AsList(new Match <string, bool>(Arrays.AsList("a", "black", "cat"), true, 0, 3), new Match <string, bool>(Arrays.AsList("a", "white", "hat"), true, 5, 8)), 3.0)); // ([[a, black, hat]-[a, black, hat]] -> true-true at (0,8),4.0), expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "black", "cat", "a", "black", "hat"), true, 0, 8, Arrays.AsList(new Match <string, bool>(Arrays.AsList("a", "black", "cat"), true, 0, 3), new Match <string, bool>(Arrays.AsList("a", "black", "hat"), true, 5, 8)), 4.0)); // ([[a, black, hat]-[a, colored, hat]] -> true-true at (0,8),4.0), expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "black", "cat", "a", "colored", "hat"), true, 0, 8, Arrays.AsList(new Match <string, bool>(Arrays.AsList("a", "black", "cat"), true, 0, 3), new Match <string, bool>(Arrays.AsList("a" , "colored", "hat"), true, 5, 8)), 4.0)); // ([[a, black, cat]-[a, white, cat]] -> true-true at (0,8),4.0), expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "black", "cat", "a", "white", "cat"), true, 0, 8, Arrays.AsList(new Match <string, bool>(Arrays.AsList("a", "black", "cat"), true, 0, 3), new Match <string, bool>(Arrays.AsList("a", "white", "cat"), true, 5, 8)), 4.0)); // ([[a, black, cat]-[a, white, hat]] -> true-true at (0,8),4.0), expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "black", "hat", "a", "white", "hat"), true, 0, 8, Arrays.AsList(new Match <string, bool>(Arrays.AsList("a", "black", "hat"), true, 0, 3), new Match <string, bool>(Arrays.AsList("a", "white", "hat"), true, 5, 8)), 4.0)); // ([[a, black, cat]-[a, black, cat]-[a, white, hat]] -> true-true at (0,8),4.0)] expected.Add(new ApproxMatch <string, bool>(Arrays.AsList("a", "black", "cat", "a", "black", "cat", "a", "white", "hat"), true, 0, 8, Arrays.AsList(new Match <string, bool>(Arrays.AsList("a", "black", "cat"), true, 0, 3), new Match <string, bool >(Arrays.AsList("a", "black", "cat"), true, 3, 5), new Match <string, bool>(Arrays.AsList("a", "white", "hat"), true, 5, 8)), 4.0)); NUnit.Framework.Assert.AreEqual("\nExpecting " + StringUtils.Join(expected, "\n") + ",\ngot " + StringUtils.Join(matches, "\n"), expected, matches); }
public override IList <TrieMapMatcher.PartialApproxMatch <K, V> > ToSortedList() { IList <TrieMapMatcher.PartialApproxMatch <K, V> > all = new List <TrieMapMatcher.PartialApproxMatch <K, V> >(Size()); foreach (BoundedCostOrderedMap <Match <K, V>, TrieMapMatcher.PartialApproxMatch <K, V> > q in multimatchQueues.Values) { Sharpen.Collections.AddAll(all, q.ValuesList()); } all.Sort(TrieMapMatcher.PartialMatchComparator <K, V>()); return(all); }
public virtual void TestTrieFindNonOverlapping() { TrieMap <string, bool> trieMap = new TrieMap <string, bool>(); trieMap.Put(new string[] { "a", "white", "cat" }, true); trieMap.Put(new string[] { "a", "white", "hat" }, true); trieMap.Put(new string[] { "a", "black", "cat" }, true); trieMap.Put(new string[] { "a", "black", "cat", "climbed", "on", "the", "sofa" }, true); trieMap.Put(new string[] { "white" }, true); TrieMapMatcher <string, bool> matcher = new TrieMapMatcher <string, bool>(trieMap); IList <Match <string, bool> > matches = matcher.FindNonOverlapping("a", "white", "cat", "is", "wearing", "a", "white", "hat", "and", "a", "black", "cat", "climbed", "on", "the", "sofa"); IList <Match <string, bool> > expected = new List <Match <string, bool> >(); expected.Add(new Match <string, bool>(Arrays.AsList("a", "white", "cat"), true, 0, 3)); expected.Add(new Match <string, bool>(Arrays.AsList("a", "white", "hat"), true, 5, 8)); expected.Add(new Match <string, bool>(Arrays.AsList("a", "black", "cat", "climbed", "on", "the", "sofa"), true, 9, 16)); NUnit.Framework.Assert.AreEqual("Expecting " + expected.Count + " matches: got " + matches, expected.Count, matches.Count); NUnit.Framework.Assert.AreEqual("Expecting " + expected + ", got " + matches, expected, matches); }