-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
executable file
·36 lines (28 loc) · 1.03 KB
/
train.py
File metadata and controls
executable file
·36 lines (28 loc) · 1.03 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import pickle
import logging
from textblob.classifiers import NaiveBayesClassifier
logger = logging.getLogger('marlowe.redobject.train')
def save_classifier(classifier):
f = open('objectivity_classifier.pickle', 'wb')
pickle.dump(classifier, f, -1)
f.close()
def load_classifier():
f = open('objectivity_classifier.pickle', 'rb')
classifier = pickle.load(f)
f.close()
return classifier
if __name__ == '__main__':
logger.warning('Loading objectivity dataset')
with open('../dataset/objectivity.json', 'r') as fp:
# split data to train and test data, going with 80/20 to start
dataset = json.load(fp)
train_data = dataset[int(len(dataset) * .2):]
test_data = dataset[:int(len(dataset) * .2)]
logger.warning('Loaded dataset, training classifier')
logger.warning('Length of training data %s' % len(train_data))
cl = NaiveBayesClassifier(train_data)
logger.warning(cl.accuracy(test_data))
save_classifier(cl)