Skip to content
Newer
Older
100755 70 lines (51 sloc) 2.02 KB
3c28516 Correctly pull in the environment python
Keoki Seu authored Jan 28, 2013
1 #!/usr/bin/env python
8cb9fbc @japerk initial script to combine classifiers into AvgProbClassifier, make ot…
authored Jun 12, 2011
2 import argparse, os.path
3 import nltk.data
4 from nltk_trainer import dump_object
5 from nltk_trainer.classification import multi
6
7 ########################################
8 ## command options & argument parsing ##
9 ########################################
10
11 parser = argparse.ArgumentParser(description='Combine NLTK Classifiers')
12 parser.add_argument('classifiers', nargs='+',
13 help='one or more pickled classifiers to load and combine')
14 parser.add_argument('filename', default='~/nltk_data/classifiers/combined.pickle',
15 help='Filename to pickle combined classifier, defaults to %(default)s')
16 parser.add_argument('--trace', default=1, type=int,
17 help='How much trace output you want, defaults to 1. 0 is no trace output.')
a6d99ba @japerk default hierarchy to empty list for combining classifiers
authored Oct 4, 2011
18 parser.add_argument('--hierarchy', nargs='+', default=[],
59122bf @japerk can combine classifiers in a 2-level HierarchicalClassifier
authored Jun 15, 2011
19 help='''Mapping of labels to classifier pickle paths to specify a classification hierarchy, such as
20 "-h neutral:classifiers/movie_reviews.pickle"
21 ''')
8cb9fbc @japerk initial script to combine classifiers into AvgProbClassifier, make ot…
authored Jun 12, 2011
22
23 args = parser.parse_args()
24
59122bf @japerk can combine classifiers in a 2-level HierarchicalClassifier
authored Jun 16, 2011
25 #####################
26 ## AvgProb combine ##
27 #####################
8cb9fbc @japerk initial script to combine classifiers into AvgProbClassifier, make ot…
authored Jun 12, 2011
28
59122bf @japerk can combine classifiers in a 2-level HierarchicalClassifier
authored Jun 16, 2011
29 # TODO: support MaxVote combining
8cb9fbc @japerk initial script to combine classifiers into AvgProbClassifier, make ot…
authored Jun 12, 2011
30
31 classifiers = []
32
33 for name in args.classifiers:
34 if args.trace:
35 print 'loading %s' % name
36
37 classifiers.append(nltk.data.load(name))
38
39 combined = multi.AvgProbClassifier(classifiers)
40
59122bf @japerk can combine classifiers in a 2-level HierarchicalClassifier
authored Jun 16, 2011
41 ##########################
42 ## Hierarchical combine ##
43 ##########################
44
45 labels = combined.labels()
46 label_classifiers = {}
47
48 for h in args.hierarchy:
49 label, path = h.split(':')
50
51 if label not in labels:
52 raise ValueError('%s is not in root labels: %s' % (label, labels))
53
54 label_classifiers[label] = nltk.data.load(path)
55
56 if args.trace:
57 print 'mapping %s to %s from %s' % (label, label_classifiers[label], path)
58
59 if label_classifiers:
60 if args.trace:
61 'combining %d label classifiers for root %s' % (len(label_classifiers), combined)
62
63 combined = multi.HierarchicalClassifier(combined, label_classifiers)
64
8cb9fbc @japerk initial script to combine classifiers into AvgProbClassifier, make ot…
authored Jun 12, 2011
65 ##############################
66 ## dump combined classifier ##
67 ##############################
68
69 fname = os.path.expanduser(args.filename)
70 dump_object(combined, fname, trace=args.trace)
Something went wrong with that request. Please try again.