-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrf.py
262 lines (221 loc) · 7.78 KB
/
crf.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
# coding: utf-8
import random, time, re
from nltk import word_tokenize
from nltk.tag import CRFTagger
from nltk.metrics import *
COMMON_LABEL = u'O'
STOP_LABELS = [u'CPU', u'D', u'HD', u'B']
LABELS = [u'CPU', u'D', u'HD', u'B', u'O']
DEBUG = []
def get_common_words():
common_words = []
common_file = open('common_words.txt', 'r')
for word in common_file:
common_words.append(code_sentence(word))
common_file.close()
return common_words
def code_sentence(sentence):
return unicode(sentence.strip().decode('utf-8').replace(u'\xa0', u' '))
def get_domain_set(no_stopwords):
full_set = []
domain_data_file = open('common_specs.txt', 'r')
count = 0
for line in domain_data_file:
data_line = line.split('`')
sentence = code_sentence(data_line[1])
if sentence != "":
domain_label = unicode(translate_label(data_line[0]))
tagged_sentence = ""
if no_stopwords:
tagged_sentence = tag_sentence_no_stopwords(domain_label, sentence)
else:
tagged_sentence = tag_sentence(domain_label, sentence)
full_set.append(tagged_sentence)
domain_data_file.close()
return full_set
def translate_label(label):
if label == 'cpu':
return 'CPU'
elif label == 'screen':
return 'D'
elif label == 'storage':
return 'HD'
elif label == 'name':
return 'B'
else:
raise TypeError
def tag_sentence(domain_label, sent):
tokenized_sent = word_tokenize(sent)
unicode_sent = map(lambda w: unicode(w), tokenized_sent)
return [(word, COMMON_LABEL) if word in COMMON_WORDS else (word, domain_label) for word in unicode_sent]
def tag_sentence_no_stopwords(domain_label, sent):
tokenized_sent = word_tokenize(sent)
unicode_sent = [unicode(w) for w in tokenized_sent if w not in COMMON_WORDS]
return [(word, domain_label) for word in unicode_sent]
def get_other_set():
other_set = []
other_file = open('other_specs.txt', 'r')
for line in other_file:
sentence = code_sentence(line)
if sentence != "":
domain_label = COMMON_LABEL
tagged_sentence = tag_sentence(domain_label, sentence)
other_set.append(tagged_sentence)
other_file.close()
return other_set
def divide_sets(full_set, pct):
full_size = len(full_set)
indexes = [i for i in range(full_size)]
train_indexes = random.sample(indexes, int(pct*full_size))
test_indexes = list(set(indexes) - set(train_indexes))
train_set = [full_set[j] for j in train_indexes]
test_set = [full_set[k] for k in test_indexes]
return train_set, test_set
COMMON_WORDS = get_common_words()
def map_test_set(test_set, word):
final_test_set = []
for sent in test_set:
if word:
final_test_set.append(map(lambda (u, t): u, sent))
else:
final_test_set.append(map(lambda (u, t): t, sent))
return final_test_set
def get_manual_set(no_stopwords):
manual_set = []
manual_file = open('manual_tags.txt', 'r')
for line in manual_file:
new_line = code_sentence(line)
vector_sentence = new_line.split(' ')
vector_sentence = filter(lambda w: w != u" " or w!= u'', vector_sentence)
tagged_sentence = [(vector_sentence[i], vector_sentence[i+1]) for i in range(0, len(vector_sentence), 2)]
if no_stopwords:
tagged_sentence = filter(lambda (w,l): l != u'O', tagged_sentence)
if len(tagged_sentence) > 0:
manual_set.append(tagged_sentence)
manual_file.close()
return manual_set
def feature_extraction(tokens, idx):
token = tokens[idx]
DEBUG.append(tokens[idx])
feature_list = []
#if tokens[idx] in LABELS:
# print tokens[idx-1].encode('utf-8')
if not token:
return feature_list
# First letter capitalized
if token[0].isupper():
feature_list.append('FIRST_CAPITALIZED')
# All letters capitalized
if token.isupper():
feature_list.append('ALL_CAPITALIZED')
# Has a number
if re.search("\d", token) is not None:
feature_list.append('HAS_NUM')
# Does not have chars
if re.search("[^a-zA-Z]*", token) is not None:
feature_list.append('NOT_CHARS')
#Has colon
if re.search("\:", token) is not None:
feature_list.append("HAS_COLON")
# Has quotes
if u"'" in token or u'"' in token:
feature_list.append("HAS_QUOTES")
# Is a number TODO
#if re.search(, token) is not None:
# feature_list.append('IS_NUM')
# Has a dot
if re.search(u"\.", token) is not None:
feature_list.append('HAS_DOT')
#Has parenthesis
if re.search(u"\(", token) is not None:
feature_list.append('HAS_PARENTHESIS(')
elif re.search(u"\)", token) is not None:
feature_list.append('HAS_PARENTHESIS)')
#Has an hifen
if re.search(u"-", token) is not None:
feature_list.append('HAS_HIFEN')
# Previous word
if idx-1 >= 0:
feature_list.append('PREVIOUS_WORD_' + tokens[idx-1])
else:
feature_list.append('PREVIOUS_WORD_<s>')
## Next word
if idx+1 < len(tokens):
feature_list.append('NEXT_WORD_' + tokens[idx+1])
else:
feature_list.append('NEXT_WORD_<\s>')
#
#Is punctuation
#punc_cat = set(["Pc", "Pd", "Ps", "Pe", "Pi", "Pf", "Po"])
#if all (unicodedata.category(x) in punc_cat for x in token):
# feature_list.append('PUNCTUATION')
# feature_list.append('SENT_SIZE_'+str(len(tokens)))
if len(token) > 1:
feature_list.append('SUF_' + token[-1:])
if len(token) > 2:
feature_list.append('SUF_' + token[-2:])
feature_list.append('WORD_' + token)
return feature_list
def create_vector_of_predicted_labels(tagged_sents):
predicted_labels = []
labels_per_sent = map_test_set(tagged_sents, word=False)
for labels in labels_per_sent:
predicted_labels.extend(labels)
return predicted_labels
def hasNumbers(inputString):
return any(char.isdigit() for char in inputString)
def calculate_micro_accuracy(predicted, golden, no_stopwords):
metric = {}
considered_labels = []
conf_matrix = ConfusionMatrix(golden, predicted)
if no_stopwords:
considered_labels = STOP_LABELS
else:
considered_labels = LABELS
print considered_labels
den = sum([conf_matrix[class1, class2] for class1 in considered_labels for class2 in considered_labels])
num = sum([conf_matrix[class1, class1] for class1 in considered_labels])
metric['SYSTEM_ACC'] = float(num)/den
for ithclass in considered_labels:
part_den_row = sum([conf_matrix[ithclass, class_column_idx] if class_column_idx != ithclass else 0 for class_column_idx in considered_labels])
part_den_column = sum([conf_matrix[class_row_idx, ithclass] if class_row_idx != ithclass else 0 for class_row_idx in considered_labels])
metric[ithclass+'_ACC'] = float(num)/(num+part_den_row+part_den_column)
return metric
def main(no_stopwords, use_manual_train_set):
print "MAINTAIN COMMON WORDS: " + str(not no_stopwords)
print "USING HAND LABELED TRAIN DATA: " + str(use_manual_train_set)
full_set = get_domain_set(no_stopwords)
if not no_stopwords:
full_set.extend(get_other_set())
train_set, test_set_auto = divide_sets(full_set, 0.75)
set_manual = get_manual_set(no_stopwords)
train_set_manual = []
test_set_manual = []
if use_manual_train_set:
train_set_manual, test_set_manual = divide_sets(set_manual, 0.28)
train_set.extend(train_set_manual)
else:
test_set_manual = set_manual
tagger = CRFTagger(feature_func=feature_extraction)
try:
tagger.train(train_set, 'laptop.crf.tagger')
except ValueError:
fi = open('DEBUG', 'w')
for li in DEBUG:
fi.write(str(li.encode('utf-8')) + '\n')
fi.close()
print "AUTOMATIC LABELED TEST"
tagged_sents_auto = tagger.tag_sents(map_test_set(test_set_auto, word=True))
predicted_auto = create_vector_of_predicted_labels(tagged_sents_auto)
golden_auto = create_vector_of_predicted_labels(test_set_auto)
print calculate_micro_accuracy(predicted_auto, golden_auto, no_stopwords)
print "MANUAL LABELED TEST"
tagged_sents_manual = tagger.tag_sents(map_test_set(test_set_manual, word=True))
predicted_manual = create_vector_of_predicted_labels(tagged_sents_manual)
golden_manual = create_vector_of_predicted_labels(test_set_manual)
print calculate_micro_accuracy(predicted_manual, golden_manual, no_stopwords)
print ""
main(True, True)
main(False, True)
main(True, False)
main(False, False)