-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
93 lines (82 loc) · 3.49 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from Trie import Trie
from DBController import DBController
import sys
def runSimulation(trie,db):
"""
This function acts as a interface for the user to interact with the program.
Parameters
-------------
trie - Trie object
db - dbController object
Return
-------------
None
"""
print("Welcome to the word autocompleter!")
while True:
print("")
print("To interact with the program simply send a message with the corresponding number of the activity you wish to start.")
print(f"1. Search the trie for words proceeding a given word. MaxDepth currently {'not set.' if trie.maxDepth == -1 else 'set at '+str(trie.maxDepth)+ ' characters.'}")
print("2. Check if a word is in the trie or not.")
print("3. Insert a new word into the trie.")
print("4. Change amount of characters to find proceeding given word.")
print("5. Remove a word from the trie.")
print("6. To quit the program.")
while True:
try:
answer = int(input("Enter value: "))
if answer<=6 and answer>0:
break
except Exception:
print("Invalid input!")
#Complete necessary task following user input.
if answer == 1:
answer = input("Enter word you wish to use: ")
words = []
wordsList = sorted(trie.findCandidates(answer,words,trie.maxDepth))
if len(wordsList) != 0:
print("Words that follow the given word are: ")
for i,word in enumerate(wordsList,1):
print(f"{i}. {word.title()}")
else:
print("No words can be found in the trie!")
elif answer == 2:
answer = input("Enter word you wish to search for: ")
result = trie.containsWord(answer)
print(f"The word {answer} is {'NOT in'if result == False else 'in'} the trie.")
elif answer == 3:
answer = input("Enter word you wish to enter into the trie: ")
if trie.containsWord(answer) == False:
result = db.addWord(answer)
if result:
trie.insertWord(answer)
print(f"{answer} has been added to the trie!")
else:
print(f"{answer} is not valid to be entered to the trie!")
else:
print(f"{answer} is already in the trie!")
elif answer == 4:
while True:
try:
print("*Value of -1 means there is no limit.*")
answer = int(input("Enter the value you wish to use: "))
trie.setMaxDepth(answer)
print()
break
except:
print("Invalid input, please enter a value!")
elif answer == 5:
answer = input("Enter word you wish to remove from the trie: ")
if db.deleteWord(answer):
trie.deleteWord(answer)
print(f"{answer} has been removed from the trie.")
else:
print(f"{answer} has NOT been removed from the trie.")
elif answer == 6:
sys.exit()
if __name__ == "__main__":
trie = Trie()
db = DBController()
words = db.getWords()
trie.buildTrie(words)
runSimulation(trie,db)