-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_vec.py
193 lines (159 loc) · 8.51 KB
/
feature_vec.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
import os, argparse, re, pickle, spacy, json
import numpy as np
import sklearn
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.datasets import load_files
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from scipy import sparse
parser = argparse.ArgumentParser(description = 'Creating Cusotmized Feature Vector')
parser.add_argument('--vect', default = 'bow', type = str, help = 'Specify vectorization method.')
parser.add_argument('--dep', default = False, type =bool, help='Add dependency relationship?')
parser.add_argument('--ngram', default = 3, type = int, help = 'Specify ngram range.')
parser.add_argument('--num_feat', default = 1000000, type = int, help='Specify the maximum number of features')
parser.add_argument('--verbose',default = False, type=bool, help="Print progress?")
parser.add_argument('--maxdf',default = 1, type=float, help="Maximum word frequency cap")
parser.add_argument('--mindf',default = 1, type=int, help="Lowest word presence in document")
# <-------------------------- Read Data ---------------------------->
def readData(rootPath):
category = ["pos","neg"]
#load only labeled data
movie_train = load_files(rootPath + "aclImdb/train", shuffle=True, categories=category)
movie_test = load_files(rootPath + "aclImdb/test", shuffle=True, categories=category)
return [movie_train, movie_test]
# defines a custom vectorizer class
class CustomVectorizer(CountVectorizer):
count = 0
# overwrite the build_analyzer method, allowing one to
# create a custom analyzer for the vectorizer
def build_analyzer(self):
# load stop words using CountVectorizer's built in method
stop_words = self.get_stop_words()
# create the analyzer that will be returned by this method
def analyser(doc):
doc = doc.decode("utf-8-sig")
doc = re.sub(r'(\s*<br.*?>)+\s*', " ", doc)
doc = re.sub("[^a-zA-Z]+", " ", doc)
# load spaCy's model for english language
spacy.load('en')
# instantiate a spaCy tokenizer
lemmatizer = spacy.lang.en.English()
# apply the preprocessing and tokenzation steps
doc_clean = (doc).lower()
tokens = lemmatizer(doc_clean)
lemmatized_tokens = [token.lemma_ for token in tokens]
# print(lemmatized_tokens)
# use CountVectorizer's _word_ngrams built in method
# to remove stop words and extract n-grams
# for that sentence, find the unigram and bigram
unigram_bigram = self._word_ngrams(lemmatized_tokens, stop_words)
count = CustomVectorizer.count
progress = CustomVectorizer.count/25000*100
CustomVectorizer.count += 1
if verbose:
print (progress,"% processed")
return(unigram_bigram)
return(analyser)
class CustomVectorizer_WithDep(CountVectorizer):
count = 0
# overwrite the build_analyzer method, allowing one to
# create a custom analyzer for the vectorizer
def build_analyzer(self):
# load stop words using CountVectorizer's built in method
stop_words = self.get_stop_words()
# create the analyzer that will be returned by this method
def analyser(doc):
doc = doc.decode("utf-8-sig")
doc = re.sub(r'(\s*<br.*?>)+\s*', " ", doc)
doc = re.sub("[^a-zA-Z]+", " ", doc)
# load spaCy's model for english language
spacy.load('en')
# instantiate a spaCy tokenizer
lemmatizer = spacy.lang.en.English()
# apply the preprocessing and tokenzation steps
doc_clean = (doc).lower()
tokens = lemmatizer(doc_clean)
lemmatized_tokens = [token.lemma_ for token in tokens]
# use CountVectorizer's _word_ngrams built in method
# to remove stop words and extract n-grams
# for that sentence, find the unigram and bigram
unigram_bigram = self._word_ngrams(lemmatized_tokens, stop_words)
count = CustomVectorizer.count
lst_pair = dependency_dic[str(count)]
for pair in lst_pair:
temp_str = "" + str(pair[0]) + " " + str(pair[1])
unigram_bigram.append(temp_str)
progress = CustomVectorizer.count/25000*100
CustomVectorizer.count += 1
if verbose:
print (progress,"% processed")
return(unigram_bigram)
return(analyser)
def main():
args = parser.parse_args()
print("Loading data...")
train_data, test_data = readData("")
global verbose
verbose = args.verbose
if args.dep:
# <---------------------train data--------------------->
global dependency_dic
with open('train_data.json') as json_file:
dependency_dic = json.load(json_file)
print("Creating customized feature vector on training data...")
custom_vectorizer = CustomVectorizer_WithDep(ngram_range=(1,args.ngram),
stop_words='english',
encoding="utf-8-sig",
token_pattern=r"(?u)\b\w\w+\b",
max_df=args.maxdf, min_df=args.mindf, lowercase=True,
max_features=args.num_feat)
custom_vector = custom_vectorizer.fit_transform(train_data.data)
print("Dumping training data vector...")
with open('vector-%s-%s-%s-dep-%s.pickle' % (args.ngram, args.maxdf,
args.mindf, args.dep), 'wb') as f:
pickle.dump(custom_vector, f, pickle.HIGHEST_PROTOCOL)
print("Dumping feature names...")
with open('vector-%s-%s-%s-dep-%s-feature_name.pickle' % (args.ngram, args.maxdf,
args.mindf,
args.dep),"wb") as f:
pickle.dump(custom_vectorizer.get_feature_names(), f, pickle.HIGHEST_PROTOCOL)
# <---------------------test data--------------------->
CustomVectorizer.count = 0
with open('test_data.json') as json_file:
dependency_dic = json.load(json_file)
print("Fitting customized feature vector on testing data...")
test_vector = custom_vectorizer.transform(test_data.data)
print("Dumping test data vector...")
with open('vector-%s-%s-%s-dep-%s-test.pickle' % (args.ngram, args.maxdf,
args.mindf, args.dep), 'wb') as f:
pickle.dump(test_vector, f, pickle.HIGHEST_PROTOCOL)
else:
# <---------------------train data--------------------->
print("Creating customized feature vector on training data...")
custom_vectorizer = CustomVectorizer(ngram_range=(1,args.ngram),
stop_words='english',
encoding="utf-8-sig",
token_pattern=r"(?u)\b\w\w+\b",
max_df=args.maxdf, min_df=args.mindf, lowercase=True,
max_features=args.num_feat)
custom_vector = custom_vectorizer.fit_transform(train_data.data)
print("Dumping training data vector...")
with open('vector-%s-%s-%s-dep-%s.pickle' % (args.ngram, args.maxdf,
args.mindf, args.dep), 'wb') as f:
pickle.dump(custom_vector, f, pickle.HIGHEST_PROTOCOL)
print("Dumping feature names...")
with open('vector-%s-%s-%s-dep-%s-feature_name.pickle' % (args.ngram, args.maxdf,
args.mindf, args.dep),"wb") as f:
pickle.dump(custom_vectorizer.get_feature_names(), f, pickle.HIGHEST_PROTOCOL)
# <---------------------test data--------------------->
CustomVectorizer.count = 0
print("Fitting customized feature vector on testing data...")
test_vector = custom_vectorizer.transform(test_data.data)
print("Dumping test data vector...")
with open('vector-%s-%s-%s-dep-%s-test.pickle' % (args.ngram, args.maxdf,
args.mindf, args.dep), 'wb') as f:
pickle.dump(test_vector, f, pickle.HIGHEST_PROTOCOL)
if __name__ == '__main__':
main()