def game():
    print("\nWelcome to Autocomplete\n")

    word_part = input("Give me a word part\n\n")
    # checks emptiness
    if not word_part:
        print("You must enter a word part\n")
        sys.exit(1)

    loads = load("/usr/share/dict/words")
    tree = PrefixTree(loads)
    words = tree.complete(word_part)
    results = print("{} results were found".format(len(words)))
    number = input("How many results would you like?\n")

    # checks edge cases for results display
    if (len(number) <= 0) or not (number.isdigit()):
        print("You must input a positive number :)")
        sys.exit()

    count = 0

    # retrives set number of results
    if 0 < len(words):
        if len(words) < int(number):
            print("Sorry there's not that many but here's what we got\n")
            number = len(words)

        print()

        while (count < int(number)):
            sentence = "    {}.    {}".format((count + 1), words[count])
            print(sentence)
            count += 1
 def test_complete(self):
     strings = ['ABC', 'ABD', 'A', 'XYZ']
     tree = PrefixTree(strings)
     # Verify completions for all substrings
     assert tree.complete('ABC') == ['ABC']
     assert tree.complete('ABD') == ['ABD']
     assert tree.complete('AB') == ['ABC', 'ABD']
     assert tree.complete('BC') == []
     assert tree.complete('BD') == []
     assert tree.complete('A') == ['A', 'ABC', 'ABD']
     assert tree.complete('B') == []
     assert tree.complete('C') == []
     assert tree.complete('D') == []
     assert tree.complete('XYZ') == ['XYZ']
     assert tree.complete('XY') == ['XYZ']
     assert tree.complete('YZ') == []
     assert tree.complete('X') == ['XYZ']
     assert tree.complete('Y') == []
     assert tree.complete('Z') == []
     # Verify empty prefix string as input - aka "suggestions" feature
     completions_with_empty_string = tree.complete('')
     # Check length only
     assert len(completions_with_empty_string) == len(strings)
     # Ignore order
     self.assertCountEqual(completions_with_empty_string, strings)
 def test_size_and_is_empty(self):
     tree = PrefixTree()
     # Verify size after initializing tree
     assert tree.size == 0
     assert tree.is_empty() is True
     # Verify size after first insert
     tree.insert('A')
     assert tree.size == 1
     assert tree.is_empty() is False
     # Verify size after second insert
     tree.insert('ABC')
     assert tree.size == 2
     assert tree.is_empty() is False
     # Verify size after third insert
     tree.insert('ABD')
     assert tree.size == 3
     assert tree.is_empty() is False
     # Verify size after fourth insert
     tree.insert('XYZ')
     assert tree.size == 4
     assert tree.is_empty() is False
     # Verify that size still increases by 1 when spaces included in string
     tree.insert('WAFFLE TIME')
     assert tree.size == 5
     assert tree.is_empty() is False
    def test_complete(self):
        strings = ['ABC', 'ABD', 'A', 'XYZ']
        tree = PrefixTree(strings)
        # Verify completions for all substrings
        assert tree.complete('ABC') == ['ABC']
        assert tree.complete('ABD') == ['ABD']
        assert tree.complete('AB') == ['ABC', 'ABD']

        assert tree.complete('BC') == []
        assert tree.complete('BD') == []

        assert tree.complete('A') == ['A', 'ABC', 'ABD']

        assert tree.complete('B') == []
        assert tree.complete('C') == []
        assert tree.complete('D') == []

        assert tree.complete('XYZ') == ['XYZ']
        assert tree.complete('XY') == ['XYZ']

        assert tree.complete('YZ') == []

        assert tree.complete('X') == ['XYZ']

        assert tree.complete('Y') == []
        assert tree.complete('Z') == []
 def test_size_with_repeated_insert(self):
     tree = PrefixTree()
     # Verify size after initializing tree
     assert tree.size == 0
     assert tree.is_empty() is True
     # Verify size after first insert
     tree.insert('A')
     assert tree.size == 1
     assert tree.is_empty() is False
     # Verify size after repeating first insert
     tree.insert('A')
     assert tree.size == 1
     # Verify size after second insert
     tree.insert('ABC')
     assert tree.size == 2
     # Verify size after repeating second insert
     tree.insert('ABC')
     assert tree.size == 2
     # Verify size after third insert
     tree.insert('ABD')
     assert tree.size == 3
     # Verify size after repeating third insert
     tree.insert('ABD')
     assert tree.size == 3
     # Verify size after fourth insert
     tree.insert('XYZ')
     assert tree.size == 4
     # Verify size after repeating fourth insert
     tree.insert('XYZ')
     assert tree.size == 4
Exemple #6
0
def main():
    boggle = Boggle()
    tree = PrefixTree()
    load_tree(tree)
    found = set()
    boggle.play(tree, found)
    for word in sorted(found):
        print(word)
    print(boggle)
 def test_delete_from_empty_trie(self):
     """
     If string cannot be found, a ValueError is raised.
     """
     # Make the tree
     tree = PrefixTree()
     # raise Exception
     with self.assertRaises(ValueError):
         tree.delete('CS Rocks!')
 def test_init_and_properties(self):
     tree = PrefixTree()
     # Verify tree size property
     assert isinstance(tree.size, int)
     assert tree.size == 0
     # Verify root node
     assert isinstance(tree.root, PrefixTreeNode)
     assert tree.root.character == PrefixTree.START_CHARACTER
     assert tree.root.is_terminal() is False
     assert tree.root.num_children() == 0
Exemple #9
0
def autocomplete_setup(vocabulary, algorithm='trie'):
    """Return the main data structure needed to set up autocomplete using the
    given vocabulary and algorithm, specified as linear_search, trie, etc."""
    if algorithm == 'linear_search':
        # Use the given vocabulary list
        return vocabulary
    elif algorithm == 'trie':
        from prefixtree import PrefixTree
        # Create a trie structure with the vocabulary
        return PrefixTree(vocabulary)
 def test_strings(self):
     tree = PrefixTree()
     input_strings = []  # Strings that have been inserted into the tree
     for string in ['ABC', 'ABD', 'A', 'XYZ']:  # Strings to be inserted
         # Insert new string and add to list of strings already inserted
         tree.insert(string)
         input_strings.append(string)
         # Verify tree can retrieve all strings that have been inserted
         tree_strings = tree.strings()
         assert len(tree_strings) == len(input_strings)  # Check length only
         self.assertCountEqual(tree_strings, input_strings)  # Ignore order
 def test_init_with_string(self):
     tree = PrefixTree(['A'])
     # Verify root node
     assert tree.root.character == PrefixTree.START_CHARACTER
     assert tree.root.is_terminal() is False
     assert tree.root.num_children() == 1
     assert tree.root.has_child('A') is True
     # Verify node 'A'
     node_A = tree.root.get_child('A')
     assert node_A.character == 'A'
     assert node_A.is_terminal() is True
     assert node_A.num_children() == 0
Exemple #12
0
 def test_tedium(self):
     """Test how tedious testing can be"""
     tree = PrefixTree()
     input_strings = ['Anisha Bayti', 'Anisha Bobeesha', 'Anisha Jain', 'Mushu the Cat', 'Mushu the Dragon']
     for ordinal, string in enumerate(input_strings):
         tree.insert(string)
         tree_strings = tree.strings()
         assert len(tree_strings) == ordinal + 1
     assert tree.complete('A') == input_strings[:3]
     assert tree.complete('Anisha') == input_strings[:3]
     assert tree.complete('Anisha J') == [input_strings[2]]
     assert tree.complete('M') == input_strings[3:]
     assert tree.complete('Mushu') == input_strings[3:]
     assert tree.complete('Mushu the D') == [input_strings[4]]
     assert tree.complete('Adriana') == []
Exemple #13
0
 def test_insert_with_string(self):
     tree = PrefixTree()
     tree.insert('AB')
     print(tree.root)
     # Verify root node
     assert tree.root.character == PrefixTree.START_CHARACTER
     assert tree.root.is_terminal() is False
     assert tree.root.num_children() == 1
     assert tree.root.has_child('A') is True
     # Verify node 'A'
     node_A = tree.root.get_child('A')
     assert node_A.character == 'A'
     assert node_A.full_path == "AB"
     assert node_A.is_terminal() is True
     assert node_A.num_children() == 0
     assert node_A.has_child('B') is False
 def test_delete_key_shares_prefix_with_other_strings(self):
     """
     A string is deleted from the trie without removing strings that contain
     the same prefix.
     """
     # Make the tree
     input = ['ABC', 'ABD', 'A', 'XYZ', "WAFFLE TIME"]
     tree = PrefixTree(input)
     # Remove a string who shares letters with other strings
     tree.delete('A')
     # test the length of trie is adjusted correctly
     assert tree.size == len(input) - 1
     # test all the other strings with common nodes can still be found
     assert tree.contains('ABC') is True
     assert tree.contains('ABD') is True
     # test that the deleted string cannot be found
     assert tree.contains('A') is False
 def test_contains(self):
     strings = ['ABC', 'ABD', 'A', 'XYZ']
     tree = PrefixTree(strings)
     # Verify contains for all substrings
     assert tree.contains('ABC') is True
     assert tree.contains('ABD') is True
     assert tree.contains('AB') is False
     assert tree.contains('BC') is False
     assert tree.contains('BD') is False
     assert tree.contains('A') is True
     assert tree.contains('B') is False
     assert tree.contains('C') is False
     assert tree.contains('D') is False
     assert tree.contains('XYZ') is True
     assert tree.contains('XY') is False
     assert tree.contains('YZ') is False
     assert tree.contains('X') is False
     assert tree.contains('Y') is False
     assert tree.contains('Z') is False
Exemple #16
0
 def test_contains(self):
     strings = ['abc', 'abd', 'a', 'xyz']
     tree = PrefixTree(strings)
     # Verify contains for all substrings
     assert tree.contains('abc') is True
     assert tree.contains('abd') is True
     assert tree.contains('ab') is False
     assert tree.contains('bc') is False
     assert tree.contains('bd') is False
     assert tree.contains('a') is True
     assert tree.contains('b') is False
     assert tree.contains('b') is False
     assert tree.contains('d') is False
     assert tree.contains('xyz') is True
     assert tree.contains('xy') is False
     assert tree.contains('yz') is False
     assert tree.contains('x') is False
     assert tree.contains('y') is False
     assert tree.contains('z') is False
Exemple #17
0
 def test_complete(self):
     strings = ['abc', 'abd', 'a', 'xyz']
     tree = PrefixTree(strings)
     # Verify completions for all substrings
     assert tree.complete('abc') == ['abc']
     assert tree.complete('abd') == ['abd']
     assert tree.complete('ab') == ['abc', 'abd']
     assert tree.complete('bc') == []
     assert tree.complete('bd') == []
     assert tree.complete('a') == ['a', 'abc', 'abd']
     assert tree.complete('b') == []
     assert tree.complete('c') == []
     assert tree.complete('d') == []
     assert tree.complete('xyz') == ['xyz']
     assert tree.complete('xy') == ['xyz']
     assert tree.complete('yz') == []
     assert tree.complete('x') == ['xyz']
     assert tree.complete('y') == []
     assert tree.complete('z') == []
Exemple #18
0
 def test_insert_with_string(self):
     tree = PrefixTree()
     tree.insert('AB')
     # Verify root node
     assert tree.root.character == PrefixTree.START_CHARACTER
     assert tree.root.is_terminal() is False
     assert tree.root.num_children() == 2
     assert tree.root.has_child('A') is True
     # Verify node 'A'
     node_A = tree.root.get_child('A')
     assert node_A.character == 'A'
     assert node_A.is_terminal() is False
     assert node_A.num_children() == 1
     assert node_A.has_child('B') is True
     # Verify node 'B'
     node_B = node_A.get_child('B')
     assert node_B.character == 'B'
     assert node_B.is_terminal() is True
     assert node_B.num_children() == 1
     assert node_B.children[0].character == '$'
 def test_size_and_is_empty(self):
     tree = PrefixTree()
     # Verify size after initializing tree
     assert tree.size == 0
     assert tree.is_empty() is True
     # Verify size after first insert
     tree.insert('A')
     assert tree.size == 1
     assert tree.is_empty() is False
     # Verify size after second insert
     tree.insert('ABC')
     assert tree.size == 2
     assert tree.is_empty() is False
     # Verify size after third insert
     tree.insert('ABD')
     assert tree.size == 3
     assert tree.is_empty() is False
     # Verify size after fourth insert
     tree.insert('XYZ')
     assert tree.size == 4
     assert tree.is_empty() is False
Exemple #20
0
 def test_complete(self):
     strings = ['ABC', 'ABD', 'A', 'XYZ']
     tree = PrefixTree(strings)
     # Verify completions for all substrings
     assert tree.complete('ABC') == ['ABC']
     assert tree.complete('ABD') == ['ABD']
     assert tree.complete('AB') == ['ABC', 'ABD']
     assert tree.complete('BC') == []
     assert tree.complete('BD') == []
     assert tree.complete('A') == ['A', 'ABC', 'ABD']
     assert tree.complete('B') == []
     assert tree.complete('C') == []
     assert tree.complete('D') == []
     assert tree.complete('XYZ') == ['XYZ']
     assert tree.complete('XY') == [
         'XYZ'
     ]  # changed test, I believe it was faulty before
     assert tree.complete('YZ') == []
     assert tree.complete('X') == ['XYZ']
     assert tree.complete('Y') == []
     assert tree.complete('Z') == []
 def __init__(self, wordlist):
     self.dix = PrefixTree([(word, None) for word in wordlist])
Exemple #22
0
    def test_insert_with_4_strings(self):
        tree = PrefixTree()
        # Insert new string that starts from root node
        tree.insert('ABC')
        # Verify root node
        assert tree.root.character == PrefixTree.START_CHARACTER
        assert tree.root.is_terminal() is False
        assert tree.root.num_children() == 1
        assert tree.root.has_child('A') is True

        # Changed test to fit structure of Radix tree
        # Verify new node 'A'
        node_A = tree.root.get_child('A')
        assert node_A.character == 'A'
        assert node_A.is_terminal() is True
        assert node_A.num_children() == 0
        assert node_A.has_child('B') is False
        assert node_A.full_path == "ABC"

        # Insert string with partial overlap so node 'B' has new child node 'D'
        tree.insert('ABD')
        # Verify root node again
        assert tree.root.character == PrefixTree.START_CHARACTER
        assert tree.root.is_terminal() is False
        assert tree.root.num_children() == 1
        assert tree.root.has_child('A') is True
        # Verify node 'A' again
        node_A = tree.root.children['A']
        assert node_A.character == 'A'
        assert node_A.is_terminal() is False
        assert node_A.num_children() == 2
        assert node_A.full_path == "AB"
        assert node_A.has_child('B') is False
        assert node_A.has_child('C') is True
        assert node_A.has_child('D') is True
        # Verify new node 'D'
        node_D = node_A.get_child('D')
        assert node_D.character == 'D'
        assert node_D.full_path == "D"
        assert node_D.is_terminal() is True
        assert node_D.num_children() == 0
        node_C = node_A.get_child('C')
        assert node_C.character == 'C'
        assert node_C.full_path == "C"
        assert node_C.is_terminal() is True
        assert node_C.num_children() == 0

        # Insert substring already in tree so node 'A' becomes terminal
        tree.insert('A')
        # Verify root node again
        assert tree.root.character == PrefixTree.START_CHARACTER
        assert tree.root.is_terminal() is False
        assert tree.root.num_children() == 1
        assert tree.root.has_child('A') is True
        # Verify node 'A' again
        node_A = tree.root.children['A']
        assert node_A.character == 'A'
        assert node_A.is_terminal() is True  # Node 'A' is now terminal
        assert node_A.num_children() == 1  # Node 'A' still has one child
        assert node_A.has_child('B') is True  # Node 'B' is still its child

        # Insert new string with no overlap that starts from root node
        tree.insert('XYZ')
        # Verify root node again
        assert tree.root.character == PrefixTree.START_CHARACTER
        assert tree.root.is_terminal() is False
        assert tree.root.num_children() == 2  # Root node now has two children
        assert tree.root.has_child('A') is True  # Node 'A' is still its child
        assert tree.root.has_child('X') is True  # Node 'X' is its new child
        # Verify new node 'X'
        node_X = tree.root.get_child('X')
        assert node_X.character == 'X'
        assert node_X.full_path == "XYZ"
        assert node_X.is_terminal() is True
        assert node_X.num_children() == 0
        assert node_X.has_child('Y') is False
    def test_insert_with_4_strings(self):
        tree = PrefixTree()
        # Insert new string that starts from root node
        tree.insert('ABC')
        # Verify root node
        assert tree.root.character == PrefixTree.START_CHARACTER
        assert tree.root.is_terminal() is False
        assert tree.root.num_children() == 1
        assert tree.root.has_child('A') is True
        # Verify new node 'A'
        node_A = tree.root.get_child('A')
        assert node_A.character == 'A'
        assert node_A.is_terminal() is False
        assert node_A.num_children() == 1
        assert node_A.has_child('B') is True
        # Verify new node 'B'
        node_B = node_A.get_child('B')
        assert node_B.character == 'B'
        assert node_B.is_terminal() is False
        assert node_B.num_children() == 1
        assert node_B.has_child('C') is True
        # Verify new node 'C'
        node_C = node_B.get_child('C')
        assert node_C.character == 'C'
        assert node_C.is_terminal() is True
        assert node_C.num_children() == 0

        # Insert string with partial overlap so node 'B' has new child node 'D'
        tree.insert('ABD')
        # Verify root node again
        assert tree.root.character == PrefixTree.START_CHARACTER
        assert tree.root.is_terminal() is False
        assert tree.root.num_children() == 1
        assert tree.root.has_child('A') is True
        # Verify node 'A' again
        assert node_A.character == 'A'
        assert node_A.is_terminal() is False
        assert node_A.num_children() == 1
        assert node_A.has_child('B') is True
        # Verify node 'B' again
        assert node_B.character == 'B'
        assert node_B.is_terminal() is False
        assert node_B.num_children() == 2  # Node 'B' now has two children
        assert node_B.has_child('C') is True  # Node 'C' is still its child
        assert node_B.has_child('D') is True  # Node 'D' is its new child
        # Verify new node 'D'
        node_D = node_B.get_child('D')
        assert node_D.character == 'D'
        assert node_D.is_terminal() is True
        assert node_D.num_children() == 0

        # Insert substring already in tree so node 'A' becomes terminal
        tree.insert('A')
        # Verify root node again
        assert tree.root.character == PrefixTree.START_CHARACTER
        assert tree.root.is_terminal() is False
        assert tree.root.num_children() == 1
        assert tree.root.has_child('A') is True
        # Verify node 'A' again
        assert node_A.character == 'A'
        assert node_A.is_terminal() is True  # Node 'A' is now terminal
        assert node_A.num_children() == 1  # Node 'A' still has one child
        assert node_A.has_child('B') is True  # Node 'B' is still its child

        # Insert new string with no overlap that starts from root node
        tree.insert('XYZ')
        # Verify root node again
        assert tree.root.character == PrefixTree.START_CHARACTER
        assert tree.root.is_terminal() is False
        assert tree.root.num_children() == 2  # Root node now has two children
        assert tree.root.has_child('A') is True  # Node 'A' is still its child
        assert tree.root.has_child('X') is True  # Node 'X' is its new child
        # Verify new node 'X'
        node_X = tree.root.get_child('X')
        assert node_X.character == 'X'
        assert node_X.is_terminal() is False
        assert node_X.num_children() == 1
        assert node_X.has_child('Y') is True
        # Verify new node 'Y'
        node_Y = node_X.get_child('Y')
        assert node_Y.character == 'Y'
        assert node_Y.is_terminal() is False
        assert node_Y.num_children() == 1
        assert node_Y.has_child('Z') is True
        # Verify new node 'Z'
        node_Z = node_Y.get_child('Z')
        assert node_Z.character == 'Z'
        assert node_Z.is_terminal() is True
        assert node_Z.num_children() == 0
Exemple #24
0
    # for w2 in mvDict['ford']:
    #     if testWord(w2):
    #         d["ford " + str(w2)] = mvDict['ford'][w2]["variance"]
    # sf = [(k, d[k]) for k in sorted(d, key=d.get, reverse=True)]
    # print(sf)

    # # get Zaphod
    # d = {}
    # for w2 in mvDict['zaphod']:
    #     if testWord(w2):
    #         d["zaphod " + str(w2)] = mvDict['zaphod'][w2]["variance"]
    # sz = [(k, d[k]) for k in sorted(d, key=d.get, reverse=True)]
    # print(sz)

    start = time.time()
    pt = PrefixTree(wordsDict=mvDict)
    pt.buildPrefixTree()
    stop = time.time()
    print("Mean-Variance build prefix tree time (s):", (stop - start))
    print(pt.getRecommendations("k"))

    # save prefix tree build with mean-variance to file
    pt.saveToFile("mvtree.json")

    # Chi-Squared 
    print("********************************")
    print("*          Chi-Squared         *")
    print("********************************")

    start = time.time()