-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.py
169 lines (151 loc) · 5.06 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
from sklearn.utils import shuffle
import pickle
import re
import numpy as np
import os
def clean_str(string):
"""
Tokenization/string cleaning for all datasets except for SST.
Original taken from https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py
"""
string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
string = re.sub(r"\'s", " \'s", string)
string = re.sub(r"\'ve", " \'ve", string)
string = re.sub(r"n\'t", " n\'t", string)
string = re.sub(r"\'re", " \'re", string)
string = re.sub(r"\'d", " \'d", string)
string = re.sub(r"\'ll", " \'ll", string)
string = re.sub(r",", " , ", string)
string = re.sub(r"!", " ! ", string)
string = re.sub(r"\(", " \( ", string)
string = re.sub(r"\)", " \) ", string)
string = re.sub(r"\?", " \? ", string)
string = re.sub(r"\s{2,}", " ", string)
return string.strip().lower().split()
def read_test_data(dir="SST"):
data = {}
test_file_0 = "sentiment.test.0.cbert"
test_file_1 = "sentiment.test.1.cbert"
x, y = [], []
with open(os.path.join(dir,test_file_0), "r", encoding="utf-8") as f:
for line in f:
if line[-1] == "\n":
_, content, label = line.split('\t')
y.append(1 - int(label))
content = clean_str(content)
x.append(content)
with open(os.path.join(dir,test_file_1), "r", encoding="utf-8") as f:
for line in f:
if line[-1] == "\n":
_, content, label = line.split('\t')
y.append(1 - int(label))
content = clean_str(content)
x.append(content)
x, y = shuffle(x, y)
data["test_x"], data["test_y"] = x, y
return data
def read_data(dir="SST", train=None, dev=None, test=None):
def clean_str(string):
"""
Tokenization/string cleaning for all datasets except for SST.
Original taken from https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py
"""
string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
string = re.sub(r"\'s", " \'s", string)
string = re.sub(r"\'ve", " \'ve", string)
string = re.sub(r"n\'t", " n\'t", string)
string = re.sub(r"\'re", " \'re", string)
string = re.sub(r"\'d", " \'d", string)
string = re.sub(r"\'ll", " \'ll", string)
string = re.sub(r",", " , ", string)
string = re.sub(r"!", " ! ", string)
string = re.sub(r"\(", " \( ", string)
string = re.sub(r"\)", " \) ", string)
string = re.sub(r"\?", " \? ", string)
string = re.sub(r"\s{2,}", " ", string)
return string.strip().lower().split()
data = {}
label_cnt = [0,0]
if train:
train_file_0 = "sentiment.train.0"
train_file_1 = "sentiment.train.1"
x, y = [], []
with open(os.path.join(dir,train_file_0), "r", encoding="utf-8") as f:
for line in f:
if line[-1] == "\n":
y.append(0)
label_cnt[0] += 1
line = clean_str(line)
x.append(line)
with open(os.path.join(dir,train_file_1), "r", encoding="utf-8") as f:
for line in f:
if line[-1] == "\n":
y.append(1)
label_cnt[1] += 1
line = clean_str(line)
x.append(line)
x, y = shuffle(x, y)
data["train_x"], data["train_y"] = x, y
if dev:
dev_file_0 = "sentiment.dev.0"
dev_file_1 = "sentiment.dev.1"
x, y = [], []
with open(os.path.join(dir, dev_file_0), "r", encoding="utf-8") as f:
for line in f:
if line[-1] == "\n":
y.append(0)
line = clean_str(line)
x.append(line)
with open(os.path.join(dir, dev_file_1), "r", encoding="utf-8") as f:
for line in f:
if line[-1] == "\n":
y.append(1)
line = clean_str(line)
x.append(line)
x, y = shuffle(x, y)
data["dev_x"], data["dev_y"] = x, y
if test:
test_file_0 = "sentiment.test.0"
test_file_1 = "sentiment.test.1"
x, y = [], []
with open(os.path.join(dir,test_file_0), "r", encoding="utf-8") as f:
for line in f:
if line[-1] == "\n":
y.append(0)
line = clean_str(line)
x.append(line)
with open(os.path.join(dir,test_file_1), "r", encoding="utf-8") as f:
for line in f:
if line[-1] == "\n":
y.append(1)
line = clean_str(line)
x.append(line)
x, y = shuffle(x, y)
data["test_x"], data["test_y"] = x, y
return data, label_cnt
def save_cls(model, dataset, model_name):
path = "pytorch_pretrained_cls/{}.{}.pkl".format(dataset, model_name)
pickle.dump(model, open(path, "wb"))
print("A model is saved successfully as {}!".format(path))
def save_vocab(vocab, dataset, model_name):
path = "pytorch_pretrained_cls/{}_vocab.{}.pkl".format(dataset, m)
pickle.dump(vocab, open(path, "wb"))
print("A vocab is saved successfully as {}!".format(path))
def load_cls(dataset, model_name):
path = "pytorch_pretrained_cls/{}.{}.pkl".format(dataset, model_name)
try:
model = pickle.load(open(path, "rb"))
print("Model pytorch_pretrained_cls/{}.{}.pkl loaded successfully!".format(dataset, model_name))
return model
except:
print("No available model such as {}!".format(path))
exit()
def load_vocab(dataset, model_name):
path = "pytorch_pretrained_cls/{}_vocab.{}.pkl".format(dataset, model_name)
try:
vocab = pickle.load(open(path, "rb"))
print("Model pytorch_pretrained_cls/{}_vocab.{}.pkl loaded successfully!".format(dataset, model_name))
return vocab
except:
print("No available vocab such as {}!".format(path))
exit()