forked from teamtachyon/Quillpad-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuillSourceProcessor.py
executable file
·359 lines (285 loc) · 14.4 KB
/
QuillSourceProcessor.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# -*- coding: utf-8 -*-
# @Date : Jul 13, 2016
# @Author : Ram Prakash, Sharath Puranik
# @Version : 1
import QuillLanguage as qlang
import QuillEngXlit as xlit
import re
import const
import primaryHelper
class QuillSourceProcessor(object):
def __init__(self):
useCCart=True
bengaliDefFile='Bengali_Vrinda.xml'
bengaliKnowledgeInput='bengali'
gujaratiDefFile='Gujarati_Shruti.xml'
gujaratiKnowledgeInput='gujarati'
hindiDefFile='Hindi_Mangal.xml'
hindiKnowledgeInput='hindi'
hindiMobileDefFile='Hindi_Mangal_Mobile.xml'
hindiMobileKnowledgeInput='hindiMobile'
kannadaDefFile='Kannada_Tunga.xml'
kannadaKnowledgeInput='kannada'
kannadaMobileDefFile='Kannada_Tunga_Mobile.xml'
kannadaMobileKnowledgeInput='kannada_list_mobile.txt'
malayalamDefFile='Malayalam_Kartika.xml'
malayalamKnowledgeInput='malayalam'
malayalamMobileDefFile='Malayalam_Kartika_Mobile.xml'
malayalamMobileKnowledgeInput='malayalam_list_mobile.txt'
marathiDefFile='Marathi_Mangal.xml'
marathiKnowledgeInput='marathi'
marathiMobileDefFile='Marathi_Mangal_Mobile.xml'
marathiMobileKnowledgeInput='marathi_list_mobile.txt'
nepaliDefFile='Nepali_Mangal.xml'
nepaliKnowledgeInput='nepali'
punjabiDefFile='Punjabi_Raavi.xml'
punjabiKnowledgeInput='punjabi'
tamilDefFile='Tamil_Latha.xml'
tamilKnowledgeInput='tamil'
tamilMobileDefFile='Tamil_Latha_Mobile.xml'
tamilMobileKnowledgeInput='tamil_list_mobile.txt'
teluguDefFile='Telugu_Raavi.xml'
teluguKnowledgeInput='telugu'
teluguMobileDefFile='Telugu_Raavi_Mobile.xml'
teluguMobileKnowledgeInput='telugu_list_mobile.txt'
self.scriptEngines = {'english':None,
'bengali':qlang.QuillLanguage(bengaliDefFile,bengaliKnowledgeInput,useCCart),
'gujarati':qlang.QuillLanguage(gujaratiDefFile,gujaratiKnowledgeInput,useCCart),
'hindi':qlang.QuillLanguage(hindiDefFile,hindiKnowledgeInput,useCCart),
#'hindiMobile':qlang.QuillLanguage(hindiMobileDefFile,hindiMobileKnowledgeInput,useCCart),
'kannada':qlang.QuillLanguage(kannadaDefFile,kannadaKnowledgeInput,useCCart),
#'kannadaMobile':qlang.QuillLanguage(kannadaMobileDefFile,kannadaMobileKnowledgeInput,useCCart),
'malayalam':qlang.QuillLanguage(malayalamDefFile,malayalamKnowledgeInput,useCCart),
#'malayalamMobile':qlang.QuillLanguage(malayalamMobileDefFile,malayalamMobileKnowledgeInput,useCCart),
'marathi':qlang.QuillLanguage(marathiDefFile,marathiKnowledgeInput,useCCart),
#'marathiMobile':qlang.QuillLanguage(marathiMobileDefFile,marathiMobileKnowledgeInput,useCCart),
'nepali':qlang.QuillLanguage(nepaliDefFile,nepaliKnowledgeInput,useCCart),
'punjabi':qlang.QuillLanguage(punjabiDefFile,punjabiKnowledgeInput,useCCart),
'tamil':qlang.QuillLanguage(tamilDefFile,tamilKnowledgeInput,useCCart),
#'tamilMobile':qlang.QuillLanguage(tamilMobileDefFile,tamilMobileKnowledgeInput,useCCart),
'telugu':qlang.QuillLanguage(teluguDefFile,teluguKnowledgeInput,useCCart),
#'teluguMobile':qlang.QuillLanguage(teluguMobileDefFile,teluguMobileKnowledgeInput,useCCart)
}
self.xlitEngines = {
'kannada': xlit.QuillEngXliterator('EnglishPronouncingTrees','IndianPronouncingTrees','Kannada_Xlit.xml'),
'bengali': xlit.QuillEngXliterator('EnglishPronouncingTrees','IndianPronouncingTrees','Bengali_Xlit.xml'),
'gujarati': xlit.QuillEngXliterator('EnglishPronouncingTrees','IndianPronouncingTrees','Gujarati_Xlit.xml'),
'hindi': xlit.QuillEngXliterator('EnglishPronouncingTrees','IndianPronouncingTrees','Hindi_Xlit.xml'),
'marathi': xlit.QuillEngXliterator('EnglishPronouncingTrees','IndianPronouncingTrees','Marathi_Xlit.xml'),
'nepali': xlit.QuillEngXliterator('EnglishPronouncingTrees','IndianPronouncingTrees','Nepali_Xlit.xml'),
'punjabi': xlit.QuillEngXliterator('EnglishPronouncingTrees','IndianPronouncingTrees','Punjabi_Xlit.xml'),
'telugu': xlit.QuillEngXliterator('EnglishPronouncingTrees','IndianPronouncingTrees','Telugu_Xlit.xml'),
'tamil': xlit.QuillEngXliterator('EnglishPronouncingTrees','IndianPronouncingTrees','Tamil_Xlit.xml'),
'malayalam': xlit.QuillEngXliterator('EnglishPronouncingTrees','IndianPronouncingTrees','Malayalam_Xlit.xml')
}
self.clashMaps = {
'bengali': self.makeClashMap('bengaliClashList.txt'),
'gujarati': self.makeClashMap('gujaratiClash.txt'),
'hindi': self.makeClashMap('hindiClash.txt'),
'kannada': self.makeClashMap('kannadaClash.txt'),
'tamil': self.makeClashMap('tamilClash.txt'),
'marathi': self.makeClashMap('marathiClash.txt'),
'nepali': self.makeClashMap('nepaliClash.txt'),
'punjabi': self.makeClashMap('punjabiClash.txt'),
'telugu': self.makeClashMap('teluguClash.txt'),
'malayalam': self.makeClashMap('malayalamClash.txt')
}
self.modeTypes = ['predictive','xliterate','itrans']
self.inputBuffer =''
self.outputBuffer=''
self.scriptCommandRE = r"(?<!\\)\\(english|bengali|gujarati|hindi|hindiMobile|kannada|kannadaMobile|malayalam|malayalamMobile|marathi|marathiMobile|nepali|punjabi|tamil|tamilMobile|telugu|teluguMobile)" #starts with alpha followed alpha-numerics
self.modeCommandRE = r"(?<!\\)\\(predictive|xliterate|itrans){((?:\\{|[^{}\\]|\\}|\\)*)}"
self.compSC = re.compile(self.scriptCommandRE)
self.compMC = re.compile(self.modeCommandRE)
self.currLanguage = 'english'
self.currMode = 'predictive'
self.engine = None
self.loadEnglishDict('dict.txt')
def loadEnglishDict(self, fname):
words = open(fname).read().split()
self.engWords = dict([(w, None) for w in words])
print "Loaded english dictionary from...", fname
def makeClashMap(self, fname):
words = open(fname).read().split()
return dict([(w, None) for w in words])
def processText(self,inString, onlyFirstOptions=False):
self.inputBuffer = inString
self.outputBuffer = ''
index = 0
langText=''
while index < len(self.inputBuffer):
scriptCmdMatch = self.compSC.match(self.inputBuffer,index)
modeCmdMatch = self.compMC.match(self.inputBuffer,index)
if scriptCmdMatch != None:
self.outputBuffer += self.renderText(langText)
langText = ''
self.currLanguage = scriptCmdMatch.group(1)
self.switchLanguage(self.currLanguage)
index = scriptCmdMatch.end()
elif modeCmdMatch != None and self.currLanguage != 'english':
self.outputBuffer += self.renderText(langText)
langText = ''
mode = modeCmdMatch.group(1)
text = modeCmdMatch.group(2)
self.switchMode(mode)
self.outputBuffer += self.renderText(text)
self.switchMode('predictive')
index = modeCmdMatch.end()
else:
langText += self.inputBuffer[index]
index +=1
self.outputBuffer += self.renderText(langText, onlyFirstOptions)
return self.outputBuffer
def switchMode(self,mode):
self.currMode = mode
def renderText(self,langText, onlyFirstOptions=False):
index = 0
insideWord = False
renderedText = ''
currWord = ''
if self.engine == None:
return langText
if self.currMode == 'predictive' and (not onlyFirstOptions):
convertedList = self.engine.convert(langText,"predictive", True)
if len(convertedList) == 1:
onlyTuple = convertedList[0]
if type(onlyTuple[0]) == str:
renderedText = onlyTuple[0]
else:
renderedText = const.optionSeperator.join(onlyTuple[0])
else :
renderedText += '----multiple----\n'
for (ustr, count) in convertedList:
if type(ustr) == str:
#some char like ,.-' etc..
renderedText += str(ustr) + "\n"
else:
renderedText += const.langWordMark + str(const.optionSeperator).join(ustr) + "\n";
elif self.currMode == 'predictive' and onlyFirstOptions:
convertedList = self.engine.convert(langText,"predictive", True)
for (ustr, count) in convertedList:
if type(ustr) == str:
renderedText += str(ustr)
else:
renderedText += ustr[0]
elif self.currMode == 'itrans':
convertedList = self.engine.convert(langText,"primary")
for (uStr,count) in convertedList:
for s in uStr :
renderedText += s
elif self.currMode == 'xliterate':
renderedText = langText
return renderedText
def switchLanguage(self,script):
if self.scriptEngines.has_key(script):
self.engine = self.scriptEngines[script]
else:
self.engine = None
def xlit(self, inString, lang):
if lang in self.xlitEngines:
inString = inString.lower()
engine = self.xlitEngines[lang]
return {'xlitWords': engine.xliterate(inString)}
else:
return {'xlitWords': [inString]}
def processString(self, inString, lang):
def transliterate(word):
if re.search("[a-zA-Z]+", word):
return self.processWord(word, lang)["twords"][0]["options"][0]
return word
words = map(lambda x: x[0], re.findall("(([a-zA-Z]+)|([^a-zA-Z])+)", inString))
return "".join(map(transliterate, words))
def processReverseWord(self, uStr, lang):
if self.scriptEngines.has_key(lang):
engine = self.scriptEngines[lang]
trainTuples = engine.getTrainingTuples(uStr)
literals = [''.join(lit) for (lit,c,flags) in trainTuples]
return literals
else:
return []
def processWord(self, inString, lang):
response = {"inString": inString, "twords": []}
inString = inString.lower()
if self.scriptEngines.has_key(lang):
engine = self.scriptEngines[lang]
else:
# We don't support the language
response["twords"].append({
"word": True,
"options": [inString],
"optmap": {inString: inString.split()}
})
return response
convertedList, numOptions = engine.literalToUnicode(inString,
"predictive", True)
options = []
optmap = {}
for litList in convertedList:
options.append("".join(litList))
optmap["".join(litList)] = litList
# options = ["".join([l[0] for l in litList]) for litList in convertedList]
def dictSort(dlang, arr):
a1 = []
a2 = []
for i in arr:
if primaryHelper.isDictWord(dlang, i):
a1.append(i)
else:
a2.append(i)
return a1 + a2
if (lang=="hindiMobile") or (lang=="hindi"):
options = dictSort("hindi", options)
else :
options = dictSort(lang, options)
def isNotITRANS(word):
for i in word:
if i in ".~^/":
return False
return True
def isNotDigit(word):
for i in word:
if i in "0123456789":
return False
return True
if lang in self.xlitEngines and isNotITRANS(inString) and isNotDigit(inString):
xlitWords = self.xlitEngines[lang].xliterate(inString)
if len(xlitWords) > 0 and len(xlitWords[0]) > 0:
xlitWord = xlitWords[0]
if inString in self.engWords:
if inString in self.clashMaps[lang]:
if xlitWord not in options[:4]:
options = options[:1] + [xlitWord] + options[1:]
else:
if xlitWord in options:
options.remove(xlitWord)
options = [xlitWord] + options
else:
if xlitWord not in options[:4]:
options = options[:3] + [xlitWord] + options[3:]
response["twords"].append({
"word": True,
"options": options,
"optmap": optmap
})
return response
def getCorrections(self, lang, currWord, userInput, pos):
if self.scriptEngines.has_key(lang):
engine = self.scriptEngines[lang]
else:
return ["".join(currWord)]
return engine.getCorrections(currWord, userInput, pos)
def getCorrectionsStr(self, lang, currWord, userInput, pos):
if self.scriptEngines.has_key(lang):
engine = self.scriptEngines[lang]
else:
return currWord
return engine.getCorrectionsStr(currWord, userInput, pos)
if __name__ == '__main__':
inString = "raja-deepthi"
proc = QuillSourceProcessor()
proc.switchLanguage("hindi")
out = proc.processText(inString);
f = open('out.txt','w')
utext= out.encode('utf-8')
f.write(utext)
f.close()