-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslatetext.py
179 lines (145 loc) · 6.35 KB
/
translatetext.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import operator
class TrieNode(object):
def __init__(self, char):
self.char = char
self.children = []
self.wordFinished = False
class TextPredictionAPI(object):
def __init__(self):
self.unigramMap = dict()
self.bigramMap = dict()
self.trigramMap = dict()
self.ngramMap = dict()
self.nextWordsListMap = dict()
self.root = TrieNode('*')
self.corpusArray = self.getStringArrayFromCorpus('Big.txt')
self.makeUnigramMap()
self.makeBigramMap()
self.makeTrigramMap()
self.makeNgramMap()
self.makeNextWordsListMap()
self.addWordsToTrie()
self.wordList = []
def getStringArrayFromCorpus(self, fileName):
with open(fileName, 'r') as myfile:
data = myfile.read().replace('\n', ' ')
return data.split(' ')
def makeUnigramMap(self):
for string in self.corpusArray:
self.unigramMap[string] = self.unigramMap.get(string, 0) + 1
def makeBigramMap(self):
size = len(self.corpusArray)
for i in range(size - 1):
string = self.corpusArray[i] + ' ' + self.corpusArray[i + 1]
self.bigramMap[string] = self.bigramMap.get(string, 0) + 1
def makeTrigramMap(self):
size = len(self.corpusArray)
for i in range(size - 2):
string = self.corpusArray[i] + ' ' + self.corpusArray[i + 1] + ' ' + self.corpusArray[i + 2]
self.trigramMap[string] = self.trigramMap.get(string, 0) + 1
def makeNgramMap(self):
size = len(self.corpusArray)
for i in range(size - 3):
string = self.corpusArray[i] + ' ' + self.corpusArray[i + 1] + ' ' + self.corpusArray[i + 2] + ' ' + self.corpusArray[i + 3]
self.ngramMap[string] = self.ngramMap.get(string, 0) + 1
def makeNextWordsListMap(self):
for string in self.corpusArray:
self.nextWordsListMap[string] = set()
size = len(self.corpusArray)
for i in range(size - 1):
self.nextWordsListMap[self.corpusArray[i]].add(self.corpusArray[i + 1])
def addWordsToTrie(self):
size = len(self.corpusArray)
for i in range(size):
word = str(self.corpusArray[i])
node = self.root
for char in word:
charFound = False
for child in node.children:
if char == child.char:
node = child
charFound = True
break
if not charFound:
newNode = TrieNode(char)
node.children.append(newNode)
node = newNode
node.wordFinished = True
def getLastWordFromSentence(self, screenText):
screenTextArray = screenText.split()
size = len(screenTextArray)
return screenTextArray[size - 1]
def getLargestCommonPrefix(self, root, word):
node = root
prefix = ""
for char in word:
charFound = False
for child in node.children:
if char == child.char:
node = child
charFound = True
prefix = prefix + child.char
break
if not charFound:
return self.DFSOnTrie(node, prefix)
return self.DFSOnTrie(node, prefix)
def DFSOnTrie(self, node, prefixNow):
if node.wordFinished:
self.wordList.append(prefixNow)
for child in node.children:
self.DFSOnTrie(child, prefixNow + child.char)
def calculateProbability(self, num, den):
return float(float(num) / float(den))
def clearWordList(self):
self.wordList = []
def nextWordPrediction(self, screenText):
screenTextArray = screenText.split()
size = len(screenTextArray)
probabilityMap = dict()
if(size == 0):
for key, value in self.unigramMap.items():
probabilityMap[str(key)] = float(value)
elif(size == 1):
denString = screenTextArray[size - 1]
lastWord = screenTextArray[size - 1]
den = float(self.unigramMap.get(denString))
for numString in self.nextWordsListMap[lastWord]:
search = denString + ' ' + numString
num = float(self.bigramMap.get(search, 0) if self.bigramMap.get(search) is not None else 0)
value = self.calculateProbability(num, den)
probabilityMap[numString] = value
elif(size == 2):
denString = screenTextArray[size - 2] + ' ' + screenTextArray[size - 1]
lastWord = screenTextArray[size - 1]
den = float(self.bigramMap.get(denString))
for numString in self.nextWordsListMap[lastWord]:
search = denString + ' ' + numString
num = float(self.trigramMap.get(search, 0) if self.trigramMap.get(search) is not None else 0)
value = self.calculateProbability(num, den)
probabilityMap[numString] = value
else:
denString = screenTextArray[size - 3] + ' ' + screenTextArray[size - 2] + ' ' + screenTextArray[size - 1]
lastWord = screenTextArray[size - 1]
den = float(self.trigramMap.get(denString))
for numString in self.nextWordsListMap[lastWord]:
search = denString + ' ' + numString
num = float(self.ngramMap.get(search, 0) if self.ngramMap.get(search) is not None else 0)
value = self.calculateProbability(num, den)
probabilityMap[numString] = value
sortedProbabilityMap = sorted(probabilityMap.items(), key=operator.itemgetter(1), reverse=True)
topWords = []
for key, value in sortedProbabilityMap[:3]:
topWords.append(key)
return topWords
def wordCompletion(self, screenText):
self.clearWordList()
lastWord = self.getLastWordFromSentence(screenText)
self.getLargestCommonPrefix(self.root, lastWord)
frequencyMap = dict()
for word in self.wordList:
frequencyMap[word] = self.unigramMap[word]
sortedFrequencyMap = sorted(frequencyMap.items(), key=operator.itemgetter(1), reverse=True)
topWords = []
for key, value in sortedFrequencyMap[:3]:
topWords.append(key)
return topWords