-
Notifications
You must be signed in to change notification settings - Fork 50
/
utils.py
179 lines (152 loc) · 5.78 KB
/
utils.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
import numpy as np
from collections import Counter
import pandas as pd
import re
#import tensorflow as tf
from sklearn.cluster import KMeans
def get_clusters(W_word, n_clusters=10, **kwargs):
clusterer = KMeans(n_clusters=n_clusters,
n_jobs=-1, **kwargs)
cluster_labels = clusterer.fit_predict(W_word)
return cluster_labels
def read_glove(filename,
ndims=50):
vocab = []
char_vocab = Counter()
W = []
with open(filename) as fp:
for line in fp:
line = line.rstrip().split()
word = line[0]
embed = list(map(float, line[1:]))
vocab.append(word)
W.append(embed)
char_vocab.update(list(word))
return vocab, char_vocab, np.array(W)
def crf_loss(y_true, y_pred):
y_true = tf.cast(tf.squeeze(y_true), tf.int32)
seq_lengths_t = tf.reduce_sum(
tf.cast(tf.not_equal(y_true, 0),
tf.int32), axis=-1)
log_likelihood, transition_params = tf.contrib.crf.crf_log_likelihood(
y_pred, y_true, seq_lengths_t)
return tf.reduce_mean(-log_likelihood, axis=-1)
def load_sequences(filenames, sep=" ", col_ids=None):
sequences = []
if isinstance(filenames, str):
filenames = [filenames]
for filename in filenames:
with open(filename, encoding='utf-8') as fp:
seq = []
for line in fp:
line = line.rstrip()
if line:
line = line.split(sep)
if col_ids is not None:
line = [line[idx] for idx in col_ids]
seq.append(tuple(line))
else:
if seq:
sequences.append(seq)
seq = []
if seq:
sequences.append(seq)
return sequences
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)
line = [line[0]] + list(map(float, line[1:-1])) + [int(line[-1])]
report_list.append(tuple(line))
return pd.DataFrame(report_list[1:], columns=report_list[0])
def conll_classification_report_to_df(report):
report_list = []
report_list.append(["class", "accuracy", "precision", "recall", "f1-score", "support"])
for i, line in enumerate(report.split("\n")):
line = line.strip()
if not line:
continue
if i == 0:
continue
if i == 1:
line = re.findall(
'accuracy:\s*([0-9\.]{4,5})%; precision:\s+([0-9\.]{4,5})%; recall:\s+([0-9\.]{4,5})%; FB1:\s+([0-9\.]{4,5})',
line)[0]
line = ("overall",) + tuple(map(float, line)) + (0,)
else:
line = re.findall(
'\s*(.+?): precision:\s+([0-9\.]{4,5})%; recall:\s+([0-9\.]{4,5})%; FB1:\s+([0-9\.]{4,5})\s+([0-9]+)',
line)[0]
line = (line[0], 0.0) + tuple(map(float, line[1:-1])) + (int(line[-1]),)
report_list.append(line)
return pd.DataFrame(report_list[1:], columns=report_list[0])
def get_labels(y_arr):
return np.expand_dims(
np.array([
np.zeros(max_len)
if y is None else y
for y in y_arr],
dtype='int'),
-1)
def create_tagged_sequence(seq, task2col, default_tag):
seq_tags = []
for t in seq:
try:
tag = default_tag._replace(token=t[0], **{ti: t[ci] for ti, ci in task2col.items()})
except:
print("Error processing tag:", t)
print("Error in sequence: ", seq)
raise
seq_tags.append(tag)
return seq_tags
def get_tagged_corpus(corpus, *args):
max_len = 0
for seq in corpus:
if seq:
max_len = max(len(seq), max_len)
yield create_tagged_sequence(seq, *args)
print("Max sequence length in the corpus is: %s" % max_len)
def gen_vocab_counts(corpus, tasks, include_chars=False, token_counts=None):
task_counts = {k: Counter() for k in tasks}
if token_counts is None:
token_counts = Counter()
max_seq_len = 0
max_word_len = 0
if include_chars:
char_counts = Counter()
for seq in corpus:
max_seq_len = max(len(seq), max_seq_len)
for t in seq:
token_counts[t.token] += 1
if include_chars:
char_counts.update(list(t.token))
max_word_len = max(len(t.token), max_word_len)
for k in task_counts:
v = getattr(t, k)
if v is not None:
task_counts[k][v] += 1
if include_chars:
return token_counts, task_counts, max_seq_len, char_counts, max_word_len
return token_counts, task_counts, max_seq_len
def print_predictions(tagged_seq, predictions, filename, label_id=0, task_id=0):
from sklearn.metrics import classification_report, accuracy_score
y_true, y_pred = [], []
with open(filename, "w+") as fp:
for seq, pred in zip(tagged_seq, predictions[label_id]):
for tag, label in zip(seq, pred):
true_label = tag[task_id+1]
print(u"%s\t%s\t%s" % (tag[0], true_label, label), file=fp)
y_true.append(true_label)
y_pred.append(label)
print(u"", file=fp)
report = classification_report(y_true, y_pred)
print(report)
print("Accuracy: %s" % accuracy_score(y_true, y_pred))
return classification_report_to_df(report)