-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathpreprocess_yahoo.py
176 lines (148 loc) · 4.45 KB
/
preprocess_yahoo.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
# -*- coding: utf-8 -*-
"""
Created on Mon May 1 01:53:46 2017
@author: DinghanShen
"""
import csv
import numpy as np
import os
import re
import cPickle
import string
import pdb
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
#==============================================================================
loadpath = './test.csv'
x = []
with open(loadpath, 'rb') as f:
for line in f:
x.append(line)
def clean_str(string):
"""
Tokenization/string cleaning for all datasets except for SST.
Every dataset is lower cased except for TREC
"""
string = re.sub(r"[^A-Za-z0-9(),\.!?]", " ", string)
#string = re.sub(r"\'s", " \'s", string)
string = re.sub(r"e\.g\.,", " ", string)
string = re.sub(r"a\.k\.a\.", " ", string)
string = re.sub(r"i\.e\.,", " ", string)
string = re.sub(r"i\.e\.", " ", string)
#string = re.sub(r"\'ve", " \'ve", string)
#string = re.sub(r"\'", "", 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"br", "", 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)
string = re.sub(r"u\.s\.", " us ", string)
return string.strip().lower()
lab = []
sent = []
vocab = {}
for i in range(60000):
# lab.append(clean_str(x[i].split(",")[0]))
m = re.search(',', x[i])
rest = x[i][m.start()+1:]
m = re.search(',', rest)
temp = clean_str(rest[:m.start()]).split()
# temp = clean_str(x[i][m.start()+1 : n + 1]).split()
temp = [ j if not is_number(j) else '0' for j in temp]
if len(temp) > 300:
lab.append(clean_str(x[i].split(",")[0]))
temp = temp[:300]
sent.append(temp)
elif len(temp) <= 5: # remove too short question
continue
else:
lab.append(clean_str(x[i].split(",")[0]))
sent.append(temp)
t = set(temp)
for word in t:
if word in vocab:
vocab[word] += 1
else:
vocab[word] = 1
loadpath = './train.csv'
x = []
with open(loadpath, 'rb') as f:
for line in f:
x.append(line)
train_lab = []
train_sent = []
for i in range(len(x)):
# train_lab.append(clean_str(x[i].split(",")[0]))
m = re.search(',', x[i])
rest = x[i][m.start()+1:]
m = re.search(',', rest)
temp = clean_str(rest[:m.start()]).split()
# temp = clean_str(x[i][m.start()+1: n+ 1]).split()
temp = [ j if not is_number(j) else '0' for j in temp]
if len(temp) > 300:
train_lab.append(clean_str(x[i].split(",")[0]))
temp = temp[:300]
train_sent.append(temp)
elif len(temp) <= 5: # remove too short question
continue
else:
train_lab.append(clean_str(x[i].split(",")[0]))
train_sent.append(temp)
t = set(temp)
for word in t:
if word in vocab:
vocab[word] += 1
else:
vocab[word] = 1
#==============================================================================
print('create ixtoword and wordtoix lists...')
v = [x for x, y in vocab.iteritems() if y >= 30]
# create ixtoword and wordtoix lists
ixtoword = {}
# period at the end of the sentence. make first dimension be end token
ixtoword[0] = 'END'
ixtoword[1] = 'UNK'
wordtoix = {}
wordtoix['END'] = 0
wordtoix['UNK'] = 1
ix = 2
for w in vocab:
wordtoix[w] = ix
ixtoword[ix] = w
ix += 1
def convert_word_to_ix(data):
result = []
for sent in data:
temp = []
for w in sent:
if w in wordtoix:
temp.append(wordtoix[w])
else:
temp.append(1)
temp.append(0)
result.append(temp)
return result
train_x = train_sent[:1100000]
train_y = train_lab[:1100000]
val_x = train_sent[1100000:]
val_y = train_lab[1100000:]
test_x = sent
test_y = lab
train_text = [' '.join(s) for s in train_x]
val_text = [' '.join(s) for s in val_x]
test_text = [' '.join(s) for s in test_x]
train_x = convert_word_to_ix(train_x)
val_x = convert_word_to_ix(val_x)
test_x = convert_word_to_ix(test_x)
cPickle.dump([train_x, val_x, test_x, train_text, val_text, test_text, train_y, val_y, test_y, wordtoix, ixtoword], open("yahoo4char.p", "wb"))
pdb.set_trace()