Example usage for org.apache.commons.cli DefaultParser DefaultParser

List of usage examples for org.apache.commons.cli DefaultParser DefaultParser

Introduction

In this page you can find the example usage for org.apache.commons.cli DefaultParser DefaultParser.

Prototype

DefaultParser

Source Link

Usage

From source file:edu.harvard.hul.ois.drs.pdfaconvert.PdfaConvert.java

public static void main(String[] args) throws IOException {
    if (logger == null) {
        System.out.println("About to initialize Log4j");
        logger = LogManager.getLogger();
        System.out.println("Finished initializing Log4j");
    }//from   w  w  w . j ava  2 s. c o m

    logger.debug("Entering main()");

    // WIP: the following command line code was pulled from FITS
    Options options = new Options();
    Option inputFileOption = new Option(PARAM_I, true, "input file");
    options.addOption(inputFileOption);
    options.addOption(PARAM_V, false, "print version information");
    options.addOption(PARAM_H, false, "help information");
    options.addOption(PARAM_O, true, "output sub-directory");

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args, true);
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }

    // print version info
    if (cmd.hasOption(PARAM_V)) {
        if (StringUtils.isEmpty(applicationVersion)) {
            applicationVersion = "<not set>";
            System.exit(1);
        }
        System.out.println("Version: " + applicationVersion);
        System.exit(0);
    }

    // print help info
    if (cmd.hasOption(PARAM_H)) {
        displayHelp();
        System.exit(0);
    }

    // input parameter
    if (cmd.hasOption(PARAM_I)) {
        String input = cmd.getOptionValue(PARAM_I);
        boolean hasValue = cmd.hasOption(PARAM_I);
        logger.debug("Has option {} value: [{}]", PARAM_I, hasValue);
        String paramVal = cmd.getOptionValue(PARAM_I);
        logger.debug("value of option: [{}] ****", paramVal);

        File inputFile = new File(input);
        if (!inputFile.exists()) {
            logger.warn("{} does not exist or is not readable.", input);
            System.exit(1);
        }

        String subDir = cmd.getOptionValue(PARAM_O);
        PdfaConvert convert;
        if (!StringUtils.isEmpty(subDir)) {
            convert = new PdfaConvert(subDir);
        } else {
            convert = new PdfaConvert();
        }
        if (inputFile.isDirectory()) {
            if (inputFile.listFiles() == null || inputFile.listFiles().length < 1) {
                logger.warn("Input directory is empty, nothing to process.");
                System.exit(1);
            } else {
                logger.debug("Have directory: [{}] with file count: {}", inputFile.getAbsolutePath(),
                        inputFile.listFiles().length);
                DirectoryStream<Path> dirStream = null;
                dirStream = Files.newDirectoryStream(inputFile.toPath());
                for (Path filePath : dirStream) {
                    logger.debug("Have file name: {}", filePath.toString());
                    // Note: only handling files, not recursively going into sub-directories
                    if (filePath.toFile().isFile()) {
                        // Catch possible exception for each file so can handle other files in directory.
                        try {
                            convert.examine(filePath.toFile());
                        } catch (Exception e) {
                            logger.error("Problem processing file: {} -- Error message: {}",
                                    filePath.getFileName(), e.getMessage());
                        }
                    } else {
                        logger.warn("Not a file so not processing: {}", filePath.toString()); // could be a directory but not recursing
                    }
                }
                dirStream.close();
            }
        } else {
            logger.debug("About to process file: {}", inputFile.getPath());
            try {
                convert.examine(inputFile);
            } catch (Exception e) {
                logger.error("Problem processing file: {} -- Error message: {}", inputFile.getName(),
                        e.getMessage());
                logger.debug("Problem processing file: {} -- Error message: {}", inputFile.getName(),
                        e.getMessage(), e);
            }
        }
    } else {
        System.err.println("Missing required option: " + PARAM_I);
        displayHelp();
        System.exit(-1);
    }

    System.exit(0);
}

From source file:general.Main.java

/**
 * Selects the files to be processed and specifies the files to write to.
 *
 * @param args Arguments to specify runtime behavior.
 *///from w w w.ja  v a2  s  .c o  m
public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException,
        InstantiationException, IllegalAccessException {
    Options options = new Options();
    options.addOption("l", "logging", false, "enables file logging");
    options.addOption("j", "jena", false, "uses the Jena SPARQL Parser");
    options.addOption("o", "openrdf", false, "uses the OpenRDF SPARQL Parser");
    options.addOption("f", "file", true, "defines the input file prefix");
    options.addOption("h", "help", false, "displays this help");
    options.addOption("t", "tsv", false, "reads from .tsv-files");
    // options.addOption("p", "parquet", false, "read from .parquet-files");
    options.addOption("n", "numberOfThreads", true, "number of used threads, default 1");
    options.addOption("b", "withBots", false, "enables metric calculation for bot queries+");
    options.addOption("p", "readPreprocessed", false, "enables reading of preprocessed files");

    //some parameters which can be changed through parameters
    //QueryHandler queryHandler = new OpenRDFQueryHandler();
    String inputFilePrefix;
    String inputFileSuffix = ".tsv";
    String queryParserName = "OpenRDF";
    Class inputHandlerClass = null;
    Class queryHandlerClass = null;
    int numberOfThreads = 1;

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(options, args);
        if (cmd.hasOption("help")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("help", options);
            return;
        }
        if (cmd.hasOption("openrdf")) {
            queryHandlerClass = OpenRDFQueryHandler.class;
        }
        if (cmd.hasOption("tsv")) {
            inputFileSuffix = ".tsv";
            inputHandlerClass = InputHandlerTSV.class;
        }
        if (cmd.hasOption("parquet")) {
            inputFileSuffix = ".parquet";
            Logger.getLogger("org").setLevel(Level.WARN);
            Logger.getLogger("akka").setLevel(Level.WARN);
            SparkConf conf = new SparkConf().setAppName("SPARQLQueryAnalyzer").setMaster("local");
            JavaSparkContext sc = new JavaSparkContext(conf);
            inputHandlerClass = InputHandlerParquet.class;
        }
        if (inputHandlerClass == null) {
            System.out.println("Please specify which parser to use, either -t for TSV or -p for parquet.");
        }
        if (cmd.hasOption("file")) {
            inputFilePrefix = cmd.getOptionValue("file").trim();
        } else {
            System.out.println(
                    "Please specify at least the file which we should work on using the option '--file PREFIX' or 'f PREFIX'");
            return;
        }
        if (cmd.hasOption("logging")) {
            LoggingHandler.initFileLog(queryParserName, inputFilePrefix);
        }
        if (cmd.hasOption("numberOfThreads")) {
            numberOfThreads = Integer.parseInt(cmd.getOptionValue("numberOfThreads"));
        }
        if (cmd.hasOption("withBots")) {
            withBots = true;
        }
        if (cmd.hasOption("readPreprocessed")) {
            readPreprocessed = true;
        }
    } catch (UnrecognizedOptionException e) {
        System.out.println("Unrecognized commandline option: " + e.getOption());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("help", options);
        return;
    } catch (ParseException e) {
        System.out.println(
                "There was an error while parsing your command line input. Did you rechecked your syntax before running?");
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("help", options);
        return;
    }

    LoggingHandler.initConsoleLog();

    loadPreBuildQueryTypes();

    long startTime = System.nanoTime();

    ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);

    for (int day = 1; day <= 31; day++) {
        String inputFile = inputFilePrefix + String.format("%02d", day) + inputFileSuffix;
        Runnable parseOneMonthWorker = new ParseOneMonthWorker(inputFile, inputFilePrefix, inputHandlerClass,
                queryParserName, queryHandlerClass, day);
        executor.execute(parseOneMonthWorker);
    }
    executor.shutdown();

    while (!executor.isTerminated()) {
        //wait until all workers are finished
    }

    writeQueryTypes(inputFilePrefix);

    long stopTime = System.nanoTime();
    long millis = TimeUnit.MILLISECONDS.convert(stopTime - startTime, TimeUnit.NANOSECONDS);
    Date date = new Date(millis);
    System.out.println("Finished executing with all threads: "
            + new SimpleDateFormat("mm-dd HH:mm:ss:SSSSSSS").format(date));
}

From source file:com.act.biointerpretation.sars.SeqDBReactionGrouper.java

public static void main(String[] args) throws Exception {
    // Build command line parser.
    Options opts = new Options();
    for (Option.Builder b : OPTION_BUILDERS) {
        opts.addOption(b.build());/*from  w w w .  j  a  va  2s  .  c o m*/
    }

    CommandLine cl = null;
    try {
        CommandLineParser parser = new DefaultParser();
        cl = parser.parse(opts, args);
    } catch (ParseException e) {
        LOGGER.error("Argument parsing failed: %s", e.getMessage());
        HELP_FORMATTER.printHelp(SeqDBReactionGrouper.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
        System.exit(1);
    }

    // Print help.
    if (cl.hasOption(OPTION_HELP)) {
        HELP_FORMATTER.printHelp(SeqDBReactionGrouper.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
        return;
    }

    // Handle arguments
    String mongoDBName = cl.getOptionValue(OPTION_DB);
    MongoDB mongoDB = new MongoDB(LOCAL_HOST, MONGO_PORT, mongoDBName);

    File outputFile = new File(cl.getOptionValue(OPTION_OUTPUT_PATH));
    if (outputFile.isDirectory() || outputFile.exists()) {
        LOGGER.error("Supplied output file is a directory or already exists.");
        System.exit(1);
    }
    outputFile.createNewFile();

    Integer limit = DEFAULT_LIMIT_INFINITY;
    if (cl.hasOption(OPTION_LIMIT)) {
        limit = Integer.parseInt(cl.getOptionValue(OPTION_LIMIT));
    }
    LOGGER.info("Only processing first %d entries in Seq DB.", limit);

    SeqDBReactionGrouper enzymeGrouper = new SeqDBReactionGrouper(mongoDB.getSeqIterator(), mongoDBName, limit);

    LOGGER.info("Scanning seq db for reactions with same seq.");
    ReactionGroupCorpus groupCorpus = enzymeGrouper.getReactionGroupCorpus();

    LOGGER.info("Writing output to file.");
    groupCorpus.printToJsonFile(outputFile);

    LOGGER.info("Complete!");
}

From source file:de.morbz.osmpoispbf.Scanner.java

public static void main(String[] args) {
    System.out.println("OsmPoisPbf " + VERSION + " started");

    // Get input file
    if (args.length < 1) {
        System.out.println("Error: Please provide an input file");
        System.exit(-1);/*from   ww w. j a va 2s  . c o  m*/
    }
    String inputFile = args[args.length - 1];

    // Get output file
    String outputFile;
    int index = inputFile.indexOf('.');
    if (index != -1) {
        outputFile = inputFile.substring(0, index);
    } else {
        outputFile = inputFile;
    }
    outputFile += ".csv";

    // Setup CLI parameters
    options = new Options();
    options.addOption("ff", "filterFile", true, "The file that is used to filter categories");
    options.addOption("of", "outputFile", true, "The output CSV file to be written");
    options.addOption("rt", "requiredTags", true, "Comma separated list of tags that are required [name]");
    options.addOption("ut", "undesiredTags", true,
            "Comma separated list of tags=value combinations that should be filtered [key=value]");
    options.addOption("ot", "outputTags", true, "Comma separated list of tags that are exported [name]");
    options.addOption("ph", "printHeader", false,
            "If flag is set, the `outputTags` are printed as first line in the output file.");
    options.addOption("r", "relations", false, "Parse relations");
    options.addOption("nw", "noWays", false, "Don't parse ways");
    options.addOption("nn", "noNodes", false, "Don't parse nodes");
    options.addOption("u", "allowUnclosedWays", false, "Allow ways that aren't closed");
    options.addOption("d", "decimals", true, "Number of decimal places of coordinates [7]");
    options.addOption("s", "separator", true, "Separator character for CSV [|]");
    options.addOption("v", "verbose", false, "Print all found POIs");
    options.addOption("h", "help", false, "Print this help");

    // Parse parameters
    CommandLine line = null;
    try {
        line = (new DefaultParser()).parse(options, args);
    } catch (ParseException exp) {
        System.err.println(exp.getMessage());
        printHelp();
        System.exit(-1);
    }

    // Help
    if (line.hasOption("help")) {
        printHelp();
        System.exit(0);
    }

    // Get filter file
    String filterFile = null;
    if (line.hasOption("filterFile")) {
        filterFile = line.getOptionValue("filterFile");
    }

    // Get output file
    if (line.hasOption("outputFile")) {
        outputFile = line.getOptionValue("outputFile");
    }

    // Check files
    if (inputFile.equals(outputFile)) {
        System.out.println("Error: Input and output files are the same");
        System.exit(-1);
    }
    File file = new File(inputFile);
    if (!file.exists()) {
        System.out.println("Error: Input file doesn't exist");
        System.exit(-1);
    }

    // Check OSM entity types
    boolean parseNodes = true;
    boolean parseWays = true;
    boolean parseRelations = false;
    if (line.hasOption("noNodes")) {
        parseNodes = false;
    }
    if (line.hasOption("noWays")) {
        parseWays = false;
    }
    if (line.hasOption("relations")) {
        parseRelations = true;
    }

    // Unclosed ways allowed?
    if (line.hasOption("allowUnclosedWays")) {
        onlyClosedWays = false;
    }

    // Get CSV separator
    char separator = '|';
    if (line.hasOption("separator")) {
        String arg = line.getOptionValue("separator");
        if (arg.length() != 1) {
            System.out.println("Error: The CSV separator has to be exactly 1 character");
            System.exit(-1);
        }
        separator = arg.charAt(0);
    }
    Poi.setSeparator(separator);

    // Set decimals
    int decimals = 7; // OSM default
    if (line.hasOption("decimals")) {
        String arg = line.getOptionValue("decimals");
        try {
            int dec = Integer.valueOf(arg);
            if (dec < 0) {
                System.out.println("Error: Decimals must not be less than 0");
                System.exit(-1);
            } else {
                decimals = dec;
            }
        } catch (NumberFormatException ex) {
            System.out.println("Error: Decimals have to be a number");
            System.exit(-1);
        }
    }
    Poi.setDecimals(decimals);

    // Verbose mode?
    if (line.hasOption("verbose")) {
        printPois = true;
    }

    // Required tags
    if (line.hasOption("requiredTags")) {
        String arg = line.getOptionValue("requiredTags");
        requiredTags = arg.split(",");
    }

    // Undesired tags
    if (line.hasOption("undesiredTags")) {
        String arg = line.getOptionValue("undesiredTags");
        undesiredTags = new HashMap<>();
        for (String undesired : arg.split(",")) {
            String[] keyVal = undesired.split("=");
            if (keyVal.length != 2) {
                System.out.println("Error: Undesired Tags have to formated like tag=value");
                System.exit(-1);
            }
            if (!undesiredTags.containsKey(keyVal[0])) {
                undesiredTags.put(keyVal[0], new HashSet<>(1));
            }
            undesiredTags.get(keyVal[0]).add(keyVal[1]);
        }
    }

    // Output tags
    if (line.hasOption("outputTags")) {
        String arg = line.getOptionValue("outputTags");
        outputTags = arg.split(",");
    }

    // Get filter rules
    FilterFileParser parser = new FilterFileParser(filterFile);
    filters = parser.parse();
    if (filters == null) {
        System.exit(-1);
    }

    // Setup CSV output
    try {
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF8"));
    } catch (IOException e) {
        System.out.println("Error: Output file error");
        System.exit(-1);
    }

    // Print Header
    if (line.hasOption("printHeader")) {
        String header = "category" + separator + "osm_id" + separator + "lat" + separator + "lon";
        for (int i = 0; i < outputTags.length; i++) {
            header += separator + outputTags[i];
        }
        try {
            writer.write(header + "\n");
        } catch (IOException e) {
            System.out.println("Error: Output file write error");
            System.exit(-1);
        }
    }

    // Setup OSMonaut
    EntityFilter filter = new EntityFilter(parseNodes, parseWays, parseRelations);
    Osmonaut naut = new Osmonaut(inputFile, filter, false);

    // Start watch
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();

    // Start OSMonaut
    String finalSeparator = String.valueOf(separator);
    naut.scan(new IOsmonautReceiver() {

        boolean entityNeeded;

        @Override
        public boolean needsEntity(EntityType type, Tags tags) {
            // Are there any tags?
            if (tags.size() == 0) {
                return false;
            }

            // Check required tags
            for (String tag : requiredTags) {
                if (!tags.hasKey(tag)) {
                    return false;
                }
            }

            entityNeeded = getCategory(tags, filters) != null;

            if (undesiredTags != null && entityNeeded) {
                for (String key : undesiredTags.keySet()) {
                    if (tags.hasKey(key)) {
                        for (String val : undesiredTags.get(key)) {
                            if (tags.hasKeyValue(key, val)) {
                                return false;
                            }
                        }
                    }
                }
            }

            return entityNeeded;
        }

        @Override
        public void foundEntity(Entity entity) {
            // Check if way is closed
            if (onlyClosedWays && entity.getEntityType() == EntityType.WAY) {
                if (!((Way) entity).isClosed()) {
                    return;
                }
            }

            // Get category
            Tags tags = entity.getTags();
            String cat = getCategory(tags, filters);
            if (cat == null) {
                return;
            }

            // Get center
            LatLon center = entity.getCenter();
            if (center == null) {
                return;
            }

            // Make OSM-ID
            String type = "";
            switch (entity.getEntityType()) {
            case NODE:
                type = "node";
                break;
            case WAY:
                type = "way";
                break;
            case RELATION:
                type = "relation";
                break;
            }
            String id = String.valueOf(entity.getId());

            // Make output tags
            String[] values = new String[outputTags.length];
            for (int i = 0; i < outputTags.length; i++) {
                String key = outputTags[i];
                if (tags.hasKey(key)) {
                    values[i] = tags.get(key).replaceAll(finalSeparator, "").replaceAll("\"", "");
                }
            }

            // Make POI
            poisFound++;
            Poi poi = new Poi(values, cat, center, type, id);

            // Output
            if (printPois && System.currentTimeMillis() > lastMillis + 40) {
                printPoisFound();
                lastMillis = System.currentTimeMillis();
            }

            // Write to file
            try {
                writer.write(poi.toCsv() + "\n");
            } catch (IOException e) {
                System.out.println("Error: Output file write error");
                System.exit(-1);
            }
        }
    });

    // Close writer
    try {
        writer.close();
    } catch (IOException e) {
        System.out.println("Error: Output file close error");
        System.exit(-1);
    }

    // Output results
    stopWatch.stop();

    printPoisFound();
    System.out.println();
    System.out.println("Elapsed time in milliseconds: " + stopWatch.getElapsedTime());

    // Quit
    System.exit(0);
}

From source file:Executable.LinkImputeR.java

/**
 * Main function//from ww w . j a va  2  s. co m
 * @param args Command line arguments
 * @throws Exception If an uncaught error occurs
 */
public static void main(String[] args) throws Exception {
    try {
        Options options = new Options();

        OptionGroup all = new OptionGroup();
        all.addOption(Option.builder("c").build());
        all.addOption(Option.builder("s").build());
        all.addOption(Option.builder("v").build());
        all.addOption(Option.builder("h").build());
        options.addOptionGroup(all);

        CommandLineParser parser = new DefaultParser();
        CommandLine commands = parser.parse(options, args);

        String[] fileNames = commands.getArgs();

        XMLConfiguration c;
        boolean done = false;

        if (commands.hasOption("c")) {
            if (fileNames.length == 2) {
                c = convert(new File(fileNames[0]));
                writeXML(c, new File(fileNames[1]));
            } else {
                System.out.println("An input and output file must be provided");
                System.out.println();
                help();
            }
            done = true;
        }

        if (commands.hasOption("s")) {
            if (fileNames.length == 1) {
                c = convert(new File(fileNames[0]));
                accuracy(c);
            } else {
                System.out.println("An input file must be provided");
                System.out.println();
                help();
            }
            done = true;
        }

        if (commands.hasOption("v")) {
            System.out.println("LinkImputeR version 1.1.3");
            done = true;
        }

        if (commands.hasOption("h")) {
            help();
            done = true;
        }

        if (!done) {
            if (fileNames.length == 3) {
                File xml = new File(fileNames[0]);

                FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(
                        XMLConfiguration.class).configure(new Parameters().xml().setFile(xml));

                XMLConfiguration config = builder.getConfiguration();

                switch (config.getString("mode")) {
                case "accuracy":
                    accuracy(config);
                    break;
                case "impute":
                    if (args.length == 3) {
                        impute(config, args[1], new File(args[2]));
                    } else {
                        impute(config, null, null);
                    }
                    break;
                }
            } else {
                System.out.println("An input file, case name and output file must be provided (in that order)");
                System.out.println();
                help();
            }
        }
    } catch (UnrecognizedOptionException ex) {
        System.err.println("Unrecognised command line option (" + ex.getOption() + ")");
        System.err.println();
        help();
    } catch (AlreadySelectedException ex) {
        System.err.println("Only one option can be selected at a time");
        System.err.println();
        help();
    } catch (VCFException ex) {
        System.err.println("=====");
        System.err.println("ERROR");
        System.err.println("=====");
        System.err.println("There's a problem with the VCF file");
        System.err.println(ex.getMessage());
        System.err.println();
        System.err.println("Technical details follow:");
        throw ex;
    } catch (INIException ex) {
        System.err.println("=====");
        System.err.println("ERROR");
        System.err.println("=====");
        System.err.println("There's a problem with the ini file");
        System.err.println(ex.getMessage());
        System.err.println();
        System.err.println("Technical details follow:");
        throw ex;
    } catch (OutputException ex) {
        System.err.println("=====");
        System.err.println("ERROR");
        System.err.println("=====");
        System.err.println("There's a problem writing an output file");
        System.err.println(ex.getMessage());
        System.err.println();
        System.err.println("Technical details follow:");
        throw ex;
    } catch (AlgorithmException ex) {
        System.err.println("=====");
        System.err.println("ERROR");
        System.err.println("=====");
        System.err.println("There's a problem with the algorithms");
        System.err.println(ex.getMessage());
        System.err.println();
        System.err.println("Technical details follow:");
        throw ex;
    } catch (ProgrammerException ex) {
        System.err.println("=====");
        System.err.println("ERROR");
        System.err.println("=====");
        System.err.println("Well this is embarrassing.  This shouldn't have happened.  "
                + "Please contact the maintainer if you can not solve the error"
                + "from the technical details.");
        System.err.println();
        System.err.println("Technical details follow:");
        throw ex;
    } catch (Exception ex) {
        System.err.println("=====");
        System.err.println("ERROR");
        System.err.println("=====");
        System.err.println("Well this is embarrassing.  This was not expected to have happened.  "
                + "Please contact the maintainer if you can not solve the error"
                + "from the technical details.");
        System.err.println();
        System.err.println("Note: The maintainer would be interested in knowing "
                + "about any XML related messages so he can write nicer error "
                + "messages for these problems.");
        System.err.println();
        System.err.println("Technical details follow:");
        throw ex;
    }
}

From source file:edu.uiowa.javatm.JavaTM.java

/**
 * @param args First one indicates which topic model to use
 *///from   w  w w.  jav a2s.c o  m
public static void main(String[] args) {
    TMGibbsSampler tmGibbsSampler = null;

    Option modelType = Option.builder("model").longOpt("model-type").desc("Type of topic models to use")
            .hasArg().required().build();

    Option dataName = Option.builder("name").longOpt("data-name").desc("Data name: used for saving outputs")
            .hasArg().required().build();

    Option alpha = Option.builder("a").longOpt("alpha")
            .desc("Dirichlet prior for document (author) over topic multinomial").hasArg().required().build();

    Option beta = Option.builder("b").longOpt("beta").desc("Dirichlet prior for topic over word multinomial")
            .hasArg().required().build();

    Option pi = Option.builder("p").longOpt("pi").desc("Dirichlet prior for topic over time multinomial")
            .hasArg().build();

    Option K = Option.builder("K").longOpt("K").desc("The number of timestamp indices").hasArg().build();

    /*Option tau = Option.builder("tau").longOpt("tau")
    .desc("Smoothing constant for topic time")
    .hasArg().build();*/

    Option doc = Option.builder("doc").longOpt("document-file").desc("WD matrix to use").hasArg().required()
            .build();

    Option voc = Option.builder("voc").longOpt("vocabulary-file")
            .desc("Vocabulary file of the corpus of interest").hasArg().required().build();

    Option auth = Option.builder("auth").longOpt("auth-file").desc("Author indices for each token").hasArg()
            .build();

    Option authArray = Option.builder("authArray").longOpt("author-list-file").desc("Author list").hasArg()
            .build();

    Option dkArray = Option.builder("dk").longOpt("document-time-file").desc("Document timestamp file").hasArg()
            .build();

    Option citationMat = Option.builder("cm").longOpt("citation-matrix")
            .desc("Citation overtime for the corpus").hasArg().build();

    Option numTopics = Option.builder("topic").longOpt("num-topics").desc("The total number of topics").hasArg()
            .required().build();

    Option numIters = Option.builder("iter").longOpt("num-iters").desc("The total number of iterations")
            .hasArg().required().build();

    Option outputDir = Option.builder("odir").longOpt("output-dir").desc("Output directory").hasArg().required()
            .build();

    Options options = new Options();
    options.addOption(modelType).addOption(alpha).addOption(beta).addOption(numTopics).addOption(K)
            .addOption(pi).addOption(citationMat).addOption(numIters).addOption(doc).addOption(voc)
            .addOption(dkArray).addOption(outputDir).addOption(auth).addOption(authArray).addOption(dataName);

    CommandLineParser parser = new DefaultParser();
    try {
        // parse the command line arguments
        CommandLine line = parser.parse(options, args);
        String model = line.getOptionValue("model");
        String name = line.getOptionValue("name");
        String docFile = line.getOptionValue("doc");
        String vocFile = line.getOptionValue("voc");
        int topics = Integer.parseInt(line.getOptionValue("topic"));
        int iters = Integer.parseInt(line.getOptionValue("iter"));
        double a = Double.parseDouble(line.getOptionValue("a"));
        double b = Double.parseDouble(line.getOptionValue("b"));

        String modelLower = model.toLowerCase();
        if (modelLower.equals("lda")) {
            tmGibbsSampler = new LDAGibbsSampler(topics, iters, a, b, docFile, vocFile);
        } else if (modelLower.equals("at")) {
            String authFile = line.getOptionValue("auth");
            String authArrayFile = line.getOptionValue("authArray");
            //double tau_val = Double.parseDouble(line.getOptionValue("tau"));
            tmGibbsSampler = new ATGibbsSampler(topics, iters, a, b, docFile, vocFile, authFile, authArrayFile);
        } else if (modelLower.equals("tot")) {
            String dkFile = line.getOptionValue("dk");
            //double tau_val = Double.parseDouble(line.getOptionValue("tau"));
            tmGibbsSampler = new ToTGibbsSampler(topics, iters, a, b, docFile, vocFile, dkFile);
        } else if (modelLower.equals("tiot")) {
            String timeFile = line.getOptionValue("dk");
            String citationFile = line.getOptionValue("cm");
            double p = Double.parseDouble(line.getOptionValue("p"));
            //int k = Integer.parseInt(line.getOptionValue("K"));
            tmGibbsSampler = new TIOTGibbsSampler(topics, iters, a, b, p, docFile, vocFile, timeFile,
                    citationFile);
        } else {
            System.err.println("Invalid model type selection. Must be lda, at, tot or atot.");
            System.exit(ExitStatus.ILLEGAL_ARGUMENT);
        }

        long startTime = System.nanoTime();
        tmGibbsSampler.fit();
        TMOutcome outcome = tmGibbsSampler.get_outcome();
        long endTime = System.nanoTime();
        long duration = (endTime - startTime);
        System.out.println("Overall elapsed time: " + duration / 1000000000. + " seconds");

        tmGibbsSampler.showTopics(10);
        outcome.showTopicDistribution();

        String oDir = line.getOptionValue("odir");
        if (!oDir.endsWith("/")) {
            oDir = oDir + "/";
        }
        // append name to `oDir`
        oDir = oDir + name + "-";

        if (modelLower.contains("tot")) {
            // topic over time (tot and atot) has beta distribution parameters to write
            Utils.write2DArray(((ToTOutcome) outcome).getPsi(), oDir + "psi-" + modelLower + ".csv");
        }

        if (modelLower.contains("tiot")) {
            // topic over time (tot and atot) has beta distribution parameters to write
            Utils.write2DArray(((TIOTOutcome) outcome).getPsi(), oDir + "psi-" + modelLower + ".csv");
            double[][][] ga = ((TIOTOutcome) outcome).getGa();
            for (int t = 0; t < ga.length; t++) {
                Utils.write2DArray(ga[t], oDir + "gamma-" + t + "-" + modelLower + ".csv");
            }
        }

        Utils.write2DArray(outcome.getPhi(), oDir + "phi-" + modelLower + ".csv");
        Utils.write2DArray(outcome.getTheta(), oDir + "theta-" + modelLower + ".csv");

        System.out.println("Output files saved to " + oDir);
    } catch (ParseException exp) {
        // oops, something went wrong
        System.err.println("Parsing failed. Reason: " + exp.getMessage());
    }

}

From source file:com.act.analysis.similarity.ROBinning.java

public static void main(String[] args) throws Exception {
    Options opts = new Options();
    for (Option.Builder b : OPTION_BUILDERS) {
        opts.addOption(b.build());/*from  w w w . jav a2s .  c o  m*/
    }

    CommandLine cl = null;
    try {
        CommandLineParser parser = new DefaultParser();
        cl = parser.parse(opts, args);
    } catch (ParseException e) {
        System.err.format("Argument parsing failed: %s\n", e.getMessage());
        HELP_FORMATTER.printHelp(ROBinning.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
        System.exit(1);
    }

    if (cl.hasOption("help")) {
        HELP_FORMATTER.printHelp(ROBinning.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
        return;
    }

    String dbName = cl.getOptionValue(OPTION_DB);

    // We read and write to the same database
    NoSQLAPI api = new NoSQLAPI(dbName, dbName);
    ErosCorpus erosCorpus = new ErosCorpus();
    erosCorpus.loadValidationCorpus();

    ROBinning roBinning = new ROBinning(erosCorpus, api);
    roBinning.init();
    roBinning.processChemicals();
}

From source file:com.twentyn.patentSearch.DocumentIndexer.java

public static void main(String[] args) throws Exception {
    System.out.println("Starting up...");
    System.out.flush();/*w  w w. j  av  a2  s  .  c  o m*/
    Options opts = new Options();
    opts.addOption(Option.builder("i").longOpt("input").hasArg().required()
            .desc("Input file or directory to index").build());
    opts.addOption(Option.builder("x").longOpt("index").hasArg().required()
            .desc("Path to index file to generate").build());
    opts.addOption(Option.builder("h").longOpt("help").desc("Print this help message and exit").build());
    opts.addOption(Option.builder("v").longOpt("verbose").desc("Print verbose log output").build());

    HelpFormatter helpFormatter = new HelpFormatter();
    CommandLineParser cmdLineParser = new DefaultParser();
    CommandLine cmdLine = null;
    try {
        cmdLine = cmdLineParser.parse(opts, args);
    } catch (ParseException e) {
        System.out.println("Caught exception when parsing command line: " + e.getMessage());
        helpFormatter.printHelp("DocumentIndexer", opts);
        System.exit(1);
    }

    if (cmdLine.hasOption("help")) {
        helpFormatter.printHelp("DocumentIndexer", opts);
        System.exit(0);
    }

    if (cmdLine.hasOption("verbose")) {
        // With help from http://stackoverflow.com/questions/23434252/programmatically-change-log-level-in-log4j2
        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        Configuration ctxConfig = ctx.getConfiguration();
        LoggerConfig logConfig = ctxConfig.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
        logConfig.setLevel(Level.DEBUG);

        ctx.updateLoggers();
        LOGGER.debug("Verbose logging enabled");
    }

    LOGGER.info("Opening index at " + cmdLine.getOptionValue("index"));
    Directory indexDir = FSDirectory.open(new File(cmdLine.getOptionValue("index")).toPath());

    /* The standard analyzer is too aggressive with chemical entities (it strips structural annotations, for one
     * thing), and the whitespace analyzer doesn't do any case normalization or stop word elimination.  This custom
     * analyzer appears to treat chemical entities better than the standard analyzer without admitting too much
     * cruft to the index. */
    Analyzer analyzer = CustomAnalyzer.builder().withTokenizer("whitespace").addTokenFilter("lowercase")
            .addTokenFilter("stop").build();

    IndexWriterConfig writerConfig = new IndexWriterConfig(analyzer);
    writerConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
    writerConfig.setRAMBufferSizeMB(1 << 10);
    IndexWriter indexWriter = new IndexWriter(indexDir, writerConfig);

    String inputFileOrDir = cmdLine.getOptionValue("input");
    File splitFileOrDir = new File(inputFileOrDir);
    if (!(splitFileOrDir.exists())) {
        LOGGER.error("Unable to find directory at " + inputFileOrDir);
        System.exit(1);
    }

    DocumentIndexer indexer = new DocumentIndexer(indexWriter);
    PatentCorpusReader corpusReader = new PatentCorpusReader(indexer, splitFileOrDir);
    corpusReader.readPatentCorpus();
    indexer.commitAndClose();
}

From source file:com.mfalaize.zipdiff.Main.java

/**
 * The command line interface to zipdiff utility
 *
 * @param args The command line parameters
 *//*from  w  w  w  . jav  a 2  s . com*/
public static void main(String[] args) {
    CommandLineParser parser = new DefaultParser();

    try {
        CommandLine line = parser.parse(options, args);

        String filename1;
        String filename2;

        filename1 = line.getOptionValue(OPTION_FILE1);
        filename2 = line.getOptionValue(OPTION_FILE2);

        File f1 = new File(filename1);
        File f2 = new File(filename2);

        checkFile(f1);
        checkFile(f2);

        System.out.println("File 1 = " + f1);
        System.out.println("File 2 = " + f2);

        DifferenceCalculator calc = new DifferenceCalculator(f1, f2);

        String regularExpression;

        // todo - calc.setFilenamesToIgnore();

        if (line.hasOption(OPTION_COMPARE_CRC_VALUES)) {
            calc.setCompareCRCValues(true);
        } else {
            calc.setCompareCRCValues(false);
        }

        if (line.hasOption(OPTION_IGNORE_CVS_FILES)) {
            calc.setIgnoreCVSFiles(true);
        } else {
            calc.setIgnoreCVSFiles(false);
        }

        if (line.hasOption(OPTION_COMPARE_TIMESTAMPS)) {
            calc.setIgnoreTimestamps(false);
        } else {
            calc.setIgnoreTimestamps(true);
        }

        if (line.hasOption(OPTION_REGEX)) {
            regularExpression = line.getOptionValue(OPTION_REGEX);
            Set<String> regexSet = new HashSet<String>();
            regexSet.add(regularExpression);

            calc.setFilenameRegexToIgnore(regexSet);
        }

        boolean exitWithErrorOnDiff = false;
        if (line.hasOption(OPTION_EXIT_WITH_ERROR_ON_DIFF)) {
            exitWithErrorOnDiff = true;
        }

        Differences d = calc.getDifferences();

        if (line.hasOption(OPTION_OUTPUT_FILE)) {
            String outputFilename = line.getOptionValue(OPTION_OUTPUT_FILE);
            writeOutputFile(outputFilename, d);
        }

        if (d.hasDifferences()) {
            if (line.hasOption(OPTION_VERBOSE)) {
                System.out.println(d);
                System.out.println(d.getFilename1() + " and " + d.getFilename2() + " are different.");
            }
            if (exitWithErrorOnDiff) {
                System.exit(EXITCODE_DIFF);
            }
        } else {
            System.out.println("No differences found.");
        }
    } catch (ParseException pex) {
        System.err.println(pex.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("Main [options] ", options);
        System.exit(EXITCODE_ERROR);
    } catch (Exception ex) {
        ex.printStackTrace();
        System.exit(EXITCODE_ERROR);
    }

}

From source file:com.smartmarmot.dbforbix.DBforBix.java

public static void main(final String[] args) {

    Config config = Config.getInstance();
    String action = "";

    options = new Options();
    options.addOption("v", false, "display version and exit");
    options.addOption("t", false, "test configuration");
    options.addOption("h", false, "display help");

    options.addOption("b", true, "base directory");
    options.addOption("c", true, "config file location");

    //      options.addOption("d", false, "enable debugging");
    options.addOption("C", false, "enable console output");

    options.addOption("a", true, "action (start/stop/status)");

    // handle command line parameters
    try {//from ww w .j  a va2 s . c om
        CommandLineParser cmdparser = new DefaultParser();
        CommandLine cmd = cmdparser.parse(options, args);

        if (cmd.hasOption("v")) {
            System.out.println(Constants.BANNER);
            System.exit(0);
        }

        if (cmd.hasOption("t")) {
            System.out.println("not implemented");
            System.exit(0);
        }

        if (args.length == 0 || cmd.hasOption("h")) {
            displayUsage();
            System.exit(0);
        }

        //         if (cmd.hasOption("d")) {
        //            debug = true;
        //            Logger.getRootLogger().setLevel(Level.ALL);
        //         }

        if (cmd.hasOption("C"))
            Logger.getRootLogger().addAppender(new ConsoleAppender(new SimpleLayout()));

        action = cmd.getOptionValue("a", "help");

        /**
         * set config file path
         */
        config.setBasedir(cmd.getOptionValue("b", "."));
        config.setConfigFile(cmd.getOptionValue("c",
                config.getBasedir() + File.separator + "conf" + File.separator + "config.properties"));
    } catch (ParseException e) {
        System.err.println("Error in parameters: " + e.getLocalizedMessage());
        System.exit(-1);
    }

    //autoupdate cycle
    while (true) {
        try {
            switch (action.toLowerCase()) {
            case "start": {
                reinit();

                LOG.info("Starting " + Constants.BANNER);
                // writePid(_pid, _pidfile);

                _zabbixSender = new ZabbixSender(ZabbixSender.PROTOCOL.V32);
                _zabbixSender.updateServerList(config.getZabbixServers().toArray(new ZabbixServer[0]));
                _zabbixSender.start();

                //persSender = new PersistentDBSender(PersistentDBSender.PROTOCOL.V18);
                //persSender.updateServerList(config.getZabbixServers().toArray(new ZServer[0]));
                //persSender.start();

                config.startChecks();
                action = "update";
            }
                break;
            case "update": {
                LOG.info("Sleeping before configuration update...");
                Thread.sleep(config.getUpdateConfigTimeout() * 1000);
                LOG.info("Updating DBforBix configuration...");
                if (config.checkConfigChanges())
                    action = "stop";
            }
                break;
            case "stop": {
                LOG.info("Stopping DBforBix...");
                config = config.reset();
                if (_zabbixSender != null) {
                    _zabbixSender.terminate();
                    while (_zabbixSender.isAlive())
                        Thread.yield();
                }
                //workTimers=new HashMap<String, Timer>();
                _zabbixSender = null;

                if (persSender != null) {
                    persSender.terminate();
                    while (persSender.isAlive())
                        Thread.yield();
                }

                DBManager dbman = DBManager.getInstance();
                dbman = dbman.cleanAll();

                action = "start";
            }
                break;
            default: {
                LOG.error("Unknown action " + action);
                System.exit(-5);
            }
                break;
            }
        } catch (Throwable th) {
            LOG.fatal("DBforBix crashed!", th);
        }
    }
}