def load(self, filename): """Load classifier from FILENAME""" debug.trace_fmtd(4, "tc.load({f})", f=filename) try: (self.keys, self.classifier) = system.load_object(filename) except (TypeError, ValueError): system.print_stderr("Problem loading classifier from {f}: {exc}". format(f=filename, exc=sys.exc_info())) return
def main(args): """Supporting code for command-line processing""" debug.trace_fmtd(6, "main({a})", a=args) if (len(args) != 2): system.print_stderr("Usage: {p} model".format(p=args[0])) return model = args[1] start_web_controller(model) return
def change_directory(dir): """Changes into file system DIR (ignoring errors)""" debug_print("change_directory(%s)" % dir, 4) try: os.chdir(dir) except: print_stderr("Unable to change into directory '%s': %s" % (filename, sys.exc_info())) return
def make_directory(dir): """Creates file system DIR (ignoring errors)""" debug_print("make_directory(%s)" % dir, 4) try: os.mkdir(dir) except: print_stderr("Unable to create directory '%s': %s" % (dir, sys.exc_info())) return
def write_file(filename, text): """Outpus TEXT to FILENAME""" debug_print("write_file(%s, _)" % filename, 5) try: with open(filename, 'w') as f: f.write(text) except: print_stderr("Unable to write file '%s': %s" % (filename, sys.exc_info())) return
def run_command(command_line, level=5): """Runs COMMAND_LINE and returns the output (as a string), with debug tracing at specified LEVEL""" # Issue command debug_print("Running command: %s" % command_line, level=level) (status, output) = getstatusoutput(command_line) if (status != 0): print_stderr("Warning: problem running command (status=%d): %s" % (status, command_line)) # Return output debug_print("Command output: %s\n" % output, level=level + 1) return (output)
def usage(): """Show command-line usage""" # TODO: remove path from script filename script = (__file__ or "n/a") system.print_stderr( "Usage: {scr} training-file model-file [testing]".format(scr=script)) system.print_stderr("") system.print_stderr("Notes:") system.print_stderr( "- Use - to indicate the file is not needed (e.g., existing training model)." ) system.print_stderr( "- You need to supply either training file or model file.") system.print_stderr("- The testing file is optional when training.") return
def main(): """ Main routine: parse arguments and perform main processing TODO: revise comments Note: Used to avoid conflicts with globals (e.g., if this were done at end of script). """ # debug_print("start %s: %s" % (__file__, debug.timestamp()), 3) # Parse command-line, showing usage statement if no arguments given (or --help) args = sys.argv debug_print("argv = %s" % sys.argv, 3) num_args = len(args) if ((num_args == 1) or ((num_args > 1) and (args[1] == "--help"))): print_stderr("Usage: %s [--help] URL" % args[0]) print_stderr( "Example: %s 'http://en.wikipedia.org/wiki/Category:Major_League_Baseball_players'" % args[0]) print_stderr("Notes:") print_stderr( "- MAKE_SUBDIRS recreates category hierarchy in file system") print_stderr("- SKIP_SUBCATS disables subcat traversals") print_stderr("- SKIP_PAGES disables page downloading") sys.exit() arg_pos = 1 while (arg_pos < num_args) and (args[arg_pos][0] == "-"): debug_print("args[%d]: %s" % (arg_pos, args[arg_pos]), 3) if (args[arg_pos] == "-"): # note: - is used to avoid usage statement with file input from stdin pass else: print_stderr("Error: unexpected argument '%s'" % args[arg_pos]) sys.exit() arg_pos += 1 url = args[arg_pos] # Optionally create and change into output directory for wiki pages if OUTPUT_DIR != ".": make_directory(OUTPUT_DIR) change_directory(OUTPUT_DIR) # Process wiki categorization file # TODO: accept file input as well (e.g., '2013 Pittsburgh Pirates season - Wikipedia, the free encyclopedia.html') download_category_articles(url) debug_print("stop %s: %s" % (__file__, debug.timestamp()), 3)