-
Notifications
You must be signed in to change notification settings - Fork 0
/
LabelledFeatures.py
284 lines (240 loc) · 12.7 KB
/
LabelledFeatures.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
import pickle
from nltk import pos_tag
from nltk.tokenize import word_tokenize
from nltk.stem.porter import PorterStemmer
from nltk.corpus import stopwords
import string
import re
def getStem(word, stemmer):
return stemmer.stem(word)
def getShape(word):
shape = ""
for char in word:
if char.isupper():
shape += 'X'
else:
shape += 'x'
return shape
def getPOSTag(line, wordNum):
words = []
for wordLabelPair in line:
words.append(wordLabelPair[0])
tags = pos_tag(words)
return tags[wordNum][1]
def getPrevTokensClasses(line, wordNum, stopList):
prev_tokens = []
prev_classes = []
for i in range(1, wordNum + 1):
if len(prev_tokens) < 3 and line[wordNum - i][0] not in stopList:
prev_tokens.append(line[wordNum - i][0])
prev_classes.append(line[wordNum - i][1])
while len(prev_tokens) < 3:
prev_tokens.append("")
prev_classes.append("")
for i in [0,1,2]:
prev_tokens[i] = prev_tokens[i].lower()
return prev_tokens, prev_classes
def getNextTokens(line, wordNum, stopList):
next_tokens = []
for i in range(1, len(line) - wordNum):
if len(next_tokens) < 3 and line[wordNum + i][0] not in stopList:
next_tokens.append(line[wordNum + i][0])
while len(next_tokens) < 3:
next_tokens.append("")
for i in [0,1,2]:
next_tokens[i] = next_tokens[i].lower()
return next_tokens
def getLabelledFeatures(labelled_sentences_file, labelled_features_file):
inputFile = open(labelled_sentences_file[:-4] + ".pickle","rb")
inputList = pickle.load(inputFile)
inputFile.close()
outputFile = open(labelled_features_file[:-4] + ".txt", "w+", encoding="utf-8")
outputFile2 = open(labelled_features_file[:-4] + "2.txt", "w+", encoding="utf-8")
outputFile3 = open(labelled_features_file[:-4] + "3.txt", "w+", encoding="utf-8")
outputFile4 = open(labelled_features_file[:-4] + "4.txt", "w+", encoding="utf-8")
outputFile5 = open(labelled_features_file[:-4] + "5.txt", "w+", encoding="utf-8")
outputFile6 = open(labelled_features_file[:-4] + "6.txt", "w+", encoding="utf-8")
outputFile7 = open(labelled_features_file[:-4] + "7.txt", "w+", encoding="utf-8")
outputFile8 = open(labelled_features_file[:-4] + "8.txt", "w+", encoding="utf-8")
stemmer = PorterStemmer()
stops = stopwords.words('english')
stops.remove('m')
stopList = stops + list(string.punctuation)
stopList.remove("'")
stopList.remove('"')
numbers = ['0','1','2','3','4','5','6','7','8','9']
units = ['m', 'cm', 'mm', 'ft', 'in', 'inches', 'feet', 'foot', 'km', 'miles', 'kilometre', 'kilometres', 'centimetre', 'metre', 'centimetres', 'metres']
allFeatures = []
allFeatures2 = []
allFeatures3 = []
allFeatures4 = []
allFeatures5 = []
allFeatures6 = []
allFeatures7 = []
allFeatures8 = []
for line in inputList:
for wordNum in range(len(line)):
if line[wordNum][0] not in stopList:
# set up feature class tuple
features = {'token':line[wordNum][0].lower()}
features2 = {'token':line[wordNum][0].lower()}
features3 = {'token':line[wordNum][0].lower()}
features4 = {'token':line[wordNum][0].lower()}
classLabel = line[wordNum][1]
# add new features
# stemmed token
features['stemmed'] = getStem(line[wordNum][0].lower(), stemmer)
features3['stemmed'] = getStem(line[wordNum][0].lower(), stemmer)
features4['stemmed'] = getStem(line[wordNum][0].lower(), stemmer)
# token shape
features['shape'] = getShape(line[wordNum][0])
features2['shape'] = getShape(line[wordNum][0])
features3['shape'] = getShape(line[wordNum][0])
features4['shape'] = getShape(line[wordNum][0])
# part of speech tag
features['pos_tag'] = getPOSTag(line, wordNum)
features2['pos_tag'] = getPOSTag(line, wordNum)
features3['pos_tag'] = getPOSTag(line, wordNum)
features4['pos_tag'] = getPOSTag(line, wordNum)
# Previous 3 tokens
# Previous 3 tokens classes
prev_tokens_classes = getPrevTokensClasses(line, wordNum, stopList)
features['prev_tokens'], features['prev_classes'] = str(prev_tokens_classes[0]).lower(), str(prev_tokens_classes[1]).lower()
features2['prev_tokens'], features2['prev_classes'] = str(prev_tokens_classes[0]).lower(), str(prev_tokens_classes[1]).lower()
features4['prev_tokens'], features4['prev_classes'] = str(prev_tokens_classes[0]).lower(), str(prev_tokens_classes[1]).lower()
# Next 3 tokens
features['next_tokens'] = str(getNextTokens(line, wordNum, stopList))
features2['next_tokens'] = str(getNextTokens(line, wordNum, stopList))
features4['next_tokens'] = str(getNextTokens(line, wordNum, stopList))
# Sub-tokens
features['sub-tokens'] = "('','')"
features2['sub-tokens'] = "('','')"
features3['sub-tokens'] = "('','')"
# Contains numbers
features['numbers'] = False
features2['numbers'] = False
features3['numbers'] = False
for c in range(len(line[wordNum][0])):
if line[wordNum][0][c] in numbers:
features['numbers'] = True
features2['numbers'] = True
features3['numbers'] = True
try:
if c != 0 and line[wordNum][0][c + 1] not in numbers:
features['sub-tokens'] = "('" + line[wordNum][0].split(line[wordNum][0][c])[0] + line[wordNum][0][c] + "','" + line[wordNum][0].split(line[wordNum][0][c])[1] + "')"
features2['sub-tokens'] = "('" + line[wordNum][0].split(line[wordNum][0][c])[0] + line[wordNum][0][c] + "','" + line[wordNum][0].split(line[wordNum][0][c])[1] + "')"
features3['sub-tokens'] = "('" + line[wordNum][0].split(line[wordNum][0][c])[0] + line[wordNum][0][c] + "','" + line[wordNum][0].split(line[wordNum][0][c])[1] + "')"
except IndexError:
pass
# Contains a unit of size
features['unit_of_size'] = False
features2['unit_of_size'] = False
features3['unit_of_size'] = False
for unit in units:
if unit in line[wordNum][0]:
features['unit_of_size'] = True
features2['unit_of_size'] = True
features3['unit_of_size'] = True
# save feature class tuple
outputFile.write(str((features, classLabel)) + "\n")
outputFile2.write(str((features2, classLabel)) + "\n")
outputFile3.write(str((features3, classLabel)) + "\n")
outputFile4.write(str((features4, classLabel)) + "\n")
allFeatures.append((features, classLabel))
allFeatures2.append((features2, classLabel))
allFeatures3.append((features3, classLabel))
allFeatures4.append((features4, classLabel))
# set up feature class tuple
features5 = {'token':line[wordNum][0].lower()}
features6 = {'token':line[wordNum][0].lower()}
features7 = {'token':line[wordNum][0].lower()}
features8 = {'token':line[wordNum][0].lower()}
classLabel = line[wordNum][1]
# add new features
# stemmed token
features5['stemmed'] = getStem(line[wordNum][0].lower(), stemmer)
features7['stemmed'] = getStem(line[wordNum][0].lower(), stemmer)
features8['stemmed'] = getStem(line[wordNum][0].lower(), stemmer)
# token shape
features5['shape'] = getShape(line[wordNum][0])
features6['shape'] = getShape(line[wordNum][0])
features7['shape'] = getShape(line[wordNum][0])
features8['shape'] = getShape(line[wordNum][0])
# part of speech tag
features5['pos_tag'] = getPOSTag(line, wordNum)
features6['pos_tag'] = getPOSTag(line, wordNum)
features7['pos_tag'] = getPOSTag(line, wordNum)
features8['pos_tag'] = getPOSTag(line, wordNum)
# Previous 3 tokens
# Previous 3 tokens classes
prev_tokens_classes = getPrevTokensClasses(line, wordNum, stopList)
features5['prev_tokens'], features5['prev_classes'] = str(prev_tokens_classes[0]).lower(), str(prev_tokens_classes[1]).lower()
features6['prev_tokens'], features6['prev_classes'] = str(prev_tokens_classes[0]).lower(), str(prev_tokens_classes[1]).lower()
features8['prev_tokens'], features8['prev_classes'] = str(prev_tokens_classes[0]).lower(), str(prev_tokens_classes[1]).lower()
# Next 3 tokens
features5['next_tokens'] = str(getNextTokens(line, wordNum, stopList))
features6['next_tokens'] = str(getNextTokens(line, wordNum, stopList))
features8['next_tokens'] = str(getNextTokens(line, wordNum, stopList))
# Sub-tokens
features5['sub-tokens'] = "('','')"
features6['sub-tokens'] = "('','')"
features7['sub-tokens'] = "('','')"
# Contains numbers
features5['numbers'] = False
features6['numbers'] = False
features7['numbers'] = False
for c in range(len(line[wordNum][0])):
if line[wordNum][0][c] in numbers:
features5['numbers'] = True
features6['numbers'] = True
features7['numbers'] = True
try:
if c != 0 and line[wordNum][0][c + 1] not in numbers:
features5['sub-tokens'] = "('" + line[wordNum][0].split(line[wordNum][0][c])[0] + line[wordNum][0][c] + "','" + line[wordNum][0].split(line[wordNum][0][c])[1] + "')"
features6['sub-tokens'] = "('" + line[wordNum][0].split(line[wordNum][0][c])[0] + line[wordNum][0][c] + "','" + line[wordNum][0].split(line[wordNum][0][c])[1] + "')"
features7['sub-tokens'] = "('" + line[wordNum][0].split(line[wordNum][0][c])[0] + line[wordNum][0][c] + "','" + line[wordNum][0].split(line[wordNum][0][c])[1] + "')"
except IndexError:
pass
# Contains a unit of size
features5['unit_of_size'] = False
features6['unit_of_size'] = False
features7['unit_of_size'] = False
for unit in units:
if unit in line[wordNum][0]:
features5['unit_of_size'] = True
features6['unit_of_size'] = True
features7['unit_of_size'] = True
# save feature class tuple
outputFile5.write(str((features5, classLabel)) + "\n")
outputFile6.write(str((features6, classLabel)) + "\n")
outputFile7.write(str((features7, classLabel)) + "\n")
outputFile8.write(str((features8, classLabel)) + "\n")
allFeatures5.append((features5, classLabel))
allFeatures6.append((features6, classLabel))
allFeatures7.append((features7, classLabel))
allFeatures8.append((features8, classLabel))
with open(labelled_features_file[:-4] + '.pickle', 'wb+') as f:
pickle.dump(allFeatures, f)
with open(labelled_features_file[:-4] + '2.pickle', 'wb+') as f:
pickle.dump(allFeatures2, f)
with open(labelled_features_file[:-4] + '3.pickle', 'wb+') as f:
pickle.dump(allFeatures3, f)
with open(labelled_features_file[:-4] + '4.pickle', 'wb+') as f:
pickle.dump(allFeatures4, f)
with open(labelled_features_file[:-4] + '5.pickle', 'wb+') as f:
pickle.dump(allFeatures5, f)
with open(labelled_features_file[:-4] + '6.pickle', 'wb+') as f:
pickle.dump(allFeatures6, f)
with open(labelled_features_file[:-4] + '7.pickle', 'wb+') as f:
pickle.dump(allFeatures7, f)
with open(labelled_features_file[:-4] + '8.pickle', 'wb+') as f:
pickle.dump(allFeatures8, f)
inputFile.close()
outputFile.close()
outputFile2.close()
outputFile3.close()
outputFile4.close()
outputFile5.close()
outputFile6.close()
outputFile7.close()
outputFile8.close()