-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathread_data.py
177 lines (124 loc) · 5.97 KB
/
read_data.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
# -*- coding: utf-8 -*-
"""
@author: alexyang
@contact: alex.yang0326@gmail.com
@file: read_data.py
@time: 2018/4/21 8:06
@desc:
"""
import numpy as np
import pandas as pd
import nltk
from utils import onehot_encoding
def isinteger(_str):
return _str.strip().isdigit()
def isfloat(_str):
return sum([n.isdigit() for n in _str.strip().split('.')]) == 2
def get_index(_list, item, start_index=0):
for i in range(start_index, len(_list)):
if item == _list[i]:
return i
raise ("can not find %s in %s" % (item, list))
def read_data_for_aspect(fname, pre_trained_vectors, embedding_size):
data_csv = pd.read_csv(fname, sep='\t', header=None, index_col=None,
names=['text', 'aspect_l1', 'aspect_l2', 'score'])
data_csv = data_csv.sample(frac=1).reset_index(drop=True) # shuffle data
stop_words = [',', '.', ':', ';', '?', '(', ')', '[', ']', '!', '@', '#', '%', '$', '*', '-', '/', '&', '``', "''"]
max_context_len = 0
word2idx = {} # Index 0 represents words we haven't met before
context = []
context_len = []
aspect_class = []
for index, row in data_csv.iterrows():
word_list = nltk.word_tokenize(row.text.strip())
context_words = [word for word in word_list
if word not in stop_words and isinteger(word) is False and isfloat(word) is False
and word != 'T']
words_have_vector = [word for word in context_words if word in pre_trained_vectors]
# make sure most words can find their embedding vectors
if len(words_have_vector) / float(len(context_words)) < 0.8:
continue
max_context_len = max(max_context_len, len(words_have_vector))
idx = []
for word in words_have_vector:
if word not in word2idx:
word2idx[word] = len(word2idx)+1 # Index 0 represents absent words, so start from 1
idx.append(word2idx[word])
context.append(idx)
context_len.append(len(words_have_vector))
aspect_class.append(row.aspect_l1 + '/' + row.aspect_l2)
# convert to numpy format
context_npy = np.zeros(shape=[len(context), max_context_len])
for i in range(len(context)):
context_npy[i, :len(context[i])] = context[i]
aspect_class_npy, onehot_mapping = onehot_encoding(aspect_class)
train_data = list()
train_data.append(context_npy) # [data_size, max_context_len]
train_data.append(np.array(context_len)) # [data_size,]
train_data.append(aspect_class_npy) # [data_size, aspect_class]
word_embeddings = np.zeros([len(word2idx)+1, embedding_size])
for word in word2idx.keys():
word_embeddings[word2idx[word]] = pre_trained_vectors[word]
return train_data, word_embeddings, word2idx, max_context_len, onehot_mapping
def read_data_for_senti(fname, pre_trained_vectors, embedding_size, one_aspect):
data_csv = pd.read_csv(fname, sep='\t', header=None, index_col=None,
names=['text', 'aspect_l1', 'aspect_l2', 'score'])
data_csv = data_csv.sample(frac=1).reset_index(drop=True) # shuffle data
stop_words = [',', '.', ':', ';', '?', '(', ')', '[', ']', '!', '@', '#', '%', '$', '*', '-', '/', '&', '``', "''"]
max_context_len = 0
word2idx = {} # Index 0 represents words we haven't met before
aspect2idx = {}
context = []
context_len = []
loc_info = []
aspect = []
score = []
# aspect_class = []
for index, row in data_csv.iterrows():
word_list = nltk.word_tokenize(row.text.strip())
context_words = [word for word in word_list
if word not in stop_words and isinteger(word) is False and isfloat(word) is False
and word != 'T']
words_have_vector = [word for word in context_words if word in pre_trained_vectors]
# make sure most words can find their embedding vectors
if len(words_have_vector) / float(len(context_words)) < 0.8:
continue
max_context_len = max(max_context_len, len(words_have_vector))
idx, distance = [], []
start_index = 0
stock_loc = get_index(word_list, 'T')
for word in words_have_vector:
if word not in word2idx:
word2idx[word] = len(word2idx)+1 # Index 0 represents absent words, so start from 1
idx.append(word2idx[word])
word_loc = get_index(word_list, word, start_index=start_index)
start_index = word_loc + 1
distance.append(1 - abs(word_loc-stock_loc) / len(word_list))
context.append(idx)
context_len.append(len(words_have_vector))
loc_info.append(distance)
if one_aspect is True: # consider there is only one abstract aspect
aspect.append(0)
else:
if row.aspect_l2 not in aspect2idx:
aspect2idx[row.aspect_l2] = len(aspect2idx)
aspect.append(aspect2idx[row.aspect_l2])
score.append([row.score])
if one_aspect is True:
aspect2idx['one_aspect'] = len(aspect2idx)
# convert to numpy format
context_npy = np.zeros(shape=[len(context), max_context_len])
loc_info_npy = np.zeros(shape=[len(loc_info), max_context_len])
for i in range(len(context)):
context_npy[i, :len(context[i])] = context[i]
loc_info_npy[i, :len(loc_info[i])] = loc_info[i]
train_data = list()
train_data.append(context_npy) # [data_size, max_context_len]
train_data.append(np.array(context_len)) # [data_size,]
train_data.append(loc_info_npy) # [data_size, max_context_len]
train_data.append(np.array(aspect)) # [data_size]
train_data.append(np.array(score)) # [data_size, 1]
word_embeddings = np.zeros([len(word2idx)+1, embedding_size])
for word in word2idx.keys():
word_embeddings[word2idx[word]] = pre_trained_vectors[word]
return train_data, word_embeddings, word2idx, aspect2idx, max_context_len