-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcreation.py
284 lines (190 loc) · 7.15 KB
/
creation.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
#!/usr/bin/env python
# coding: utf-8
# # Short Texts Of Products and Services (STOPS)
# In[16]:
# define paths
PATH_TO_YELP = 'source/yelp/yelp_academic_dataset_business.json'
PATH_TO_MAVE_POS = 'source/mave/mave_positives.jsonl'
PATH_TO_MAVE_NEG = 'source/mave/mave_negatives.jsonl'
PATH_TO_BINARY_RESULTS = 'STOPS-2-long/STOPS-2'
PATH_TO_RESULTS = 'STOPS-long/STOPS'
# In[17]:
# define parameters
RANDOM_STATE = 42
TRAIN_SPLIT = 0.7
# ## Load Data
# In[18]:
import json
def read_jsonline(file_path):
# read file line by line
with open(file_path, 'r', encoding="utf8") as f:
while True:
jsonline = f.readline()
if not jsonline:
break
yield json.loads(jsonline)
# In[19]:
def countWords(sentence):
words = sentence.split()
return len(words)
# In[20]:
import string
import unicodedata
def preprocess(text):
# lowercase
text = text.lower()
# remove multiple spaces
text = ' '.join(text.split())
# remove punctuation
text = text.translate(text.maketrans('', '', string.punctuation))
# map to unicode
text = ''.join(c for c in unicodedata.normalize('NFD', text) if unicodedata.category(c) != 'Mn')
return text
# ### Mave
# In[21]:
def count_mave(path, label_counter, limit_words, limits_cat, min_char_count):
for line in read_jsonline(path):
if not line:
break
if (len(line['paragraphs']) >= 1) and (line['paragraphs'][0]['source'] == 'title') and (line['category']):
text = line['paragraphs'][0]['text']
if (countWords(text) <= limit_words) and (len(text) >= min_char_count):
label = line['category']
if label not in label_counter:
label_counter[label] = 0
if label_counter[label] < limits_cat:
label_counter[label] += 1
# In[22]:
def load_mave(path, texts, label_counter, top_cats, limit_words, limits_cat, min_char_count):
for line in read_jsonline(path):
if not line:
break
if (len(line['paragraphs']) >= 1) and (line['paragraphs'][0]['source'] == 'title') and (line['category']):
text = line['paragraphs'][0]['text']
label = line['category']
if label not in label_counter:
label_counter[label] = 0
if (label in top_cats) and (label_counter[label] < limits_cat):
texts.append((label, preprocess(text)))
label_counter[label] += 1
# In[23]:
size = 5000
min_char_count = 5
limit_words = 7
mave_texts = []
label_counter = dict()
# count most frequent labels
count_mave(PATH_TO_MAVE_POS, label_counter, limit_words, size, min_char_count)
count_mave(PATH_TO_MAVE_NEG, label_counter, limit_words, size, min_char_count)
top_cats = sorted(label_counter.items(), key=lambda x: x[1], reverse=True)
# select 20 most frequent labels
top_cats = top_cats[:20]
top_cats = [cat for cat, count in top_cats]
# In[24]:
# load most frequent labels
label_counter = dict()
load_mave(PATH_TO_MAVE_POS, mave_texts, label_counter, top_cats, limit_words, size, min_char_count)
load_mave(PATH_TO_MAVE_NEG, mave_texts, label_counter, top_cats, limit_words, size, min_char_count)
# ### Yelp Business Data
# In[25]:
# count label occurences
count_labels = dict()
for line in read_jsonline(PATH_TO_YELP):
if not line:
break
if line['categories'] and line['name']:
label = line['categories']
categories = label.replace(", ", ",").split(",")
for category in categories:
if category not in count_labels:
count_labels[category] = 0
count_labels[category] += 1
# In[26]:
# order by occurrences
count_labels = sorted(count_labels.items(), key=lambda x: x[1], reverse=True)
# In[27]:
# map data points to the most frequent label
yelp_texts = []
limit = 12000
label_counter = dict()
for line in read_jsonline(PATH_TO_YELP):
if not line:
break
if line['categories'] and line['name']:
categories = line['categories'].replace(", ", ",").split(",")
# find first label in label list
for label, _ in count_labels:
if label in categories:
if label not in label_counter:
label_counter[label] = 0
label_counter[label] += 1
if label_counter[label] <= limit:
text = str(line['name'])
yelp_texts.append((label, preprocess(text)))
break
# In[28]:
# print length
print("YELP length:",len(yelp_texts))
print("MAVE length:",len(mave_texts))
# print labels
yelp_set = set()
for label, _ in yelp_texts:
yelp_set.add(label)
mave_set = set()
for label, _ in mave_texts:
mave_set.add(label)
print("YELP labels:",yelp_set)
print("MAVE labels:",mave_set)
# ## Create Dataset
# In[29]:
# create splits with fixed seed
import random
random.seed(RANDOM_STATE)
# yelp
yelp_length = len(yelp_texts)
yelp_train_length = int(yelp_length * TRAIN_SPLIT)
yelp_train_indices = random.sample(range(yelp_length), yelp_train_length)
yelp_test_indices = [i for i in range(yelp_length) if i not in yelp_train_indices]
# mave
mave_length = len(mave_texts)
mave_train_length = int(mave_length * TRAIN_SPLIT)
mave_train_indices = random.sample(range(mave_length), mave_train_length)
mave_test_indices = [i for i in range(mave_length) if i not in mave_train_indices]
print("yelp train:", len(yelp_train_indices), "test:", len(yelp_test_indices))
print("mave train:", len(mave_train_indices), "test:", len(mave_test_indices))
# ### Create Binary Dataset
# In[30]:
# train
with open(PATH_TO_BINARY_RESULTS + '_train.txt', 'w', encoding="utf8") as f:
# write all except the last line
for i in yelp_train_indices:
f.write('service\t' + yelp_texts[i][1] + '\n')
for i in mave_train_indices[:-1]:
f.write('product\t' + mave_texts[i][1] + '\n')
f.write('product\t' + mave_texts[mave_train_indices[-1]][1])
# test
with open(PATH_TO_BINARY_RESULTS + '_test.txt', 'w', encoding="utf8") as f:
# write all except the last line
for i in yelp_test_indices:
f.write('service\t' + yelp_texts[i][1] + '\n')
for i in mave_test_indices[:-1]:
f.write('product\t' + mave_texts[i][1] + '\n')
f.write('product\t' + mave_texts[mave_test_indices[-1]][1])
# ### Create Multiclass Dataset
# In[31]:
# train
with open(PATH_TO_RESULTS + '_train.txt', 'w', encoding="utf8") as f:
# write all except the last line
for i in yelp_train_indices:
f.write(yelp_texts[i][0] + '\t' + yelp_texts[i][1] + '\n')
for i in mave_train_indices[:-1]:
f.write(mave_texts[i][0] + '\t' + mave_texts[i][1] + '\n')
f.write(mave_texts[mave_train_indices[-1]][0] + '\t' + mave_texts[mave_train_indices[-1]][1])
# test
with open(PATH_TO_RESULTS + '_test.txt', 'w', encoding="utf8") as f:
# write all except the last line
for i in yelp_test_indices:
f.write(yelp_texts[i][0] + '\t' + yelp_texts[i][1] + '\n')
for i in mave_test_indices[:-1]:
f.write(mave_texts[i][0] + '\t' + mave_texts[i][1] + '\n')
f.write(mave_texts[mave_test_indices[-1]][0] + '\t' + mave_texts[mave_test_indices[-1]][1])