1
2
3
4
5
6
7 from optparse import OptionParser
8 from nltk_lite.contrib.classifier.exceptions import filenotfounderror as fnf, invaliddataerror as inv
9 from nltk_lite.contrib.classifier import format
10
11 D_help = "Used to specify the data format. " \
12 + "Options: C45 for C4.5 format. " \
13 + "Default: C45. "
14
15
16 ALGORITHM = 'algorithm'
17 FILES = 'files'
18 TRAINING = 'training'
19 TEST = 'test'
20 GOLD = 'gold'
21 DATA_FORMAT = 'data_format'
22
23 C45_FORMAT = 'C45'
24
25 DATA_FORMAT_MAPPINGS = {C45_FORMAT: format.C45_FORMAT}
26
28 - def __init__(self, alg_choices, alg_default, a_help, f_help, t_help, T_help, g_help):
29 OptionParser.__init__(self)
30 self.add_option("-a", "--algorithm", dest=ALGORITHM, type="choice", \
31 choices=alg_choices, default=alg_default, help= a_help)
32 self.add_option("-f", "--files", dest=FILES, type="string", help=f_help)
33 self.add_option("-t", "--training-file", dest=TRAINING, type="string", help=t_help)
34 self.add_option("-T", "--test-file", dest=TEST, type="string", help=T_help)
35 self.add_option("-g", "--gold-file", dest=GOLD, type="string", help=g_help)
36
37 self.add_option("-D", "--data-format", dest=DATA_FORMAT, type="choice", choices=DATA_FORMAT_MAPPINGS.keys(), \
38 default=C45_FORMAT, help=D_help)
39
42
44 """
45 method to aid testing
46 """
47 self.parse_args(args, None)
48
59
60 - def run(self, args):
61 """
62 Main method which delegates all the work
63 """
64 self.parse(args)
65 self.execute()
66
68 if self.algorithm is None or self.files is None and (self.training_path is None or (self.test_path is None and self.gold_path is None)):
69 self.required_arguments_not_present_error()
70
72 if self.files is not None and (self.training_path is not None or self.test_path is not None or self.gold_path is not None):
73 self.error("Invalid arguments. The files argument cannot exist with training, test or gold arguments.")
74
75 - def get_instances(self, training_path, test_path, gold_path, ignore_missing = False):
83
85 if path is not None:
86 if ignore_if_missing:
87 try:
88 return method(path)
89 except fnf.FileNotFoundError:
90 return None
91 return method(path)
92 return None
93
95 self.error("Invalid arguments. One or more required arguments are not present.")
96
97 - def write_to_file(self, suffix, training, attributes, klass, test, gold):
104
106 indices = []
107 if str_array is not None:
108 for element in str_array.split(','):
109 try:
110 indices.append(int(element.strip()))
111 except ValueError:
112 raise inv.InvalidDataError('Invalid Data. ' + name + ' should be integers.')
113 return indices
114