-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmethods.py
More file actions
80 lines (69 loc) · 2.63 KB
/
Copy pathmethods.py
File metadata and controls
80 lines (69 loc) · 2.63 KB
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
from gensim import utils
from gensim.models.doc2vec import LabeledSentence
from gensim.models import Doc2Vec
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import train_test_split
from sklearn.cross_validation import ShuffleSplit
from sklearn.grid_search import GridSearchCV
from sklearn.learning_curve import learning_curve
from sklearn.linear_model import LogisticRegressionCV
from gensim.utils import smart_open, simple_preprocess
from sklearn.feature_extraction.text import ENGLISH_STOP_WORDS
from gensim.parsing.preprocessing import STOPWORDS
from time import time
import string
import re
# numpy
import numpy as np
import pickle
# random
from random import shuffle
# classifier
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import fetch_20newsgroups
def get_newsgroup_train():
#load 20 newsgroup data
newsgroup_train = fetch_20newsgroups(subset='train',
remove=('headers', 'footers', 'quotes'))
return newsgroup_train
def get_newsgroup_test():
newsgroup_test = fetch_20newsgroups(subset = 'test',remove=('headers', 'footers', 'quotes'))
return newsgroup_test
def get_newsgroup_all():
newsgroup_all = fetch_20newsgroups(remove = ('headers', 'footers', 'quotes'))
return newsgroup_all
def tokenize(text):
return [token for token in simple_preprocess(text) if token not in STOPWORDS]
def generateSentences(newsgroup_data):
result = []
for data in newsgroup_data:
for element in data.split('.'):
result.append(tokenize(element))
return result
def randomizeAndTrain(file,sentences,model,n_iter):
t0 = time()
model.train(sentences)
print("done in %0.3fs." % (time() - t0))
for i in range(1, n_iter+1):
random.shuffle(sentences)
t0 = time()
model.train(sentences)
print("done in %0.3fs." % (time() - t0))
f.write("done in %0.3fs." % (time() - t0))
return model
def tokenizer(document):
text = "".join([ch for ch in document if ch not in string.punctuation])
text_list = text.split()
normalized_text = [x.lower() for x in text_list]
# Define an empty list
nostopwords_text = []
# Scan the words
for word in normalized_text:
# Determine if the word is contained in the stop words list
if word not in ENGLISH_STOP_WORDS:
# If the word is not contained I append it
nostopwords_text.append(word)
tokenized_text = [word for word in nostopwords_text if re.search('[a-zA-Z]{2,}', word)]
return tokenized_text