-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn.py
executable file
·32 lines (26 loc) · 1.03 KB
/
learn.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
import sys
import nltk
from nltk_classifier_helpers import *
trainfilename = sys.argv[1]
trainsetname = basename(trainfilename)
shuffle = False
test_prop = 0.1
# load dataset
ds = Dataset()
ds.load(trainfilename, shuffle)
# prepare test and training datasets
(test_set, train_set) = ds.makesets(test_prop)
# Naive Bayes
classifier = makeClassifier(nltk.NaiveBayesClassifier, train_set, trainsetname+'.NaiveBayesClassifier.pkl')
print classifier.__class__.__name__
# Decision Tree
classifier = makeClassifier(nltk.DecisionTreeClassifier, train_set, trainsetname+'.DecisionTreeClassifier.pkl')
print classifier.__class__.__name__
DecisionTreeClassifierHelper(classifier)
with open(trainsetname+'.DecisionTreeClassifiers.py', 'w') as f:
f.write("import nltk\n\n")
for i in (1,2,3,4,5,6) :
f.write(classifier.pythonclasscode("MyDecisionTreeClassifier"+str(i), depth=i))
# Maxent
classifier = makeClassifier(nltk.MaxentClassifier, train_set, trainsetname+'.MaxentClassifier.pkl', { "algorithm": "GIS"})
print classifier.__class__.__name__