-
Notifications
You must be signed in to change notification settings - Fork 1
/
word_predict.py
52 lines (46 loc) · 1.74 KB
/
word_predict.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
import re
class WordPredict():
def __init__(self):
self.memory = {}
def get_next_word(self, sentence, word):
next_word_index = sentence.index(word) + 1
if len(sentence) == next_word_index:
next_word = None
else:
next_word = sentence[next_word_index]
return next_word
def learn(self, sentence):
temp_sentence = [w.lower() for w in sentence]
for word in temp_sentence:
temp_word = word.lower()
if temp_word in self.memory:
next_word = self.get_next_word(temp_sentence, temp_word)
if next_word in self.memory[temp_word].keys():
self.memory[temp_word][next_word] += 1
else:
self.memory[temp_word][next_word] = 1
else:
next_word = self.get_next_word(temp_sentence, temp_word)
self.memory[temp_word] = {next_word: 1}
print "learned a new sentence"
def predict(self, word):
temp_word = word.lower()
dictionary = self.memory[temp_word]
next_word = max(dictionary)
if next_word == None:
next_word = ''
print "{0} : {1}".format(word, next_word)
if __name__ == '__main__':
word_predict = WordPredict()
input_file = open('input.txt')
for line in input_file.readlines():
method = line.split()[0].lower()
sentence = line.split()[1:]
sentence = re.sub('[!,.]', '', sentence)
if method == 'learn':
learn = getattr(word_predict, method)
learn(sentence)
elif method == 'predict':
predict = getattr(word_predict, method)
word = ' '.join(line.split()[1:])
predict(word)