-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.py
48 lines (42 loc) · 1.22 KB
/
context.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
import commonsense
class Context:
lastwords = []
related = {}
def __init__(self):
self.lastwords = []
self.related = {}
def addWord(self,word,pos=0):
word = word.lower()
if word not in self.related:
if len(word) >= 3:
self.related[word] = commonsense.relatedWords(word)
else:
self.related[word] = []
if pos:
self.lastwords.reverse()
self.lastwords.pop(pos)
self.lastwords.insert(pos,word)
self.lastwords.reverse()
return
self.lastwords.insert(pos,word)
def __shift(self, seqo, n):
seq = list(seqo)
seq.reverse()
if n:
n = n % len(seq)
l1 = seq[:n]
l1.reverse()
l2 = seq[n:]
return l1 + l2
else:
return seq
def getBestWord(self,words, pos = 0):
last = list(self.lastwords)
last = self.__shift(last,pos)
for cword in last:
for word in words:
if word in last or word in self.related[cword]:
return word
return words[0]
def printContext(self):
print self.related