-
Notifications
You must be signed in to change notification settings - Fork 68
/
sentences.py
154 lines (130 loc) · 3.52 KB
/
sentences.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
import numpy as np
import numpy
import pickle
import gensim
from gensim.models import word2vec
d2=pickle.load(open("synsem.p",'rb'))
dtr=pickle.load(open("dwords.p",'rb'))
print "Loading Word2Vec"
#model=np.load("modelgensim.npy").item()
model = word2vec.Word2Vec.load_word2vec_format("GoogleNews-vectors-negative300.bin.gz",binary=True)
def prepare_data(data):
xa1=[]
xb1=[]
y2=[]
for i in range(0,len(data)):
xa1.append(data[i][0])
xb1.append(data[i][1])
#y2.append(round(data[i][2],0))
y2.append(data[i][2])
lengths=[]
for i in xa1:
lengths.append(len(i.split()))
for i in xb1:
lengths.append(len(i.split()))
maxlen = numpy.max(lengths)
emb1,mas1=getmtr(xa1,maxlen)
emb2,mas2=getmtr(xb1,maxlen)
y2=np.array(y2,dtype=np.float32)
return emb1,mas1,emb2,mas2,y2
def getmtr(xa,maxlen):
n_samples = len(xa)
ls=[]
x_mask = numpy.zeros((maxlen, n_samples)).astype(np.float32)
for i in range(0,len(xa)):
q=xa[i].split()
for j in range(0,len(q)):
x_mask[j][i]=1.0
while(len(q)<maxlen):
q.append(',')
ls.append(q)
xa=np.array(ls)
return xa,x_mask
# In[4]:
#new embed
def embed(stmx):
#stmx=stmx.split()
dmtr=numpy.zeros((stmx.shape[0],300),dtype=np.float32)
count=0
while(count<len(stmx)):
if stmx[count]==',':
count+=1
continue
if stmx[count] in dtr:
dmtr[count]=model[dtr[stmx[count]]]
count+=1
else:
dmtr[count]=model[stmx[count]]
count+=1
return dmtr
# In[5]:
def chsyn(s,trn):
cnt=0
global flg
x2=s.split()
x=[]
for i in x2:
x.append(i)
for i in range(0,len(x)):
q=x[i]
mst=''
if q not in d2:
continue
if q in cachedStopWords or q.title() in cachedStopWords or q.lower() in cachedStopWords:
#print q,"skipped"
continue
if q in d2 or q.lower() in d2:
if q in d2:
mst=findsim(q)
#print q,mst
elif q.lower() in d2:
mst=findsim(q)
if q not in model:
mst=''
continue
if mst in model:
if q==mst:
mst=''
continue
if model.similarity(q,mst)<0.6:
continue
#print x[i],mst
x[i]=mst
if q.find('ing')!=-1:
if x[i]+'ing' in model:
x[i]+='ing'
if x[i][:-1]+'ing' in model:
x[i]=x[i][:-1]+'ing'
if q.find('ed')!=-1:
if x[i]+'ed' in model:
x[i]+='ed'
if x[i][:-1]+'ed' in model:
x[i]=x[i][:-1]+'ed'
cnt+=1
mst=''
return ' '.join(x),cnt
def findsim(wd):
syns=d2[wd]
x=random.randint(0,len(syns)-1)
return syns[x]
def check(sa,sb,dat):
for i in dat:
if sa==i[0] and sb==i[1]:
return False
if sa==i[1] and sb==i[0]:
return False
return True
def expand(data):
n=[]
for m in range(0,10):
for i in data:
sa,cnt1=chsyn(i[0],data)
sb,cnt2=chsyn(i[1],data)
if cnt1>0 and cnt2>0:
l1=[sa,sb,i[2]]
n.append(l1)
print len(n)
for i in n:
if check(i[0],i[1],data):
data.append(i)
return data