-
Notifications
You must be signed in to change notification settings - Fork 1
/
vectorize.py
84 lines (69 loc) · 2.68 KB
/
vectorize.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
"""
Helper script used for turning text into tf-idf vector for the knn experiment
"""
import re
import numpy
from nltk import pos_tag
from nltk.corpus import stopwords
from nltk.corpus import wordnet
from nltk.stem import SnowballStemmer
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize
from sklearn.feature_extraction.text import TfidfVectorizer
def cleanText(text, lemmatize, stemmer):
"""Method for cleaning text. Removes numbers, punctuation, and capitalization. Stems or lemmatizes text."""
if isinstance(text, float):
text = str(text)
if isinstance(text, numpy.int64):
text = str(text)
try:
text = text.decode()
except AttributeError:
pass
text = re.sub(r"[^A-Za-z]", " ", text)
text = text.lower()
if lemmatize:
wordnet_lemmatizer = WordNetLemmatizer()
def get_tag(tag):
if tag.startswith('J'):
return wordnet.ADJ
elif tag.startswith('V'):
return wordnet.VERB
elif tag.startswith('N'):
return wordnet.NOUN
elif tag.startswith('R'):
return wordnet.ADV
else:
return ''
text_result = []
tokens = word_tokenize(text) # Generate list of tokens
tagged = pos_tag(tokens)
for t in tagged:
try:
text_result.append(wordnet_lemmatizer.lemmatize(t[0], get_tag(t[1][:2])))
except:
text_result.append(wordnet_lemmatizer.lemmatize(t[0]))
return text_result
if stemmer:
text_result = []
tokens = word_tokenize(text)
snowball_stemmer = SnowballStemmer('english')
for t in tokens:
text_result.append(snowball_stemmer.stem(t))
return text_result
def createTFIDF(train, test, remove_stopwords, lemmatize, stemmer):
if remove_stopwords:
vectorizer = TfidfVectorizer(analyzer='word', input='content', stop_words=stopwords.words('english'))
else:
vectorizer = TfidfVectorizer(analyzer='word', input='content')
clean_train = []
for paragraph in train:
paragraph_result = cleanText(paragraph, lemmatize, stemmer)
paragraph = " ".join(str(x) for x in paragraph_result)
clean_train.append(paragraph)
paragraph_result = cleanText(test, lemmatize, stemmer)
paragraph = " ".join(str(x) for x in paragraph_result)
clean_test = paragraph
tfidf_train = vectorizer.fit_transform(clean_train).toarray()
tfidf_test = vectorizer.transform([clean_test]).toarray()
return tfidf_train, tfidf_test