protected internal virtual void UpdateAllMatches(TrieMap <K, V> trie, IList <Match <K, V> > matches, IList <K> matched, IList <K> list, int start, int end)
 {
     for (int i = start; i < end; i++)
     {
         UpdateAllMatchesWithStart(trie, matches, matched, list, i, end);
     }
 }
Example #2
0
        public virtual void TestTrieBasic()
        {
            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);
            System.Console.Out.WriteLine(trieMap);
            System.Console.Out.WriteLine(trieMap.ToFormattedString());
            // Test get and remove
            NUnit.Framework.Assert.IsTrue(trieMap.Get(new string[] { "a", "white", "hat" }));
            NUnit.Framework.Assert.IsNull(trieMap.Get(new string[] { "a", "white" }));
            trieMap.Remove(new string[] { "a", "white", "hat" });
            NUnit.Framework.Assert.IsTrue(trieMap.Get(new string[] { "a", "white", "cat" }));
            NUnit.Framework.Assert.IsNull(trieMap.Get(new string[] { "a", "white", "hat" }));
            // Test keys
            NUnit.Framework.Assert.IsTrue(trieMap.Contains(new string[] { "a", "white", "cat" }));
            NUnit.Framework.Assert.IsFalse(trieMap.Contains(new string[] { "white", "cat" }));
            NUnit.Framework.Assert.AreEqual(3, trieMap.Count);
            NUnit.Framework.Assert.AreEqual(3, trieMap.Keys.Count);
            // Test putAll
            IDictionary <IList <string>, bool> m = new Dictionary <IList <string>, bool>();

            m[Arrays.AsList("a", "purple", "giraffe")]  = true;
            m[Arrays.AsList("four", "orange", "bears")] = true;
            trieMap.PutAll(m);
            NUnit.Framework.Assert.IsTrue(trieMap.Contains(new string[] { "a", "purple", "giraffe" }));
            NUnit.Framework.Assert.IsTrue(trieMap.Contains(new string[] { "four", "orange", "bears" }));
            NUnit.Framework.Assert.AreEqual(5, trieMap.Count);
            NUnit.Framework.Assert.AreEqual(5, trieMap.Keys.Count);
        }
Example #3
0
        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);
        }
 private PartialApproxMatch(double cost, TrieMap <K, V> trie, int alignmentLength)
 {
     // Helper class for keeping track of partial matches with TrieMatcher
     this.trie  = trie;
     this.cost  = cost;
     this.value = (trie != null) ? this.trie.value : null;
     if (alignmentLength > 0)
     {
         this.alignments = new Interval[alignmentLength];
     }
 }
 public TrieMapMatcher(TrieMap <K, V> root, IList <K> multimatchDelimiter)
 {
     this.root = root;
     this.multimatchDelimiter = multimatchDelimiter;
     if (multimatchDelimiter != null && !multimatchDelimiter.IsEmpty())
     {
         // Create a new root that always starts with the delimiter
         rootWithDelimiter = new TrieMap <K, V>();
         rootWithDelimiter.PutChildTrie(multimatchDelimiter, root);
     }
     else
     {
         rootWithDelimiter = root;
     }
 }
Example #6
0
        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);
        }
 protected internal virtual void UpdateAllMatchesWithStart(TrieMap <K, V> trie, IList <Match <K, V> > matches, IList <K> matched, IList <K> list, int start, int end)
 {
     if (start > end)
     {
         return;
     }
     if (trie.children != null && start < end)
     {
         K key = list[start];
         TrieMap <K, V> child = trie.children[key];
         if (child != null)
         {
             IList <K> p = new List <K>(matched.Count + 1);
             Sharpen.Collections.AddAll(p, matched);
             p.Add(key);
             UpdateAllMatchesWithStart(child, matches, p, list, start + 1, end);
         }
     }
     if (trie.IsLeaf())
     {
         matches.Add(new Match <K, V>(matched, trie.value, start - matched.Count, start));
     }
 }
Example #8
0
 public _KeyValuePair_289(TrieMap <K, V> _enclosing, IList <K> prefix)
 {
     this._enclosing = _enclosing;
     this.prefix     = prefix;
 }
 private TrieMapMatcher.PartialApproxMatch <K, V> WithMatch(IMatchCostFunction <K, V> costFunction, double deltaCost, K t, K k, bool multimatch, TrieMap <K, V> root)
 {
     TrieMapMatcher.PartialApproxMatch <K, V> res = WithMatch(costFunction, deltaCost, t, k);
     if (multimatch && res.matched != null && res.value != null)
     {
         // Update tracking of matched keys and values for multiple entry matches
         if (res.multimatches == null)
         {
             res.multimatches = new List <Match <K, V> >(1);
         }
         else
         {
             res.multimatches = new List <Match <K, V> >(multimatches.Count + 1);
             Sharpen.Collections.AddAll(res.multimatches, multimatches);
         }
         IList <K> newlyMatched = res.matched.SubList(lastMultimatchedMatchedStartIndex, res.matched.Count);
         res.multimatches.Add(new Match <K, V>(newlyMatched, res.value, lastMultimatchedOriginalStartIndex, res.end));
         res.cost += costFunction.MultiMatchDeltaCost(newlyMatched, res.value, multimatches, res.multimatches);
         res.lastMultimatchedMatchedStartIndex  = res.matched.Count;
         res.lastMultimatchedOriginalStartIndex = res.end;
         // Reset current value/key being matched
         res.trie = root;
     }
     return(res);
 }
 public TrieMapMatcher(TrieMap <K, V> root)
 {
     this.root = root;
     this.rootWithDelimiter = root;
 }