-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclassifier.py
More file actions
executable file
·44 lines (35 loc) · 1.19 KB
/
Copy pathclassifier.py
File metadata and controls
executable file
·44 lines (35 loc) · 1.19 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
#!/usr/bin/env python
import pickle
import nltk
import nltk.classify
from nltk.classify import NaiveBayesClassifier
def bag_of_words(text):
tokens = nltk.wordpunct_tokenize(text)
lemma = nltk.WordNetLemmatizer()
tokens = [lemma.lemmatize(token.lower()) for token in tokens]
tokens = [token for token in tokens if len(token) >= 6]
if len(tokens) == 0:
return None
bag = dict([(word, True) for word in nltk.Text(tokens)])
return bag
class NPSClassifier(object):
def __init__(self):
self._classifier = None
def train(self, scored_texts):
# Train classifier on each of the scored texts provided.
self._classifier = NaiveBayesClassifier.train(scored_texts)
def classify(self, tweet):
bag = bag_of_words(tweet)
if not bag:
return None
try:
return self._classifier.classify(bag)
except Exception:
return None
def save_to_file(self, filename):
with open(filename, 'wb') as outf:
pickle.dump(self._classifier, outf, 1)
@classmethod
def load_from_file(cls, filename):
with open(filename) as inf:
return pickle.load(inf)