-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsuggestions.py
84 lines (74 loc) · 2.25 KB
/
suggestions.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
import os
class Hinter:
'''
Hinter is used to load a dictionary and obtain some suggestions
regarding the next possible letters or compatible words.
'''
def __init__(self, words):
self.words = words
@staticmethod
def load_english_dict():
'''
Loads the english dictionary and returns a Hinter object with
the words loaded into the self.words list
'''
ENGLISH_FILENAME = "dict" + os.sep + "english.txt"
words = [i.replace("\n","") for i in open(ENGLISH_FILENAME)]
return Hinter(words)
def compatible_words(self, word, limit = 10):
'''
Returns the words that starts with the "word" parameter.
The "limit" parameter defines how many words the function
returns at maximum
'''
output = []
word_count = 0
#Cycle through all the words to find the ones that starts with "word"
for i in self.words:
if i.startswith(word):
output.append(i)
word_count+=1
if word_count>=limit: #If limit is reached, exit
break
return output
def next_letters(self, word):
'''
Returns a list of compatible letters.
A letter is compatible when there are words that starts with "word"
and are followed by the letter.
'''
#Get 100 compatible words
words = self.compatible_words(word, 100)
letters = []
#Cycle through all the compatible words
for i in words:
if len(i)>len(word): #if the "word" is longer than a compatible word, skip
letter = i[len(word):len(word)+1] #Get the following letter
if not letter in letters: #Avoid duplicates
letters.append(letter)
return letters
def does_word_exists(self, word):
'''
Check if a specific word exists in the loaded dictionary
'''
if word in self.words:
return True
else:
return False
def most_probable_letter(self, clf, classes, linearized_sample, word):
'''
Get the most probable letter for a given recorded sign and the current word
'''
if word=="":
return None
probabilities = clf.predict_log_proba(linearized_sample)
ordered = sorted(classes)
values = {}
for i in xrange(len(probabilities[0])):
values[round(probabilities[0,i], 5)] = classes[ordered[i]]
ordered = sorted(values, reverse=True)
possible_letters = self.next_letters(word)
for i in ordered:
if values[i] in possible_letters:
return values[i]
return None