forked from nithya4/CS-585-Political-Ideology-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetStats.py
More file actions
51 lines (42 loc) · 1.7 KB
/
Copy pathgetStats.py
File metadata and controls
51 lines (42 loc) · 1.7 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
from sklearn.feature_extraction.text import CountVectorizer
from collections import defaultdict
import nltk
nltk.download("stopwords")
from nltk.corpus import stopwords
from nltk.tokenize import RegexpTokenizer
total_counts = defaultdict(float)
lib_words_counts = defaultdict(float)
con_words_counts = defaultdict(float)
def tokenize_doc(doc):
'''
Tokenize a document and return its bag-of-words representation.
doc - a string representing a document.
returns a dictionary mapping each word to the number of times it appears in doc.
'''
tokens = doc.split()
lowered_tokens = map(lambda t: t.lower(), tokens)
lowered_tokens = [word for word in lowered_tokens if word not in stopwords.words('english')]
for token in lowered_tokens:
total_counts[token] += 1.0
def tokenize(doc):
toker = RegexpTokenizer(r'((?<=[^\w\s])\w(?=[^\w\s])|(\W))+', gaps=True)
tokens = toker.tokenize(doc)
lowered_tokens = map(lambda t: t.lower(), tokens)
lowered_tokens = [word for word in lowered_tokens if word not in stopwords.words('english')]
return lowered_tokens
def top_n(n):
print sorted(lib_words_counts.items(), key=lambda (w,c): -c)[:n]
print sorted(con_words_counts.items(), key=lambda (w,c): -c)[:n]
def generate_lib_con_counts(lib_result, con_result):
for each_sent in lib_result:
tokens = tokenize(each_sent)
for each in tokens:
lib_words_counts[each] += 1
for each_sent in con_result:
tokens = tokenize(each_sent)
for each in tokens:
con_words_counts[each] += 1
def run(corpus, lib_result, con_result):
# for each in corpus:
# tokenize_doc(each)
generate_lib_con_counts(lib_result, con_result)