-
Notifications
You must be signed in to change notification settings - Fork 0
/
step1_lib.py
344 lines (280 loc) · 11.7 KB
/
step1_lib.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""
Step1 - Classify News as 'D' if they are about natural disasters.
Portuguese news only.
"""
__author__ = 'eduardo'
from sklearn.linear_model import SGDClassifier
import codecs
from nltk.stem import RSLPStemmer
from gensim.corpora import Dictionary
from newspaper import Article
from nltk.tokenize import word_tokenize
import math
from scipy.sparse import csr_matrix
import numpy as np
import pickle
import feedparser
import pandas as pd
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.feature_extraction.text import CountVectorizer
IDF_PATH = "../data/idf.txt"
class PreProcessor(object):
def __init__(self, idf_path=IDF_PATH, use_idf=True):
self.stemmer = RSLPStemmer()
self.term_dict, self.freq = self.dict_from_idf(idf_path)
self.max_freq = float(max(self.freq.values()))
self.vocab_size = len(self.term_dict)
self.use_idf = use_idf
def dict_from_idf(self, idf_path):
my_dict = Dictionary()
freq_dict = {}
with codecs.open(idf_path, mode="rb", encoding="utf8") as in_file:
for line in in_file:
splitted_line = line.split(" ")
stemmed_word = self.stemmer.stem(splitted_line[0])
frequency = int(splitted_line[1])
if frequency < 5:
break
else:
id_tuple = my_dict.doc2bow([stemmed_word], allow_update=True)
word_id = id_tuple[0][0]
freq_dict[word_id] = frequency + freq_dict.setdefault(word_id, 0)
return my_dict, freq_dict
def idf(self, term_id):
return 1#math.log(self.max_freq/self.freq[term_id])
def url_to_bow(self, url):
print url
tokenized_doc = http2tokenized_stemmed(url)
bow_doc = self.term_dict.doc2bow(tokenized_doc)
new_bow_doc = []
for i in range(0, len(bow_doc)):
new_bow_doc.append((bow_doc[i][0], bow_doc[i][1]*self.idf(bow_doc[i][0])))
if self.use_idf:
return new_bow_doc
else:
return bow_doc
def corpus_from_urllist(self, url_list, label):
urls = url_list
docs_bow = [self.url_to_bow(url) for url in urls]
labels = [label] * len(urls)
return NewsCorpus(urls, labels, docs_bow, self.vocab_size)
def http2tokenized_stemmed(url):
article = Article(url, language="pt")
article.download()
article.parse()
full_text = 3 * (article.title + " ") + article.text
return word_tokenize(full_text)
def urltxt2url_generator(in_file):
with open(in_file, "rb") as read_file:
for line in read_file:
yield line
def feed2url_generator(feed):
d = feedparser.parse(feed)
for entry in d.entries:
yield entry.link.split("url=")[1]
def rss_list2url_generator(in_file):
with open(in_file, "rb") as read_file:
for line in read_file:
for url in feed2url_generator(line):
yield url
def generator2txt(gen, out_file):
with open(out_file, "wb") as write_file:
for line in gen:
write_file.write(line + "\n")
def batch_get_corpus(preprocessor, url_lists=None, url_lists_tags=None, rss_lists=None, rss_lists_tags=None):
first = True
if url_lists is not None:
for i, url_list in enumerate(url_lists):
effective_list = list(urltxt2url_generator(url_list))
print "i" + str(i)
if first:
corpus = preprocessor.corpus_from_urllist(effective_list, url_lists_tags[i])
first = False
else:
corpus.concatenate(preprocessor.corpus_from_urllist(effective_list, url_lists_tags[i]))
if rss_lists is not None:
for j, rss_list in enumerate(rss_lists):
effective_list = list(rss_list2url_generator(rss_list))
print "j" + str(j)
if first:
corpus = preprocessor.corpus_from_urllist(effective_list, rss_lists_tags[j])
first = False
else:
corpus.concatenate(preprocessor.corpus_from_urllist(effective_list, rss_lists_tags[j]))
return corpus
class BiGramPreProcessor(PreProcessor):
def __init__(self, url_list=None, vocab=None):
self.stemmer = RSLPStemmer()
self.vectorizer = CountVectorizer(preprocessor=self.stemmer.stem, tokenizer=tokenizer_with_numeric,
ngram_range=(1,2))
if url_list is not None:
self.fit_vocab(url_list)
else:
self.vectorizer.vocabulary_ = vocab
self.vocab_size = len(self.vectorizer.vocabulary_)
def fit_vocab(self, url_list):
text_generator = url2text_generator(url_list)
self.vectorizer.fit(text_generator)
def url_to_bow(self, url):
print url
text_generator = url2text_generator([url])
sparse_matrix = self.vectorizer.transform(text_generator)
return [(sparse_matrix.indices[i], value) for i, value in enumerate(sparse_matrix.data)]
def idf(self, term_id):
return None
def dict_from_idf(self, idf_path):
return None
def tokenizer_with_numeric(text):
return [replace_if_numeric(token) for token in word_tokenize(text)]
def replace_if_numeric(token):
if token.isdigit():
return "NNNnumericNNN"
else:
return token
def url2text_generator(url_list):
for url in url_list:
article = Article(url, language="pt")
article.download()
article.parse()
full_text = (article.title + " ") + article.text
yield full_text
def rss_list_labelize(classifier, preprocessor, rss_list):
urls = list(rss_list2url_generator(rss_list))
corpus = preprocessor.corpus_from_urllist(urls, "ND")
tags = classifier.get_labels(corpus)
corpus.tags = tags
return corpus
def load_classifier(file_path):
"""Load Classifier from pickle"""
with open(file_path, "rb") as in_file:
classifier = pickle.load(in_file)
return classifier
def f_measure(precision, recall):
return 2*precision*recall/(precision+recall)
def load_classifier(file_path):
with open(file_path, "rb") as in_file:
classifier = pickle.load(in_file)
return classifier
class NewsClassifier(object):
def __init__(self, classifier):
self.classifier = classifier
def train(self, newscorpus):
self.classifier.fit(newscorpus.sparse_matrix(), newscorpus.labels)
def get_labels(self, newscorpus):
return self.classifier.predict(newscorpus.sparse_matrix())
def save(self, file_path):
with open(file_path, "wb") as out_file:
pickle.dump(self, out_file)
def cross_validation(self, corpus, proportion, n_fold):
accuracy = []
precision = []
recall = []
for i in range(0, n_fold):
train, test = corpus.random_split_train_test(proportion)
self.train(train)
accuracy.append(test.accuracy(self.get_labels(test)))
precision_recall = test.precision_recall(self.get_labels(test))
precision.append(precision_recall[0])
recall.append(precision_recall[1])
accuracy = np.array(accuracy)
return np.mean(accuracy), np.mean(precision), np.mean(recall)
class NewsCorpus(object):
"""Corpus of news documents"""
def __init__(self, urls, labels, docs_bow, vocab_size):
if len(urls) != len(labels) != len(docs_bow):
raise TypeError("Lists must have the same size")
else:
self.urls = np.array(urls)
self.labels = np.array(labels)
self.docs_bow = np.array(docs_bow)
self.vocab_size = vocab_size
@classmethod
def copy(cls, corpus):
return NewsCorpus(corpus.urls, corpus.labels, corpus.docs_bow, corpus.vocab_size)
def sparse_matrix(self):
indptr = [0]
indices = []
data = []
for i, doc in enumerate(self.docs_bow):
for term_tuple in doc:
index = term_tuple[0]
indices.append(index)
data.append(term_tuple[1])
indptr.append(len(indices))
rows = len(self.docs_bow)
columns = self.vocab_size
sparse_matrix = csr_matrix((data, indices, indptr), shape=(rows, columns), dtype=float)
return sparse_matrix
def concatenate(self, newscorpus):
if not self.vocab_size == newscorpus.vocab_size:
raise TypeError("Vocabulary size is not the same!")
else:
self.urls = np.concatenate((self.urls, newscorpus.urls))
self.docs_bow = np.concatenate((self.docs_bow, newscorpus.docs_bow))
self.labels = np.concatenate((self.labels, newscorpus.labels))
def _data_frame(self):
df = pd.DataFrame()
df["urls"] = self.urls
df["labels"] = self.labels
df["docs_bow"] = self.docs_bow
df["vocab_size"] = [self.vocab_size] * len(self.labels)
return df
def save_csv(self, path):
self._data_frame().to_csv(path, encoding="utf8")
def save_pickle(self, path):
self._data_frame().to_pickle(path)
def random_split_train_test(self, proportion=0.5):
train_markers = (np.random.rand(len(self.urls),) < proportion)
train_corpus = NewsCorpus(self.urls[train_markers], self.labels[train_markers], self.docs_bow[train_markers],
self.vocab_size)
test_corpus = NewsCorpus(self.urls[train_markers == False], self.labels[train_markers == False],
self.docs_bow[train_markers == False], self.vocab_size)
return train_corpus, test_corpus
def accuracy(self, labels):
return float(np.sum(labels == self.labels))/len(self.labels)
def precision_recall(self, labels, positive_label="D"):
labels = np.array(labels)
true_positives = np.sum((labels == self.labels) * (labels == positive_label))
model_positives = np.sum(labels == positive_label)
real_positives = np.sum(self.labels == positive_label)
return float(true_positives)/model_positives , float(true_positives)/real_positives
class PreProcessor(object):
def __init__(self, idf_path=IDF_PATH, use_idf=True):
self.stemmer = RSLPStemmer()
self.term_dict, self.freq = self.dict_from_idf(idf_path)
self.max_freq = float(max(self.freq.values()))
self.vocab_size = len(self.term_dict)
self.use_idf = use_idf
def dict_from_idf(self, idf_path):
my_dict = Dictionary()
freq_dict = {}
with codecs.open(idf_path, mode="rb", encoding="utf8") as in_file:
for line in in_file:
splitted_line = line.split(" ")
stemmed_word = self.stemmer.stem(splitted_line[0])
frequency = int(splitted_line[1])
if frequency < 5:
break
else:
id_tuple = my_dict.doc2bow([stemmed_word], allow_update=True)
word_id = id_tuple[0][0]
freq_dict[word_id] = frequency + freq_dict.setdefault(word_id, 0)
return my_dict, freq_dict
def idf(self, term_id):
return math.log(self.max_freq/self.freq[term_id])
def url_to_bow(self, url):
print url
tokenized_doc = http2tokenized_stemmed(url)
bow_doc = self.term_dict.doc2bow(tokenized_doc)
new_bow_doc = []
for i in range(0, len(bow_doc)):
new_bow_doc.append((bow_doc[i][0], bow_doc[i][1]*self.idf(bow_doc[i][0])))
if self.use_idf:
return new_bow_doc
else:
return bow_doc
def corpus_from_urllist(self, url_list, label):
urls = url_list
docs_bow = [self.url_to_bow(url) for url in urls]
labels = [label] * len(urls)
return NewsCorpus(urls, labels, docs_bow, self.vocab_size)