-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_syntactic_features.py
248 lines (192 loc) · 8.75 KB
/
generate_syntactic_features.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
import nltk
from sets import Set
class SyntacticFeatures:
def __init__(self):
self.POSList = ["SBAR","SBARQ","SINV","SQ","S","FRAG"]
self.POSList = ["sbar","sbarq","sinv","sq","s","frag"]
self.PresentList = ["VBP","VBZ","VB"]
self.PresentList = ["vbp","vbz"]
self.PastList = ["VBD"]
self.PastList = ["vbd"]
#run a preprocessing on top of all sentences to capture the tense (root) form
mainPath = "./auto-grader/ArgumentDetection/"
def setPDTBRelationProductionFile(self,productionFile,relation):
self.productionFile = productionFile
self.sourceTrainingProductions, self.targetTrainingProductions = self.prod_pdtb_relation_read_from_file(self.productionFile,relation)
self.sourceTrainingProductions.extend(Set(self.targetTrainingProductions))
self.trainingProductions = Set(self.sourceTrainingProductions)
def setRelationProductionFile(self,productionFile):
self.productionFile = productionFile
self.sourceTrainingProductions, self.targetTrainingProductions = self.prod_sr_relation_read_from_file(self.productionFile)
self.sourceTrainingProductions.extend(Set(self.targetTrainingProductions))
self.trainingProductions = Set(self.sourceTrainingProductions)
def setProductionFile(self, productionFile):
self.productionFile = productionFile
self.trainingProductions = self.prod_read_from_file(self.productionFile)
def setSourceProductionFile(self, productionFile):
self.productionFile = productionFile
self.sourceTrainingProductions = self.prod_read_from_file(self.productionFile)
def setTargetProductionFile(self, productionFile):
self.productionFile = productionFile
self.targetTrainingProductions = self.prod_read_from_file(self.productionFile)
def getSourceProductionFeats(self):
return self.trainingProductions
# def getTargetProductionFeats(self):
# return self.targetTrainingProductions
def getProductionFeats(self):
return self.trainingProductions
def prod_read_from_file(self,filename):
f = open(filename,"r")
lines = f.readlines()
productions = Set()
for index in range(1,len(lines)): # 0 is the header
line = lines[index]
prodText = line.split('\t')[2]
allProds = self.createProductions(prodText)
productions.update(Set(allProds))
f.close()
return list(productions)
def prod_relation_read_from_file(self,filename):
f = open(filename,"r")
lines = f.readlines()
source_productions = Set()
target_productions = Set()
for index in range(1,len(lines)): # 0 is the header
line = lines[index]
sourceProdText = line.split('\t')[2]
sourceProdText = sourceProdText[1:len(sourceProdText)]
allSourceProds = self.createProductions(sourceProdText)
source_productions.update(Set(allSourceProds))
targetProdText = line.split('\t')[3]
targetProdText = targetProdText[1:len(targetProdText)]
allTargetProds = self.createProductions(targetProdText)
target_productions.update(Set(allTargetProds))
f.close()
return list(source_productions),list(target_productions)
def prod_pdtb_relation_read_from_file(self,filename,relation):
f = open(filename,"r")
lines = f.readlines()
source_productions = Set()
target_productions = Set()
for index in range(1,len(lines)): # 0 is the header
line = lines[index]
features = line.split('\t')
if not features[2].lower().startswith(relation):
continue
sourceProdText = line.split('\t')[3] # always check the splits
sourceProdText = sourceProdText[1:len(sourceProdText)-1]
allSourceProds = self.createProductions(sourceProdText)
source_productions.update(Set(allSourceProds))
targetProdText = line.split('\t')[4]
targetProdText = targetProdText[1:len(targetProdText)-1]
allTargetProds = self.createProductions(targetProdText)
target_productions.update(Set(allTargetProds))
f.close()
return list(source_productions),list(target_productions)
def prod_sr_relation_read_from_file(self,filename):
f = open(filename,"r")
lines = f.readlines()
source_productions = Set()
target_productions = Set()
for index in range(1,len(lines)): # 0 is the header
line = lines[index]
sourceProdText = line.split('\t')[3] # always check the splits
sourceProdText = sourceProdText[1:len(sourceProdText)-1]
allSourceProds = self.createProductions(sourceProdText)
source_productions.update(Set(allSourceProds))
targetProdText = line.split('\t')[4]
targetProdText = targetProdText[1:len(targetProdText)-1]
allTargetProds = self.createProductions(targetProdText)
target_productions.update(Set(allTargetProds))
f.close()
return list(source_productions),list(target_productions)
def createProductions(self,prodText):
productions = []
prodText = prodText[0:len(prodText)-1]
features = prodText.split(',')
for feature in features:
feature = feature.strip()
productions.append("PROD" + "|||" + feature);
return productions
def get_productions(self,production):
productionFeatures = {}
allProds = self.createProductions(production)
for prod in allProds:
if prod in self.trainingProductions:
old = productionFeatures.get(prod)
if old is None:
old = 0
productionFeatures[prod] = old+1
return productionFeatures
def get_rel_productions(self,production,type):
productionFeatures = {}
allProds = self.createProductions(production)
# self.trainingProductions = self.sourceTrainingProductions
for prod in allProds:
if prod in self.trainingProductions:
old = productionFeatures.get(prod)
if old is None:
old = 0
productionFeatures[prod] = old+1
return productionFeatures
def get_subclauses(self,clause):
subclause_count = 0
pos_index = 0
while clause.find(self.POSList[pos_index]) == -1:
pos_index +=1
if pos_index == len(self.POSList):
return 0
if pos_index < len(self.POSList):
index = clause.find(self.POSList[pos_index])
while index != -1:
subclause_count += 1
index = clause.find("("+self.POSList[pos_index],index+1)
return float(subclause_count)
def get_depth(self,clause):
max_depth = 0
depth = -1
for char in clause:
if char == "(":
depth += 1
elif char == ")":
if max_depth < depth:
max_depth = depth
depth -= 1
return float(max_depth)
def is_present_tense(self,clause,deptree):
#first search for root
relations = deptree.split()
present = False
past = False
root = False
for relation in relations:
tokens = relation.lower().split('|')
word = tokens[0]
pos = tokens[1]
chunk = tokens[2]
grammar = tokens[3]
if grammar == 'root':
if pos.startswith('v'):
root = True
if pos in self.PresentList:
present = True
if grammar == 'root':
return 2.0
if pos in self.PastList:
past = True
if grammar == 'root':
return 1.0
#if we are here then we have not found root or the POS as verb --
#in that case - just return any verb if we found as present or past list
'''
verb_index = clause.find("(vp")
main_verb = clause.find("(v",verb_index+1)
if main_verb > -1:
if clause[main_verb:clause.find(" ",main_verb)] in self.PresentList:
return 1
'''
if present == True and root == False:
return 2.0
if past == True and root == False:
return 1.0
return 0