-
Notifications
You must be signed in to change notification settings - Fork 225
/
combine_classifiers.py
executable file
·70 lines (51 loc) · 2.03 KB
/
combine_classifiers.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
69
70
#!/usr/bin/env python
import argparse, os.path
import nltk.data
from nltk_trainer import dump_object
from nltk_trainer.classification import multi
########################################
## command options & argument parsing ##
########################################
parser = argparse.ArgumentParser(description='Combine NLTK Classifiers')
parser.add_argument('classifiers', nargs='+',
help='one or more pickled classifiers to load and combine')
parser.add_argument('filename', default='~/nltk_data/classifiers/combined.pickle',
help='Filename to pickle combined classifier, defaults to %(default)s')
parser.add_argument('--trace', default=1, type=int,
help='How much trace output you want, defaults to 1. 0 is no trace output.')
parser.add_argument('--hierarchy', nargs='+', default=[],
help='''Mapping of labels to classifier pickle paths to specify a classification hierarchy, such as
"-h neutral:classifiers/movie_reviews.pickle"
''')
args = parser.parse_args()
#####################
## AvgProb combine ##
#####################
# TODO: support MaxVote combining
classifiers = []
for name in args.classifiers:
if args.trace:
print('loading %s' % name)
classifiers.append(nltk.data.load(name))
combined = multi.AvgProbClassifier(classifiers)
##########################
## Hierarchical combine ##
##########################
labels = combined.labels()
label_classifiers = {}
for h in args.hierarchy:
label, path = h.split(':')
if label not in labels:
raise ValueError('%s is not in root labels: %s' % (label, labels))
label_classifiers[label] = nltk.data.load(path)
if args.trace:
print('mapping %s to %s from %s' % (label, label_classifiers[label], path))
if label_classifiers:
if args.trace:
print('combining %d label classifiers for root %s' % (len(label_classifiers), combined))
combined = multi.HierarchicalClassifier(combined, label_classifiers)
##############################
## dump combined classifier ##
##############################
fname = os.path.expanduser(args.filename)
dump_object(combined, fname, trace=args.trace)