-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_parsers.py
186 lines (154 loc) · 6.49 KB
/
file_parsers.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
class nodesDoc:
def __init__(self,id,iid):
self.incrementalId=iid
self.docId=id
self.start=[]
self.end=[]
self.type=[]
self.mention=[]
self.position=[]
self.segmentId=[]
self.nodeId=[]
def append(self,start,end,type,mention,nodeId,segmentId):
self.start.append(int(start))
self.end.append(int(end))
self.type.append(type)
self.mention.append(mention)
#self.position.append(position)
self.nodeId.append(nodeId)
self.segmentId.append(int(segmentId))
class nodeParser:
def __init__(self,file):
docNr=-1
self.node_docs=[]
node=nodesDoc("","")
for i in range (file.shape[0]):
if '#doc' in file[i][0] or i==file.shape[0]-1:#append all docs including the last one
if (i==file.shape[0]-1):# append last line
segmentId=file[i][5].split(" ")[1]
nodeId=file[i][4].split(" ")[1]
node.append(file[i][0],file[i][1],file[i][2],file[i][3],nodeId,segmentId)
if (docNr!=-1):
self.node_docs.append(node)
docNr+=1
node=nodesDoc(file[i][0],docNr)
else:
segmentId=file[i][5].split(" ")[1]
nodeId=file[i][4].split(" ")[1]
node.append(file[i][0],file[i][1],file[i][2],file[i][3],nodeId,segmentId)#append lines
class read_properties:
def __init__(self,filepath, sep='=', comment_char='#'):
"""Read the file passed as parameter as a properties file."""
self.props = {}
#print filepath
with open(filepath, "rt") as f:
for line in f:
#print line
l = line.strip()
if l and not l.startswith(comment_char):
key_value = l.split(sep)
self.props[key_value[0].strip()] = key_value[1].strip('" \t')
def getProperty(self,propertyName):
return self.props.get(propertyName)
class goldFileDoc:
def __init__(self,id,iid):
self.incrementalId=iid
self.docId=id
self.form=[]
self.left_id=[]
self.right_id=[]
self.type=[]
self.left_mention=[]
self.right_mention=[]
def append(self,form,left_id,right_id,type,left_mention,right_mention):
self.form.append(form)
self.left_id.append(int(left_id))
self.right_id.append(int(right_id))
self.type.append(type)
self.left_mention.append(left_mention)
self.right_mention.append(right_mention)
class goldFileParser:
def __init__(self,file):
docNr=-1
self.gold_docs=[]
featureDoc=goldFileDoc("","")
for i in range (file.shape[0]):
if '#doc' in file[i][0] or i==file.shape[0]-1:
if (i==file.shape[0]-1):
featureDoc.append(file[i][0],file[i][1],file[i][2],file[i][3],file[i][4],file[i][5])
if (docNr!=-1):
self.gold_docs.append(featureDoc)
docNr+=1
featureDoc=goldFileDoc(file[i][0],docNr)
else:
featureDoc.append(file[i][0],file[i][1],file[i][2],file[i][3],file[i][4],file[i][5])
class featuresFileParser:
def __init__(self,file):
docNr=-1
#print file
self.feature_docs=[]
featureDoc=featureFileDoc("","")
for i in range (file.shape[0]):
if '#doc' in file[i][0] or i==file.shape[0]-1:
if (i==file.shape[0]-1):
left_mn_rel=find_between(file[i][1],"parent#|"," child#|").replace("_"," ")
#print left_mn_rel
right_mn_rel=find_between(file[i][1],"child#|"," ").replace("_"," ")
#print right_mn_rel
label=file[i][0]
featureDoc.append(left_mn_rel,right_mn_rel,label,file[i][1])
if (docNr!=-1):
self.feature_docs.append(featureDoc)
docNr+=1
featureDoc=featureFileDoc(file[i][0],docNr)
else:
left_mn_rel=find_between(file[i][1],"parent#|"," child#|").replace("_"," ")
right_mn_rel=find_between(file[i][1],"child#|"," ").replace("_"," ")
label=file[i][0]
featureDoc.append(left_mn_rel,right_mn_rel,label,file[i][1])
class featureFileDoc:
def __init__(self,id,iid):
self.incrementalId=iid
self.docId=id
self.label=[]
self.left_mention=[]
self.right_mention=[]
self.lines=[]
def append(self,left_mention,right_mention,label,line):
self.label.append(label)
self.left_mention.append(left_mention)
self.right_mention.append(right_mention)
self.lines.append(line)
class tokenDoc:
def __init__(self,id,iid):
self.incrementalId=iid
self.docId=id
self.start=[]
self.end=[]
self.sf=[]
def append(self,start,end,mention):
self.start.append(start)
self.end.append(end)
self.sf.append(mention)
class tokenParser:
def __init__(self,file):
docNr=-1
self.token_docs=[]
tokens=tokenDoc("","")
for i in range (file.shape[0]):
if '#doc' in file[i][0] or i==file.shape[0]-1:#append all docs including the last one
if (i==file.shape[0]-1):# append last line
tokens.append(file[i][0],file[i][1],file[i][2])
if (docNr!=-1):
self.token_docs.append(tokens)
docNr+=1
tokens=tokenDoc(file[i][0],docNr)
else:
tokens.append(file[i][0],file[i][1],file[i][2])#append lines
def find_between( s, first, last ):
try:
start = s.index( first ) + len( first )
end = s.index( last, start )
return s[start:end]
except ValueError:
return ""