-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cassim.py
285 lines (280 loc) · 13.1 KB
/
Cassim.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
__author__ = 'reihane'
import sklearn.utils.linear_assignment_ as su
import numpy as np
import sys
import os
from nltk.parse import stanford
import nltk
from nltk.tree import ParentedTree
from zss import simple_distance, Node
numnodes =0
from collections import OrderedDict
class Cassim:
def __init__(self, swbd=False):
self.sent_detector = nltk.data.load('tokenizers/punkt/english.pickle')
os.environ['STANFORD_PARSER'] = 'jars/stanford-parser.jar'
os.environ['STANFORD_MODELS'] = 'jars/stanford-parser-3.5.2-models.jar'
if swbd == False:
self.parser = stanford.StanfordParser(model_path="jars/englishPCFG.ser.gz")
else:
self.parser = stanford.StanfordParser(model_path="jars/englishPCFG_swbd.ser.gz")
def convert_mytree(self, nltktree,pnode):
global numnodes
for node in nltktree:
numnodes+=1
if type(node) is nltk.ParentedTree:
tempnode = Node(node.label())
pnode.addkid(tempnode)
self.convert_mytree(node,tempnode)
return pnode
def syntax_similarity_two_documents(self, doc1, doc2, average=False): #syntax similarity of two single documents
global numnodes
doc1sents = self.sent_detector.tokenize(doc1.strip())
doc2sents = self.sent_detector.tokenize(doc2.strip())
for s in doc1sents: # to handle unusual long sentences.
if len(s.split())>100:
return "NA"
for s in doc2sents:
if len(s.split())>100:
return "NA"
try: #to handle parse errors. Parser errors might happen in cases where there is an unsuall long word in the sentence.
doc1parsed = self.parser.raw_parse_sents((doc1sents))
doc2parsed = self.parser.raw_parse_sents((doc2sents))
except Exception as e:
sys.stderr.write(str(e))
return "NA"
costMatrix = []
doc1parsed = list(doc1parsed)
for i in range(len(doc1parsed)):
doc1parsed[i] = list(doc1parsed[i])[0]
doc2parsed = list(doc2parsed)
for i in range(len(doc2parsed)):
doc2parsed[i] = list(doc2parsed[i])[0]
for i in range(len(doc1parsed)):
numnodes = 0
sentencedoc1 = ParentedTree.convert(doc1parsed[i])
tempnode = Node(sentencedoc1.root().label())
new_sentencedoc1 = self.convert_mytree(sentencedoc1,tempnode)
temp_costMatrix = []
sen1nodes = numnodes
for j in range(len(doc2parsed)):
numnodes=0.0
sentencedoc2 = ParentedTree.convert(doc2parsed[j])
tempnode = Node(sentencedoc2.root().label())
new_sentencedoc2 = self.convert_mytree(sentencedoc2,tempnode)
ED = simple_distance(new_sentencedoc1, new_sentencedoc2)
ED = ED / (numnodes + sen1nodes)
temp_costMatrix.append(ED)
costMatrix.append(temp_costMatrix)
costMatrix = np.array(costMatrix)
if average==True:
return 1-np.mean(costMatrix)
else:
indexes = su.linear_assignment(costMatrix)
total = 0
rowMarked = [0] * len(doc1parsed)
colMarked = [0] * len(doc2parsed)
for row, column in indexes:
total += costMatrix[row][column]
rowMarked[row] = 1
colMarked [column] = 1
for k in range(len(rowMarked)):
if rowMarked[k]==0:
total+= np.min(costMatrix[k])
for c in range(len(colMarked)):
if colMarked[c]==0:
total+= np.min(costMatrix[:,c])
maxlengraph = max(len(doc1parsed),len(doc2parsed))
return 1-(total/maxlengraph)
def syntax_similarity_two_lists(self, documents1, documents2, average = False): # synax similarity of two lists of documents
global numnodes
documents1parsed = []
documents2parsed = []
for d1 in range(len(documents1)):
# print d1
tempsents = (self.sent_detector.tokenize(documents1[d1].strip()))
for s in tempsents:
if len(s.split())>100:
documents1parsed.append("NA")
break
else:
temp = list(self.parser.raw_parse_sents((tempsents)))
for i in range(len(temp)):
temp[i] = list(temp[i])[0]
temp[i] = ParentedTree.convert(temp[i])
documents1parsed.append(list(temp))
for d2 in range(len(documents2)):
# print d2
tempsents = (self.sent_detector.tokenize(documents2[d2].strip()))
for s in tempsents:
if len(s.split())>100:
documents2parsed.append("NA")
break
else:
temp = list(self.parser.raw_parse_sents((tempsents)))
for i in range(len(temp)):
temp[i] = list(temp[i])[0]
temp[i] = ParentedTree.convert(temp[i])
documents2parsed.append(list(temp))
results ={}
for d1 in range(len(documents1parsed)):
# print d1
for d2 in range(len(documents2parsed)):
# print d1,d2
if documents1parsed[d1]=="NA" or documents2parsed[d2] =="NA":
# print "skipped"
continue
costMatrix = []
for i in range(len(documents1parsed[d1])):
numnodes = 0
tempnode = Node(documents1parsed[d1][i].root().label())
new_sentencedoc1 = self.convert_mytree(documents1parsed[d1][i],tempnode)
temp_costMatrix = []
sen1nodes = numnodes
for j in range(len(documents2parsed[d2])):
numnodes=0.0
tempnode = Node(documents2parsed[d2][j].root().label())
new_sentencedoc2 = self.convert_mytree(documents2parsed[d2][j],tempnode)
ED = simple_distance(new_sentencedoc1, new_sentencedoc2)
ED = ED / (numnodes + sen1nodes)
temp_costMatrix.append(ED)
costMatrix.append(temp_costMatrix)
costMatrix = np.array(costMatrix)
if average==True:
return 1-np.mean(costMatrix)
else:
indexes = su.linear_assignment(costMatrix)
total = 0
rowMarked = [0] * len(documents1parsed[d1])
colMarked = [0] * len(documents2parsed[d2])
for row, column in indexes:
total += costMatrix[row][column]
rowMarked[row] = 1
colMarked [column] = 1
for k in range(len(rowMarked)):
if rowMarked[k]==0:
total+= np.min(costMatrix[k])
for c in range(len(colMarked)):
if colMarked[c]==0:
total+= np.min(costMatrix[:,c])
maxlengraph = max(len(documents1parsed[d1]),len(documents2parsed[d2]))
results[(d1,d2)] = 1-total/maxlengraph
return results
def syntax_similarity_conversation(self, documents1, average=False): #syntax similarity of each document with its before and after document
global numnodes
documents1parsed = []
for d1 in range(len(documents1)):
sys.stderr.write(str(d1)+"\n")
# print documents1[d1]
tempsents = (self.sent_detector.tokenize(documents1[d1].strip()))
for s in tempsents:
if len(s.split())>100:
documents1parsed.append("NA")
break
else:
temp = list(self.parser.raw_parse_sents((tempsents)))
for i in range(len(temp)):
temp[i] = list(temp[i])[0]
temp[i] = ParentedTree.convert(temp[i])
documents1parsed.append(list(temp))
results = OrderedDict()
for d1 in range(len(documents1parsed)):
d2 = d1+1
if d2 == len(documents1parsed):
break
if documents1parsed[d1] == "NA" or documents1parsed[d2]=="NA":
continue
costMatrix = []
for i in range(len(documents1parsed[d1])):
numnodes = 0
tempnode = Node(documents1parsed[d1][i].root().label())
new_sentencedoc1 = self.convert_mytree(documents1parsed[d1][i],tempnode)
temp_costMatrix = []
sen1nodes = numnodes
for j in range(len(documents1parsed[d2])):
numnodes=0.0
tempnode = Node(documents1parsed[d2][j].root().label())
new_sentencedoc2 = self.convert_mytree(documents1parsed[d2][j],tempnode)
ED = simple_distance(new_sentencedoc1, new_sentencedoc2)
ED = ED / (numnodes + sen1nodes)
temp_costMatrix.append(ED)
costMatrix.append(temp_costMatrix)
costMatrix = np.array(costMatrix)
if average==True:
return 1-np.mean(costMatrix)
else:
indexes = su.linear_assignment(costMatrix)
total = 0
rowMarked = [0] * len(documents1parsed[d1])
colMarked = [0] * len(documents1parsed[d2])
for row, column in indexes:
total += costMatrix[row][column]
rowMarked[row] = 1
colMarked [column] = 1
for k in range(len(rowMarked)):
if rowMarked[k]==0:
total+= np.min(costMatrix[k])
for c in range(len(colMarked)):
if colMarked[c]==0:
total+= np.min(costMatrix[:,c])
maxlengraph = max(len(documents1parsed[d1]),len(documents1parsed[d2]))
results[(d1,d2)] = 1-total/maxlengraph#, minWeight/minlengraph, randtotal/lengraph
return results
def syntax_similarity_one_list(self, documents1, average): #syntax similarity of each document with all other documents
global numnodes
documents1parsed = []
for d1 in range(len(documents1)):
print d1
tempsents = (self.sent_detector.tokenize(documents1[d1].strip()))
for s in tempsents:
if len(s.split())>100:
documents1parsed.append("NA")
break
else:
temp = list(self.parser.raw_parse_sents((tempsents)))
for i in range(len(temp)):
temp[i] = list(temp[i])[0]
temp[i] = ParentedTree.convert(temp[i])
documents1parsed.append(list(temp))
results ={}
for d1 in range(len(documents1parsed)):
print d1
for d2 in range(d1+1 , len(documents1parsed)):
if documents1parsed[d1] == "NA" or documents1parsed[d2]=="NA":
continue
costMatrix = []
for i in range(len(documents1parsed[d1])):
numnodes = 0
tempnode = Node(documents1parsed[d1][i].root().label())
new_sentencedoc1 = self.convert_mytree(documents1parsed[d1][i],tempnode)
temp_costMatrix = []
sen1nodes = numnodes
for j in range(len(documents1parsed[d2])):
numnodes=0.0
tempnode = Node(documents1parsed[d2][j].root().label())
new_sentencedoc2 = self.convert_mytree(documents1parsed[d2][j],tempnode)
ED = simple_distance(new_sentencedoc1, new_sentencedoc2)
ED = ED / (numnodes + sen1nodes)
temp_costMatrix.append(ED)
costMatrix.append(temp_costMatrix)
costMatrix = np.array(costMatrix)
if average==True:
return 1-np.mean(costMatrix)
else:
indexes = su.linear_assignment(costMatrix)
total = 0
rowMarked = [0] * len(documents1parsed[d1])
colMarked = [0] * len(documents1parsed[d2])
for row, column in indexes:
total += costMatrix[row][column]
rowMarked[row] = 1
colMarked [column] = 1
for k in range(len(rowMarked)):
if rowMarked[k]==0:
total+= np.min(costMatrix[k])
for c in range(len(colMarked)):
if colMarked[c]==0:
total+= np.min(costMatrix[:,c])
maxlengraph = max(len(documents1parsed[d1]),len(documents1parsed[d2]))
results[(d1,d2)] = 1-total/maxlengraph#, minWeight/minlengraph, randtotal/lengraph
return results