-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiments.py
325 lines (282 loc) · 11.3 KB
/
experiments.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_context("poster")
sns.set_style("ticks")
from itertools import chain
from sklearn.metrics import make_scorer
from sklearn.cross_validation import cross_val_score
from sklearn.grid_search import RandomizedSearchCV
from sklearn.feature_extraction import DictVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from sklearn.svm import LinearSVC
from sklearn.cluster import AgglomerativeClustering
import sklearn_crfsuite
from sklearn_crfsuite import scorers
from sklearn_crfsuite import metrics
from sklearn.cross_validation import cross_val_score
from sklearn.grid_search import RandomizedSearchCV
import regex as re
from collections import namedtuple, defaultdict, Counter, OrderedDict
from IPython.display import display
from joblib import load, dump, Parallel, delayed
import os, string, sys
from gensim.models import word2vec
class RegexFeatures(object):
PATTERNS = {
"isInitCapitalWord": re.compile(r'^[A-Z][a-z]+'),
"isAllCapitalWord": re.compile(r'^[A-Z][A-Z]+$'),
"isAllSmallCase": re.compile(r'^[a-z]+$'),
"isWord": re.compile(r'^[a-zA-Z][a-zA-Z]+$'),
"isAlphaNumeric": re.compile(r'^\p{Alnum}+$'),
"isSingleCapLetter": re.compile(r'^[A-Z]$'),
"containsDashes": re.compile(r'.*--.*'),
"containsDash": re.compile(r'.*\-.*'),
"singlePunctuation": re.compile(r'^\p{Punct}$'),
"repeatedPunctuation": re.compile(r'^[\.\,!\?"\':;_\-]{2,}$'),
"singleDot": re.compile(r'[.]'),
"singleComma": re.compile(r'[,]'),
"singleQuote": re.compile(r'[\']'),
"isSpecialCharacter": re.compile(r'^[#;:\-/<>\'\"()&]$'),
"fourDigits": re.compile(r'^\d\d\d\d$'),
"isDigits": re.compile(r'^\d+$'),
"isNumber": re.compile(r'^((\p{N}{,2}([,]?\p{N}{3})+)(\.\p{N}+)?)$'),
"containsDigit": re.compile(r'.*\d+.*'),
"endsWithDot": re.compile(r'\p{Alnum}+\.$'),
"isURL": re.compile(r'^http[s]?://'),
"isMention": re.compile(r'^(RT)?@[\p{Alnum}_]+$'),
"isHashtag": re.compile(r'^#\p{Alnum}+$'),
"isMoney": re.compile(r'^\$((\p{N}{,2}([,]?\p{N}{3})+)(\.\p{N}+)?)$'),
}
def __init__(self):
print "Initialized RegexFeature"
@staticmethod
def process(word):
features = dict()
for k, p in RegexFeatures.PATTERNS.iteritems():
if p.match(word):
features[k] = True
return features
def classification_report_to_df(report):
report_list = []
for i, line in enumerate(report.split("\n")):
if i == 0:
report_list.append(["class", "precision", "recall", "f1-score", "support"])
else:
line = line.strip()
if line:
if line.startswith("avg"):
line = line.replace("avg / total", "avg/total")
line = re.split(r'\s+', line)
report_list.append(tuple(line))
return pd.DataFrame(report_list[1:], columns=report_list[0])
DATA_DIR="data/data/"
CLEANED_DIR="data/cleaned/"
Tag = namedtuple("Tag", ["token", "tag"])
def load_sequences(filename, sep="\t", notypes=False, test_data=False):
sequences = []
with open(filename) as fp:
seq = []
for line in fp:
line = line.strip()
if line:
line = line.split(sep)
if test_data:
assert len(line) == 1
line.append("?")
if notypes:
line[1] = line[1][0]
seq.append(Tag(*line))
else:
sequences.append(seq)
seq = []
if seq:
sequences.append(seq)
return sequences
def load_vocab(filename):
vocab = set()
with open(filename) as fp:
for line in fp:
line = line.strip()
vocab.add(line)
return vocab
def print_transitions(trans_features):
for (label_from, label_to), weight in trans_features:
print("%-6s -> %-7s %0.6f" % (label_from, label_to, weight))
def print_state_features(state_features):
for (attr, label), weight in state_features:
print("%0.6f %-8s %s" % (weight, label, attr))
def plot_cm(y_test, y_pred, labels=[], axis=1):
labels_s = dict((k,i) for i,k in enumerate(labels))
cm = np.zeros((len(labels), len(labels)))
for i,j in zip(sum(y_test, []), sum(y_pred, [])):
i = labels_s[i]
j = labels_s[j]
cm[i,j] += 1
with plt.rc_context(rc={'xtick.labelsize': 12, 'ytick.labelsize': 12,
'figure.figsize': (16,14)}):
sns.heatmap(cm * 100/ cm.sum(axis=axis, keepdims=True),
#cmap=sns.cubehelix_palette(n_colors=100, rot=-.4, as_cmap=True),
cmap="Greys",
xticklabels=labels,
yticklabels=labels)
plt.ylabel("True labels")
plt.xlabel("Predicted labels")
title = "Precision Plot"
if axis== 0:
title = "Recall Plot"
plt.title(title)
print cm.shape
return cm
def print_sequences(sequences, predictions, filename, test_data=False, notypes=False):
with open(filename, "wb+") as fp:
for seq, pred in zip(sequences, predictions):
for t, p in zip(seq, pred):
token, tag = t
if tag[0] == "U":
tag = "B%s" % tag[1:]
if tag[0] == "E":
tag = "I%s" % tag[1:]
if p[0] == "U":
p = "B%s" % p[1:]
if p[0] == "E":
p = "I%s" % p[1:]
if notypes:
tag = tag[0]
p = p[0]
if test_data:
line = "\t".join((token, p))
else:
line = "\t".join((token, tag, p))
print >> fp, line
print >> fp, ""
print "Done"
WORD_SPLITTER = re.compile(r'[\p{Punct}\s]+')
class DictionaryFeatures:
def __init__(self, dictDir):
self.word2dictionaries = {}
self.word2hashtagdictionaries = {}
self.dictionaries = []
i = 0
for d in os.listdir(dictDir):
print >> sys.stderr, "read dict %s"%d
self.dictionaries.append(d)
if d == '.svn':
continue
for line in open(dictDir + "/" + d):
word = line.rstrip('\n')
word = word.strip(' ').lower()
word_hashtag = "".join(WORD_SPLITTER.split(word))
if not self.word2dictionaries.has_key(word):
self.word2dictionaries[word] = str(i)
else:
self.word2dictionaries[word] += "\t%s" % i
if not self.word2hashtagdictionaries.has_key(word_hashtag):
self.word2hashtagdictionaries[word_hashtag] = str(i)
else:
self.word2hashtagdictionaries[word_hashtag] += "\t%s" % i
i += 1
MAX_WINDOW_SIZE=6
def GetDictFeatures(self, words, i):
features = []
for window in range(1,self.MAX_WINDOW_SIZE):
start=max(i-window+1, 0)
end = start + window
phrase = ' '.join(words[start:end]).lower().strip(string.punctuation)
if self.word2dictionaries.has_key(phrase):
for j in self.word2dictionaries[phrase].split('\t'):
features.append('DICT=%s' % self.dictionaries[int(j)])
if window > 1:
features.append('DICTWIN=%s' % window)
return list(set(features))
def GetHashtagDictFeatures(self, word):
features = []
if len(word) < 2 or word[0] != "#":
return features
word = word[1:].lower().strip(string.punctuation)
if self.word2hashtagdictionaries.has_key(word):
for j in self.word2hashtagdictionaries[word].split('\t'):
features.append('DICT_HASHTAG=%s' % self.dictionaries[int(j)])
return list(set(features))
train_sequences = load_sequences("data/cleaned/train.BIEOU.tsv", sep="\t", notypes=False)
dev_sequences = (load_sequences("data/cleaned/dev.BIEOU.tsv", sep="\t", notypes=False)
+ load_sequences("data/cleaned/dev_2015.BIEOU.tsv", sep="\t", notypes=False))
vocab = load_vocab("vocab.no_extras.txt")
print len(vocab)
all_sentences = [[t[0] for t in seq] for seq in (train_sequences+dev_sequences)]
other_entities = {
"isHashtag": [],
"isMention": [],
"isURL": [],
"isMoney": [],
"isNumber": [],
"repeatedPunctuation": []
}
for seq in all_sentences:
for t in seq:
for k in other_entities.keys():
if RegexFeatures.PATTERNS[k].match(t):
other_entities[k].append(t)
for k, v in other_entities.iteritems():
print k, len(v)
ENTITY_MAPPINGS={k: "__%s__" % k for k in other_entities.keys()}
ENTITY_MAPPINGS
def preprocess_token(x, to_lower=False):
for k in ENTITY_MAPPINGS.keys():
if RegexFeatures.PATTERNS[k].match(x):
return ENTITY_MAPPINGS[k]
if to_lower:
x = x.lower()
return x
GLOVE_TWEET_MAPPINGS={
"<user>": "isMention",
"<hashtag>": "isHashtag",
"<number>": "isDigit",
"<url>": "isURL",
"<allcaps>": "isAllCapitalWord",
}
def process_glovevectors(filename):
words, dim = 0, 0
with open(filename) as fp:
for line in fp:
#line = line.strip().split(" ")
words+= 1
line = line.strip().split(" ")
dim = len(line) - 1
print "Words: {}, dim: {}".format(words, dim)
with open(filename) as fp, open("{}.processed.txt".format(filename), "wb+") as fp1:
print >> fp1, words, dim
for line in fp:
line = line.strip().split(" ", 1)
line[0] = dict.get(GLOVE_TWEET_MAPPINGS, line[0], line[0])
print >> fp1, line[0], line[1]
print "Done"
def tweet_features(sent, word2vec_model=None, cluster_vocabs=None, WORD_IDX=0):
features = {}
sent_length = len(sent) * 1.
for widx, word in enumerate(sent):
word = word[WORD_IDX]
lookup_key = preprocess_token(word, to_lower=True)
if word2vec_model and lookup_key in word2vec_model:
for i,v in enumerate(word2vec_model[lookup_key]):
features["_GLOBAL_WORDVEC_%s" % i] = dict.get(features, "_GLOBAL_WORDVEC_%s" % i, 0) + v
if cluster_vocabs and lookup_key in cluster_vocabs:
v = dict.get(cluster_vocabs, lookup_key)
features["_GLOBAL_CLUSTER_=%s" % v] = dict.get(features, "_GLOBAL_CLUSTER_=%s" % v, 0) + 1
if dict_features:
d_features = dict_features.GetDictFeatures([k[WORD_IDX] for k in sent], widx)
for k in d_features:
features[k] = dict.get(features, k, 0) + 1
d_hashtag_features = dict_features.GetHashtagDictFeatures(word)
for k in d_hashtag_features:
features[k] = dict.get(features, k, 0) + 1
#features = {k: v / sent_length for k,v in features.iteritems()}
return features
def is_tweet_type(sent, cat_type):
for t in sent:
if t.tag != "O":
if t.tag[2:] == cat_type:
return 1
return 0