/
utils.py
181 lines (154 loc) · 4.89 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
177
178
179
180
181
import csv
import numpy as np
from tqdm import tqdm
def read_founta(path_file):
reader = csv.reader(open(path_file, 'r', encoding='UTF-8'), delimiter='\t')
TWEET = 0
TARGET = 1
tweets = []
targets = []
for row in reader:
if row[TARGET] != 'spam':
tweets.append(row[TWEET])
targets.append(row[TARGET])
vocab = {
"<pad>": 0,
"<unk>": 1
}
for tweet in tweets:
for word in tweet.split():
vocab[word] = len(vocab)
vocab_target = {'normal': 0, 'abusive': 1, 'hateful': 2}
targets_tokenize = []
for target in targets:
targets_tokenize.append(vocab_target[target])
return tweets, targets_tokenize, vocab
def read_hateval(path_file):
r"""
:param with_preprocessing_crazytokenizer:
:param path_file: String
:return:
"""
file = open(path_file, 'r', encoding="UTF-8")
vocab = {
"<pad>": 0,
"<unk>": 1
}
X = []
Y = []
reader = csv.reader(file, delimiter=',')
first_row = True
for row in tqdm(reader):
if first_row:
first_row = False
else:
tweet = row[1]
is_hateful = row[2]
X.append(tweet)
Y.append(int(is_hateful))
file.close()
for tweet in X:
for word in tweet.split():
vocab[word] = len(vocab)
return X, Y, vocab
def plot_history_acc(history, path):
import matplotlib.pyplot as plt
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'dev'], loc='upper left')
plt.savefig(path)
plt.close()
def plot_history_loss(history, path):
import matplotlib.pyplot as plt
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'dev'], loc='upper left')
plt.savefig(path)
plt.close()
def one_hot_weight(vocab):
one_hot = []
for k, v in vocab.items():
if '<pad>' == 0:
one_hot.append(np.zeros(len(vocab)))
else:
vector = np.zeros(len(vocab))
vector[v - 1] = 1
one_hot.append(vector)
return np.array(one_hot)
def write_prediction_founta(path_file, prediction_file, prediction):
r"""
:param path_file:
:param prediction:
:return:
"""
# path_file_write = path_file.split('.')[0] + '-ref.tsv'
writer = csv.writer(open(prediction_file, 'w', encoding='utf-8'), delimiter='\t')
# writer_ref = csv.writer(open(reference_file, 'w', encoding='utf-8'), delimiter='\t')
reader = csv.reader(open(path_file, 'r', encoding='utf-8'), delimiter='\t')
count_prediction = 0
for row in reader:
writer.writerow([row[0], prediction[count_prediction]])
# writer_ref.writerow([row[0], row[2], row[3], row[4]])
count_prediction += 1
assert (count_prediction == len(prediction))
def write_prediction_hateval(path_file, prediction_file, prediction):
r"""
:param path_file:
:param prediction:
:return:
"""
# path_file_write = path_file.split('.')[0] + '-ref.tsv'
writer = csv.writer(open(prediction_file, 'w', encoding='utf-8'), delimiter='\t')
# writer_ref = csv.writer(open(reference_file, 'w', encoding='utf-8'), delimiter='\t')
reader = csv.reader(open(path_file, 'r', encoding='utf-8'), delimiter=',')
count_prediction = 0
first_row = True
for row in reader:
if first_row:
first_row = False
# writer.writerow([row[0], '{0,1}'])
else:
writer.writerow([row[0], str(prediction[count_prediction])])
# writer_ref.writerow([row[0], row[2], row[3], row[4]])
count_prediction += 1
assert (count_prediction == len(prediction))
def prediction_to_class_softmax(prediction):
r"""
:param prediction: list of probabilities.
:return class_prediction: list of classes.
"""
import numpy as np
class_prediction = []
for prob in prediction:
prob_max = np.argmax(prob)
if prob_max == 0:
class_prediction.append('normal')
if prob_max == 1:
class_prediction.append('abusive')
if prob_max == 2:
class_prediction.append('hateful')
return class_prediction
def prediction_to_class(prediction):
r"""
:param prediction: list of probabilities.
:return class_prediction: list of classes.
"""
class_prediction = []
for prob in prediction:
if prob < 0.5:
class_prediction.append(0)
else:
class_prediction.append(1)
return class_prediction
def reconstruct_sentence(tweet):
tweets_reconstruct = []
tweet_reconstruct = ""
for word in tweet:
tweet_reconstruct += word + " "
return tweet_reconstruct