-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathutils.py
203 lines (157 loc) · 5.66 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import spacy, os, numpy as np
import scipy.sparse as sp
from tqdm import tqdm
from gensim.corpora import Dictionary as gensim_dico
nlp = spacy.load("en_core_web_sm")
def parse_processed_amazon_dataset(FNames, max_words):
datasets = {}
dico = gensim_dico()
# First pass on document to build dictionary
for fname in FNames:
f = open(fname)
for l in f:
tokens = l.split(sep=' ')
label_string = tokens[-1]
tokens_list=[]
for tok in tokens[:-1]:
ts, tfreq = tok.split(':')
freq = int(tfreq)
tokens_list += [ts]*freq
_ = dico.doc2bow(tokens_list, allow_update=True)
f.close()
# Preprocessing_options
dico.filter_extremes(no_below=2, keep_n=max_words)
dico.compactify()
for fname in FNames:
X = []
Y = []
docid = -1
f = open(fname)
for l in f:
tokens = l.split(sep=' ')
label_string = tokens[-1]
tokens_list = []
for tok in tokens[:-1]:
ts, tfreq = tok.split(':')
freq = int(tfreq)
tokens_list += [ts]*freq
count_list = dico.doc2bow(tokens_list, allow_update=False)
docid += 1
X.append((docid, count_list))
# Preprocess Label
ls, lvalue = label_string.split(':')
if ls == "#label#":
if lvalue.rstrip() == 'positive':
lv = 1
Y.append(lv)
elif lvalue.rstrip() == 'negative':
lv = 0
Y.append(lv)
else:
raise Exception("Invalid Label Value")
else:
raise Exception('Invalid Format')
datasets[fname] = (X, np.array(Y))
f.close()
del f
return datasets, dico
def count_list_to_sparse_matrix(X_list, dico):
ndocs = len(X_list)
voc_size = len(dico.keys())
X_spmatrix = sp.lil_matrix((ndocs, voc_size))
for did, counts in X_list:
for wid, freq in counts:
X_spmatrix[did, wid]=freq
return X_spmatrix.tocsr()
def get_dataset_path(domain_name, exp_type):
prefix ='./dataset/'
if exp_type == 'small':
fname = 'labelled.review'
elif exp_type == 'all':
fname = 'all.review'
elif exp_type == 'test':
fname = 'unlabeled.review'
return os.path.join(prefix, domain_name, fname)
def get_domain_dataset(domain_name, max_words=5000, exp_type='small'):
"""
Returns training (small) / test split of a single domain
"""
domain_path = get_dataset_path(domain_name, exp_type)
datasets, dico = parse_processed_amazon_dataset([domain_path], max_words)
L_s, _ = datasets[domain_path]
X_s = count_list_to_sparse_matrix(L_s,dico)
X_s = np.array(X_s.todense())
return X_s, dico
def get_dataset(source_name, target_name, max_words=5000):
"""
Returns source domain, target domain paired dataset
"""
source_path = get_dataset_path(source_name, 'small')
target_path1 = get_dataset_path(target_name, 'small')
target_path2 = get_dataset_path(target_name, 'test')
dataset_list = [source_path, target_path1, target_path2]
datasets, dico = parse_processed_amazon_dataset(dataset_list, max_words)
L_s, Y_s = datasets[source_path]
L_t1, Y_t1 = datasets[target_path1]
L_t2, Y_t2 = datasets[target_path2]
X_s = count_list_to_sparse_matrix(L_s, dico)
X_t1 = count_list_to_sparse_matrix(L_t1, dico)
X_t2 = count_list_to_sparse_matrix(L_t2, dico)
return X_s, Y_s, X_t1, Y_t1, X_t2, Y_t2, dico
def spacy_seed_concepts(dico):
"""
Returns concepts which belongs to proper noun, noun, adjective, or adverb parts-of-speech-tag category
"""
seeds = []
concepts = list(dico.values())
tags = ['PROPN', 'NOUN', 'ADJ', 'ADV']
for item in tqdm(concepts):
if '_' not in item:
doc = nlp(item)
switch = 0
for token in doc:
if token.pos_ not in tags:
switch = 1
break
else:
continue
if switch == 0:
seeds.append(item)
return set(seeds)
def spacy_seed_concepts_list(concepts):
"""
Returns concepts which belongs to proper noun, noun, adjective, or adverb parts-of-speech-tag category
"""
seeds = []
tags = ['PROPN', 'NOUN', 'ADJ', 'ADV']
for item in concepts:
if '_' not in item:
doc = nlp(item)
switch = 0
for token in doc:
if token.pos_ not in tags:
switch = 1
break
else:
continue
if switch == 0:
seeds.append(item)
return set(seeds)
def obtain_all_seed_concepts(max_words):
"""
Returns seed concepts drwan from all the domains
"""
_, dico1 = get_domain_dataset('dvd', max_words)
_, dico2 = get_domain_dataset('electronics', max_words)
_, dico3 = get_domain_dataset('kitchen', max_words)
_, dico4 = get_domain_dataset('books', max_words)
concepts = list(set(dico1.values())\
.union(set(dico2.values()))\
.union(set(dico3.values()))\
.union(set(dico4.values())))
seeds1 = spacy_seed_concepts(dico1)
seeds2 = spacy_seed_concepts(dico2)
seeds3 = spacy_seed_concepts(dico3)
seeds4 = spacy_seed_concepts(dico4)
all_seeds = list(seeds1.union(seeds2).union(seeds3).union(seeds4))
return all_seeds