-
Notifications
You must be signed in to change notification settings - Fork 0
/
classify.py
69 lines (62 loc) · 1.93 KB
/
classify.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
import os.path
import os
from NaiveBayes import NaiveBayesClassifier
from nbio import load_words, load_classifier
def classify( infile, data_dir, k_smooth = 1 ):
'''
assign classification labels to documents using trained classifier.
'''
#initialize classifier
nbc = load_classifier( infile )
print('classifier loaded')
#load inputs
fws = []
for fn in os.listdir( data_dir ):
ws = load_words( os.path.join( data_dir, fn ) )
fws.append( (fn, ws) )
print('loaded %s files for classification' % len(fws) )
#classify
for (f, ws) in fws:
(l,u) = nbc.classify( ws, k_smooth )
print('%s classified as %s LPG=%f' % (f, l,u) )
if __name__ == '__main__':
import getopt
import sys
def usage():
''' show usage message and exit '''
print('classify (txtcat-nb)')
print('options:')
print('\t-h --help\t\t\tshow this usage information')
print('\t-m --model=\t\t\tclassification model file')
print('\t-d --datadir=\t\t\tdata directory')
print('\t-k --smooth=\t\t\tsmoothing constant (default 1)')
sys.exit(1)
try:
opts, args = getopt.getopt( sys.argv[1:], 'hm:d:k:',['help','model=','datadir=','smooth='])
except getopt.GetoptError as err:
print(err)
usage()
if 0 != len(args):
usage()
mfile = None
ddir = None
k = 1
for o,a in opts:
if o in ('-h',' --help'):
usage()
elif o in ('-m', '--model'):
mfile = a
elif o in ('-d', '--datadir'):
ddir = a
elif o in ('-k', '--smooth'):
k = int( a )
assert k >= 0, 'smoothing constant must be non-negative'
else:
print('unrecognized option %s (argument %s)' % (o,a) )
usage()
if None == mfile:
usage()
if None == ddir:
usage()
classify( mfile, ddir, k )