-
Notifications
You must be signed in to change notification settings - Fork 33
/
bag_of_words.py
54 lines (45 loc) · 1.62 KB
/
bag_of_words.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from nltk.corpus import movie_reviews
from collections import defaultdict
def count_features(bag_of_words, features, polarity):
for lst in features:
for word in lst[0].keys():
bag_of_words[polarity][word] += 1
return bag_of_words
def train_bag_of_words():
"""
@return: dictionary
bag_of_words['neg']['word'] ==> count
bag_of_words['pos']['word'] ==> count
"""
def word_feats(words):
return dict([(word, True) for word in words])
bag_of_words = {}
bag_of_words['neg'] = defaultdict(int)
bag_of_words['pos'] = defaultdict(int)
negids = movie_reviews.fileids('neg')
posids = movie_reviews.fileids('pos')
negfeats = [(word_feats(movie_reviews.words(fileids=[f])), 'neg')
for f in negids]
posfeats = [(word_feats(movie_reviews.words(fileids=[f])), 'pos')
for f in posids]
bag_of_words = count_features(bag_of_words, negfeats, 'neg')
bag_of_words = count_features(bag_of_words, posfeats, 'pos')
return bag_of_words
def classify_polarity(bag_of_words):
"""
Pops word from bag_of_words['neg'/'pos'] if the word appears
more in 'pos/'neg' respectively
"""
for word, count in bag_of_words['neg'].copy().items():
if count > bag_of_words['pos'][word]:
bag_of_words['pos'].pop(word)
else:
bag_of_words['neg'].pop(word)
return bag_of_words
if __name__ == '__main__':
bag_of_words = train_bag_of_words()
bag_of_words = classify_polarity(bag_of_words)
print(bag_of_words)