Ejemplo n.º 1
0
def test_prune_and_rescale():
    def rescalefunc(weight): return (weight//10)
    def rescalefunc2(weight): return (weight-1)
    def rescalefunc3(weight): return (weight//2)

    # test2_auto
    autocomplete_me.prune_trie(test2_auto, 20)
    assert autocomplete_me.autocomplete(test2_auto, "", 5) == [(920, "hermione"), (67, "her")]
    autocomplete_me.rescale_weight(test2_auto, rescalefunc)
    assert autocomplete_me.autocomplete(test2_auto, "", 5) == [(92, "hermione"), (6, "her")]
    assert autocomplete_me.Trie(test2_auto).searchTrie("her").maxweight == 92

    # babies_auto
    autocomplete_me.prune_trie(babies_auto, 300)
    assert autocomplete_me.autocomplete(babies_auto, "Gio", 5) == [(3086, "Giovanni"), (372, "Giovani")]
    assert autocomplete_me.autocomplete(babies_auto, "Ayl", 5) == [(1284, "Ayla"), (673, "Aylin"), (379, "Ayleen")]
    autocomplete_me.rescale_weight(babies_auto, rescalefunc2)
    assert autocomplete_me.autocomplete(babies_auto, "Sab", 5) == [(1174, "Sabrina")]
    assert autocomplete_me.autocomplete(babies_auto, "Sel", 5) == [(1043, "Selena"), (570, "Selah")]
    
    # mandarin_auto
    autocomplete_me.prune_trie(mandarin_auto, 4000)
    assert autocomplete_me.autocomplete(mandarin_auto, "不", 5) == [(15666, "不是"), (7167, "不要"), (6942, "不能"), (6892, "不知道"), (4256, "不起")]
    assert autocomplete_me.autocomplete(mandarin_auto, "好", 5) == [(6171, "好了"), (5391, "好吧"), (5170, "好的")]
    autocomplete_me.rescale_weight(mandarin_auto, rescalefunc3)
    assert autocomplete_me.autocomplete(mandarin_auto, "意", 5) == "No words match with the given prefix."
    assert autocomplete_me.autocomplete(mandarin_auto, "我", 5) == [(20865, "我的")]
    def test_weight(self):
        # test when children.weight > node.weight
        trie = autocomplete_me.Trie()
        trie.insert(Node(weight=123, word="apple"))
        trie.insert(Node(weight=234, word="apples"))

        self.assertEqual(trie.search("apple").weight, 123)
        self.assertEqual(trie.search("apple").maxWeight, 234)
Ejemplo n.º 3
0
def test_delete_term():
    # test2_auto
    # case 1: node has no children
    assert autocomplete_me.Trie(test2_auto).searchTrie("in").maxweight == 20
    autocomplete_me.delete_term(test2_auto, "inn")
    assert autocomplete_me.autocomplete(test2_auto, "i", 50) == [(5,'in')]
    assert autocomplete_me.Trie(test2_auto).searchTrie("inn") == False
    assert autocomplete_me.Trie(test2_auto).searchTrie("in").maxweight == 5

    # pokemon_auto
    # case 2: node has children
    autocomplete_me.delete_term(pokemon_auto, "Porygon2")
    assert autocomplete_me.autocomplete(pokemon_auto, "Pory", 2) == [(83878,"Porygon-Z"), (533, "Porygon")]
    assert autocomplete_me.Trie(pokemon_auto).searchTrie("Porygon2") == False
    assert autocomplete_me.Trie(pokemon_auto).searchTrie("Porygon").maxweight == 83878
    autocomplete_me.delete_term(pokemon_auto, "Porygon-Z")
    assert autocomplete_me.autocomplete(pokemon_auto, "Pory", 2) == [(533, "Porygon")]
    def test_wordList(self):
        # test if len(wordList)=len(children) when len(children)<k
        trie = autocomplete_me.Trie()
        trie.insert(Node(weight=123, word="apple"))
        trie.insert(Node(weight=234, word="apples"))
        trie.insert(Node(weight=67, word="applet"))
        trie.insert(Node(weight=88, word="appletree"))

        self.assertEqual(len(autocomplete_me.autocomplete("apple", trie, 6)),
                         4)
Ejemplo n.º 5
0
def test_add_term():
    # test2_auto
    autocomplete_me.add_term(test2_auto, "hermione", 919)
    assert autocomplete_me.Trie(test2_auto).searchTrie("hermion").children["e"].fullword == "hermione"
    assert autocomplete_me.Trie(test2_auto).searchTrie("hermione").weight == 919
    assert autocomplete_me.Trie(test2_auto).searchTrie("her").maxweight == 919
    assert autocomplete_me.Trie(test2_auto).searchTrie("her").weight == 67
    assert autocomplete_me.autocomplete(test2_auto, "he", 3) == [(919,'hermione'),(67,'her')]
    # babies_auto
    autocomplete_me.add_term(babies_auto, "Ron", 19800301)
    assert autocomplete_me.autocomplete(babies_auto, "Ro", 3) == [(19800301,'Ron'),(6882,'Robert'),(2562,'Roman')]
    assert autocomplete_me.Trie(babies_auto).searchTrie("Ron").weight == 19800301
    assert autocomplete_me.Trie(babies_auto).searchTrie("Ro").maxweight == 19800301
    assert autocomplete_me.Trie(babies_auto).searchTrie("Ro").weight == -1
    def test_insert(self):
        # test insert function
        trie = autocomplete_me.Trie()
        trie.insert(Node(weight=500, word="a"))
        trie.insert(Node(weight=123, word="apple"))
        trie.insert(Node(weight=234, word="apples"))

        self.assertEqual(trie.root.weight, -1)
        self.assertEqual(trie.root.word, None)
        self.assertEqual(trie.root.maxWeight, 500)
        self.assertTrue('a', trie.root.children)

        trie.insert(Node(weight=677, word="c"))
        trie.insert(Node(weight=2900, word="cat"))

        self.assertEqual(trie.root.weight, -1)
        self.assertEqual(trie.root.word, None)
        self.assertEqual(trie.root.maxWeight, 2900)
        self.assertTrue('a', trie.root.children)
        self.assertTrue('c', trie.root.children)
Ejemplo n.º 7
0
def test_insert_or_update():
    # updating existing words
    autocomplete_me.insert_or_update(test2_auto, "can", 23)
    assert autocomplete_me.Trie(test2_auto).searchTrie("can").weight == 16
    assert autocomplete_me.Trie(test2_auto).searchTrie("ca").maxweight == 16
    autocomplete_me.insert_or_update(test2_auto, "cat", 100)
    autocomplete_me.insert_or_update(test2_auto, "cat", 1000)
    # make sure the maxweight is updated as well
    assert autocomplete_me.Trie(test2_auto).searchTrie("cat").weight == 17
    assert autocomplete_me.Trie(test2_auto).searchTrie("ca").maxweight == 17

    # inserting new words
    # test2_auto
    autocomplete_me.insert_or_update(test2_auto, "hermione", 919)
    assert autocomplete_me.autocomplete(test2_auto, "her", 1) == [(920,'hermione')]
    assert autocomplete_me.autocomplete(test2_auto, "he", 3) == [(920,'hermione'),(67,'her')]
    assert autocomplete_me.Trie(test2_auto).searchTrie("her").maxweight == 920
    assert autocomplete_me.Trie(test2_auto).searchTrie("her").weight == 67
    # pokemon_auto
    autocomplete_me.insert_or_update(pokemon_auto, "jinhowchong", 19960308)
    assert autocomplete_me.autocomplete(pokemon_auto, "jin", 5) == [(19960308,'jinhowchong')]
    assert autocomplete_me.Trie(pokemon_auto).searchTrie("jinh").maxweight == 19960308
    assert autocomplete_me.Trie(pokemon_auto).searchTrie("jinhowchong").weight == 19960308
Ejemplo n.º 8
0
def test_change_weight():
    # update functions
    def func1(weight): return (weight + 1)
    def func2(weight): return (weight * 6)
    def func3(weight): return (weight//10000)

    # test2_auto
    autocomplete_me.change_weight(test2_auto, "cat", func2)
    assert autocomplete_me.Trie(test2_auto).searchTrie("cat").weight == 17 * 6
    assert autocomplete_me.Trie(test2_auto).searchTrie("c").maxweight == 17 * 6
    autocomplete_me.change_weight(test2_auto, "cat", func3)
    assert autocomplete_me.Trie(test2_auto).searchTrie("cat").weight == 0
    assert autocomplete_me.Trie(test2_auto).searchTrie("ca").maxweight == 16

    # trademarks_auto
    autocomplete_me.change_weight(trademarks_auto, "NORVELL IP LLC", func3)
    autocomplete_me.change_weight(trademarks_auto, "NORVELL IP LLC", func1)
    assert autocomplete_me.Trie(trademarks_auto).searchTrie("NORVELL IP LLC").weight == 85890111//10000 + 1
    assert autocomplete_me.Trie(trademarks_auto).searchTrie("NORVELL IP LLC").maxweight == 85890111//10000 + 1
Ejemplo n.º 9
0
def test_searchTrie():
    assert autocomplete_me.Trie(test_auto).searchTrie("cmugunh").children["o"].fullword == 'cmugunho'
    assert autocomplete_me.Trie(test_auto).searchTrie("cm").children["u"].weight == -1
    assert autocomplete_me.Trie(test_auto).searchTrie("cm").children["u"].maxweight == 925

    assert autocomplete_me.Trie(mandarin_auto).searchTrie("时间").fullword == '时间'
    assert autocomplete_me.Trie(mandarin_auto).searchTrie("是不是").weight == 2158
    assert autocomplete_me.Trie(mandarin_auto).searchTrie("是").maxweight == 116019

    assert autocomplete_me.Trie(pokemon_auto).searchTrie("Po").fullword == None
    assert autocomplete_me.Trie(pokemon_auto).searchTrie("Porygon").weight == 533
    assert autocomplete_me.Trie(pokemon_auto).searchTrie("Porygon").maxweight == 156945
    
    assert autocomplete_me.Trie(trademarks_auto).searchTrie("PEPSICO, INC.").fullword == 'PEPSICO, INC.'
    assert autocomplete_me.Trie(trademarks_auto).searchTrie("BLANK").weight == -1
    assert autocomplete_me.Trie(trademarks_auto).searchTrie("BLANK").maxweight == 85892501

    assert autocomplete_me.Trie(trademarks_auto).searchTrie("invalidprefix") == False