-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2clean_words.py
More file actions
27 lines (21 loc) · 785 Bytes
/
Copy path2clean_words.py
File metadata and controls
27 lines (21 loc) · 785 Bytes
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
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.datasets import fetch_20newsgroups
from nltk.corpus import names
from nltk.stem import WordNetLemmatizer
def letters_only(astr):
for c in astr:
if not c.isalpha():
return False
return True
cv = CountVectorizer(stop_words="english", max_features=500)
groups = fetch_20newsgroups()
cleaned = []
all_names = set(names.words())
lemmatizer = WordNetLemmatizer()
for post in groups.data:
cleaned.append(' '.join([lemmatizer.lemmatize(word.lower())
for word in post.split()
if letters_only(word)
and word not in all_names]))
transformed = cv.fit_transform(cleaned)
print(cv.get_feature_names())