-
Notifications
You must be signed in to change notification settings - Fork 1
/
fVectors.py
371 lines (325 loc) · 9.38 KB
/
fVectors.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
360
361
362
363
364
365
366
367
368
369
370
#MT Final project 4/29/2012 Johns Hopkins University
#Functions that take input sentences and return feature vectors
#vectors can then be used to compare words in the data
#and build a dictionary of words with similar features
#==============================================================
import sys
import math
import cPickle as pickle
import time
class fVectors:
lang=''
# Constructor
def __init__(self,l):
self.lang=l
self.vector={}
self.totalTokens=0
self.totals={}
self.legend=[]
def buildVector(self, sentence):
# get context vector
cvect = self.context(sentence)
# get ortho vector
# ovect = self.ortho(sentence)
# merge into self.vectors
for word in cvect:
if word not in self.vector:
self.vector[word] = cvect[word]
else:
for mword in cvect[word].keys():
if mword not in self.vector[word]:
self.vector[word][mword] = cvect[word][mword]
else:
self.vector[word][mword] = self.vector[word][mword]+cvect[word][mword]
def dumpVec(self):
#print self.vector
for k in self.vector:
print k
for w in self.vector[k]:
print "\t",w,self.vector[k][w]
print "TotalTokens:",self.totalTokens
for k in self.totals:
print k, self.totals[k]
#Takes a sentence as input
#Returns a dictionary with unique words in the sentence as keys
#and dictionaries for their values
#where the dictionaries contain the +/- 2 context words as keys
#and the number of times those words occur as values
def context(self,sentence):
s=sentence.split(" ")
length = len(s)
wordlist = {}
cvector = {}
r=3
for n,word in enumerate(s):
# keep track of words
if word in self.totals:
self.totals[word]+=1
else:
self.totals[word]=1
self.totalTokens+=1
if word in cvector:
wordlist = cvector[word]
for i in range(-r,r+1):
#print n+i, i
if n+i >= 0 and n+i < length and i != 0:
tmp=s[n+i]
if len(wordlist)==0:
wordlist[str(i+3)+"_"+tmp] = 1
else:
if tmp in wordlist:
value = wordlist[str(i+3)+"_"+tmp]
value = value + 1
wordlist[str(i+3)+"_"+tmp] = value
else:
wordlist[str(i+3)+"_"+tmp] = 1
cvector[word] = wordlist
wordlist = {}
return cvector
#Takes a sentence as input
#returns a dictionary with unique words in the sentence as keys
#and arrays of tri character orthograpic featuers for values
def ortho(self,sentence):
s = sentence.split(" ")
length = len(s)
ofeatures = []
ovector = {}
for n,word in enumerate(s):
if word not in ovector:
taggedword = "#"+word+"#"
for i in range(0,len(taggedword)):
trichar = taggedword[i:i+3]
bichar = taggedword[i:i+2]
if len(trichar) == 3:
ofeatures.append(trichar)
if len(bichar) == 2:
ofeatures.append(bichar)
ofeatures.append(taggedword[i])
ovector[word] = ofeatures
ofeatures=[]
return ovector
def saveVectors(self,dirs):
print "saving ", dirs + "/" + self.lang + ".p"
pickle.dump( self.vector, open( dirs + "/" + self.lang + ".p", "wb" ) )
def loadVectors(self,dirs):
print "loading ", dirs + "/" + self.lang + ".p"
self.vector = pickle.load( open( dirs + "/" + self.lang + ".p", "rb" ) )
def transfromVector(self):
start = time.time()
class breakWord1( Exception ):
pass
class breakWord2( Exception ):
pass
for word1 in self.vector.keys():
total = 0
try:
for word2 in self.vector[word1].keys():
try:
#calculate log liklihood
k11 = float(self.vector[word1][word2])
k12 = float(self.totals[word1] - k11)
k21 = float(self.totals[word2[2:]] - k11)
k22 = float(self.totalTokens - self.totals[word1] - self.totals[word2[2:]])
n = float(k11 + k12 + k21 + k22)
c1 = float(k11 + k12)
c2 = float(k21 + k22)
r1 = float(k11 + k21)
r2 = float(k12 + k22)
if 0 == k12:
#print "deleting this one w2.. ",k11,k12,k21,k22, word1,self.totals[word1], word2[2:], self.totals[word2[2:]]," co-occur:",self.vector[word1][word2]
del self.vector[word1]
raise breakWord1
elif 0 == k21:
#print "deleting this one w2.. ",k11,k12,k21,k22, word1,self.totals[word1], word2[2:], self.totals[word2[2:]]," co-occur:",self.vector[word1][word2]
del self.vector[word1][word2]
raise breakWord2
else:
try:
self.vector[word1][word2] = \
k11 * math.log(float((k11 * n))/(c1 * r1)) \
+ k12 * math.log(float((k12 * n))/(c1 * r2)) \
+ k21 * math.log(float((k21 * n))/(c2 * r1)) \
+ k22 * math.log(float((k22 * n))/(c2 * r2))
total += self.vector[word1][word2]
except:
print "ditching this one.. ",k11,k12,k21,k22, word1, word2[2:]
print float((k11 * n))/(c1 * r1)
print float((k12 * n))/(c1 * r2)
print float((k21 * n))/(c2 * r1)
print float((k22 * n))/(c2 * r2)
exit("FAIL!")
raise breakWord2
except breakWord2:
pass
#normalize
for word2 in self.vector[word1]:
self.vector[word1][word2] /= total
except breakWord1:
pass
end = time.time()
print "elapsed time CPU:",end-start
def cleanupVector(self):
print "cleaning chomping vector...."
#remove uncommon words
for word1 in self.vector.keys():
if self.totals[word1] < 100:
del self.vector[word1]
if self.lang == "fr":
filename = "french.1.part"
elif self.lang == "de":
filename = "german.1.part"
elif self.lang == "es":
filename = "spanish.1.part"
else:
return
base = []
lines = ""
f = open('./DICT/'+filename, 'r')
for l in f.readlines():
bits=l.strip().split("\t")
if len(bits)==2:
base.append(bits[1])
todel=[]
q=0
vlen=len(self.vector)
print "cleaning vector of len:",vlen
#remove words not in base lexicon
for word1 in self.vector:
if q%100==0:
print (float(q)/vlen)*100,"%"
for word2 in self.vector[word1]:
if word2[2:] not in base:
todel.append((word1,word2))
q+=1
for w1,w2 in todel:
del self.vector[w1][w2]
def cleanEnglishVector(self,alterLang):
print "cleaning english vector...."
if self.lang != "en":
print "why are you pruning a non-english vector by a different language?"
return
base = []
lines = ""
if alterLang == "fr":
filename = "french.1.part"
elif alterLang == "de":
filename = "german.1.part"
elif alterLang == "es":
filename = "spanish.1.part"
else:
print "wrong lang?"
exit()
f = open('./DICT/'+filename, 'r')
for l in f.readlines():
bits=l.strip().split("\t")
if len(bits)==2:
base.append(bits[1])
todel=[]
q=0
vlen=len(self.vector)
print "vector len:",vlen
#remove words not in base lexicon
for word1 in self.vector:
if q%100==0:
print (float(q)/vlen)*100,"%"
for word2 in self.vector[word1]:
if word2[2:] not in base:
todel.append((word1,word2))
q+=1
for w1,w2 in todel:
del self.vector[w1][w2]
def getTestVectors(self,alterLang):
print "get Test Vectors...."
if alterLang == "fr":
filename = "french.2.part"
filename2 = "french.1.part"
elif alterLang == "de":
filename = "german.2.part"
filename2 = "german.1.part"
elif alterLang == "es":
filename = "spanish.2.part"
filename2 = "spanish.1.part"
else:
print "wrong lang?"
exit()
base = {}
lines = []
# read the Dict
f = open('./DICT/'+filename, 'r')
for l in f.readlines():
lines.append(l.strip())
# only keep what is in the dictionary
entries = lines
for e in entries:
tmp = e.strip().split("\t")
#print tmp
if tmp[1] in self.vector:
base[tmp[1]]=self.vector[tmp[1]] #base[word] = {word->double}
#print "here", tmp[1],base[tmp[1]]
#break
#so, now base has testword->{foreignword->number} for each testword in dictionary
#now, we need to translate each foreignword to english (using lang.1.part), to facilitate comparisons
f.close()
f = open('./DICT/'+filename2, 'r')
for l in f.readlines():
lines.append(l.strip())
tmp = []
for l in lines:
tmp.append(l.strip().split()) #tmp = [[english,german]]
lines = tmp
entries = {}
for l in lines:
entries[l[1]] = l[0] #entries[dictionary-german] = english
testvect = {}
for w1 in base: #base = {word->{word->double}}
final = {}
for w2 in base[w1]:
final[w2[:2] + entries[w2[2:]]] = base[w1][w2]
testvect[w1] = final
#testvect = foriegn_test_word -> {n_englishword -> number}
self.vector = testvect
def getVec(self):
return self.vector
def returnVec(self):
print len(self.vector)
print type(self.vector)
self.legend=[]
allVec=[]
allVec2=[]
Alltotal=0
tCnt=0
maxLen=0
print "generating GPU vector.."
for w1 in self.vector:
#print type(self.vector[w1])
#print self.vector[w1]
curLeg=[]
curVec=[]
for w2 in self.vector[w1]:
curLeg.append(w2)
allVec2.append([self.totals[w1],self.totals[w2[2:]],self.vector[w1][w2]])
curVec.append(self.vector[w1][w2])
Alltotal+=self.vector[w1][w2]
tCnt+=1
if len(curVec)>maxLen:
maxLen=len(curVec)
self.legend.append((w1,curLeg))
allVec.append(curVec)
#print "legend:",legend
#print ""
#print "allVec:",allVec
#exit()
#print maxLen
#print float(Alltotal)/tCnt
return allVec2, self.totalTokens
def updateVec(self, inVec):
i=0
for w1 in self.vector:
vtot=0
for w2 in self.vector[w1]:
self.vector[w1][w2]=float(inVec[i])
vtot+=float(inVec[i])
i+=1
# normalize.. should happen on GPU
for w2 in self.vector[w1]:
self.vector[w1][w2]=self.vector[w1][w2]/vtot