-
Notifications
You must be signed in to change notification settings - Fork 93
/
model.py
115 lines (95 loc) · 3.48 KB
/
model.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import dictionary as d
import richWord as rw
class MultiDictionary:
def __init__(self):
self._english = d.Dictionary([], "english")
self._italian = d.Dictionary([], "italian")
self._spanish = d.Dictionary([], "spanish")
self._english.loadDictionary("resources/English.txt")
self._italian.loadDictionary("resources/Italian.txt")
self._spanish.loadDictionary("resources/Spanish.txt")
def printDic(self, language):
if language == "english":
self._english.printAll()
elif language == "italian":
self._italian.printAll()
elif language == "spanish":
self._spanish.printAll()
else:
print("Language not supported")
def searchWord(self, words, language):
# words is a list of strings
parole = []
for word in words:
word = word.lower()
found = False
richW = rw.RichWord(word)
if language == "english":
if self._english.dict.__contains__(word):
found = True
elif language == "italian":
if self._italian.dict.__contains__(word):
found = True
elif language == "spanish":
if self._spanish.dict.__contains__(word):
found = True
if (found):
richW.corretta = True
parole.append(richW)
return parole
def searchWordLinear(self, words, language):
# words is a list of strings
parole = []
for word in words:
word = word.lower()
found = False
richW = rw.RichWord(word)
if language == "english":
for entry in self._english.dict:
if entry == word:
found = True
elif language == "italian":
for entry in self._italian.dict:
if entry == word:
found = True
elif language == "spanish":
for entry in self._spanish.dict:
if entry == word:
found = True
if (found):
richW.corretta = True
parole.append(richW)
return parole
def searchWordDichotomic(self, words, language):
# words is a list of strings
parole = []
for word in words:
word = word.lower()
found = False
richW = rw.RichWord(word)
if language == "english":
currentDic = self._english.dict
found = dichotomicSearch(word, currentDic)
elif language == "italian":
currentDic = self._italian.dict
found = dichotomicSearch(word, currentDic)
elif language == "spanish":
currentDic = self._spanish.dict
found = dichotomicSearch(word, currentDic)
if (found):
richW.corretta = True
parole.append(richW)
return parole
def dichotomicSearch(word, currentDic):
start = 0
end = len(currentDic)
while (start != end):
mean = start + int((end - start)/2)
currentW = currentDic[mean]
if word == currentW:
return True
elif word > currentW: # in python < applied to strings gives True if the first string is before in lexicographic order
start = mean+1
else:
end = mean
return False