forked from teamtachyon/Quillpad-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuillTrainer.py
executable file
·131 lines (105 loc) · 4.86 KB
/
QuillTrainer.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
# -*- coding: utf-8 -*-
# @Date : Jul 13, 2016
# @Author : Ram Prakash, Sharath Puranik
# @Version : 1
import CART
from QuillLanguage import QuillLanguage
import pickle
class QuillTrainer(object):
def __init__(self,quillLang):
if isinstance(quillLang,QuillLanguage):
self.language = quillLang
else:
raise Exception,'Invalid parameter. Not of type QuillLanguage'
def train(self,uWords,scope=4,splRulesFlag=True):
self.language.setKnowledge(self.__buildKeyToCARTMap(uWords,scope,splRulesFlag,"primary"),"primary")
self.language.setKnowledge(self.__buildKeyToCARTMap(uWords,scope,splRulesFlag,"predictive"),"predictive")
return self.language
def getLanguage(self):
return self.language
def store(self,fname=None):
if fname == None:
fname = self.language.language+'.qil'
keyToCARTMap = self.language.keyToCARTMap
keyToCARTMapPrimary = self.language.keyToCARTMapPrimary
f = file(fname,'w')
f.write('<QuillLanguage lang="%s" script="%s" deffont="%s" epsilon="%s">\n'%(self.language.language,self.language.script,self.language.default_font,self.language.epsilon.encode('utf-8')))
for (key,keyCart) in keyToCARTMap.items():
keyCart.storeCart(f,"predictive")
for (key,keyCart) in keyToCARTMapPrimary.items():
keyCart.storeCart(f,"primary")
f.write('</QuillLanguage>')
f.close()
def load(self, trainedData):
pass
def __buildKeyToCARTMap ( self, uWords,scope=4,splRulesFlag=True,type="predictive" ):
contextLen = scope
splRules = []
if splRulesFlag == True:
splRules = self.language.getSpecialRules(type)
keyToCARTMap = {}
data={}
for uWord in uWords:
try:
trainPairs = self.language.getTrainingPairs(uWord,type)
except KeyError:
trainPairs = None
if trainPairs != None:
data1 = CART.CART.prepareTrainingData(trainPairs,contextLen,1)
for key in data1.keys():
if data.has_key(key):
data[key].extend( data1[key] )
else:
data.update({key:data1[key]})
if type == "primary":
contextPrefOrder = [0,1,2,-1,3,-2,4,-3-4]
elif type == "predictive":
contextPrefOrder = None
for key in data.keys():
keyCart = CART.CART(key,data[key],contextLen, splRules,contextPrefOrder)
keyCart.build()
keyToCARTMap.update( {key:keyCart } )
return keyToCARTMap
def createTrainingData( self, uWords,scope=4,splRulesType='predictive',fname = None ):
contextLen = scope
splRules = []
if splRulesType != None:
splRules = self.language.getSpecialRules(splRulesType)
if fname == None:
fname = self.language.language+'.data'
f = file(fname,'w')
f.write('<QuillTrainData lang="%s" script="%s" deffont="%s" epsilon="%s" context-len="%s">\n'%(self.language.language,self.language.script,self.language.default_font,self.language.epsilon.encode('utf-8'),scope))
f.write('\t<SpecialRules>\n')
for eachRule in splRules:
f.write('\t\t<SpecialRule>')
f.write(repr(eachRule))
f.write('</SpecialRule>')
f.write('\n')
f.write('\t\t</SpecialRules>\n')
keyToCARTMap = {}
data={}
for uWord in uWords:
try:
trainPairs = self.language.getTrainingPairs(uWord)
except KeyError:
trainPairs = None
if trainPairs != None:
data1 = CART.CART.prepareTrainingData(trainPairs,contextLen,1)
for key in data1.keys():
if data.has_key(key):
data[key].extend( data1[key] )
else:
data.update({key:data1[key]})
for key in data.keys():
keyData = data[key];
f.write('\t<QuillWordList key="%s">\n'%key)
for cWord in keyData:
f.write('\t\t<QuillWord>\n')
f.write('\t\t\t<Literal>%s</Literal>\n'%cWord.word)
f.write('\t\t\t<Focus>%s</Focus>\n'%cWord.focus)
f.write('\t\t\t<ClassAssign>%s</ClassAssign>\n'%cWord.classID.encode('utf-8'))
f.write('\t\t\t<Count>%s</Count>\n'%cWord.count)
f.write('\t\t</QuillWord>\n')
f.write('\t</QuillWordList>\n')
f.write('</QuillTrainData>\n')
f.close()