Example usage for org.apache.commons.cli Options addOption

List of usage examples for org.apache.commons.cli Options addOption

Introduction

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

Prototype

public Options addOption(String opt, String longOpt, boolean hasArg, String description) 

Source Link

Document

Add an option that contains a short-name and a long-name.

Usage

From source file:apps.Source2XML.java

public static void main(String[] args) {
    Options options = new Options();

    options.addOption("i", null, true, "input file");
    options.addOption("o", null, true, "output file");
    options.addOption("reparse_xml", null, false, "reparse each XML entry to ensure the parser doesn't fail");

    Joiner commaJoin = Joiner.on(',');

    options.addOption("source_type", null, true,
            "document source type: " + commaJoin.join(SourceFactory.getDocSourceList()));

    Joiner spaceJoin = Joiner.on(' ');

    CommandLineParser parser = new org.apache.commons.cli.GnuParser();

    BufferedWriter outputFile = null;

    int docNum = 0;

    if (USE_LEMMATIZER && USE_STEMMER) {
        System.err.println("Bug/inconsistent code: cann't use the stemmer and lemmatizer at the same time!");
        System.exit(1);//from  ww w  .ja v a 2  s  . c om
    }

    //Stemmer stemmer = new Stemmer();
    KrovetzStemmer stemmer = new KrovetzStemmer();

    System.out.println("Using Stanford NLP?        " + USE_STANFORD);
    System.out.println("Using Stanford lemmatizer? " + USE_LEMMATIZER);
    System.out.println("Using stemmer?             " + USE_STEMMER
            + (USE_STEMMER ? " (class: " + stemmer.getClass().getCanonicalName() + ")" : ""));

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

        String inputFileName = null, outputFileName = null;

        if (cmd.hasOption("i")) {
            inputFileName = cmd.getOptionValue("i");
        } else {
            Usage("Specify 'input file'", options);
        }

        if (cmd.hasOption("o")) {
            outputFileName = cmd.getOptionValue("o");
        } else {
            Usage("Specify 'output file'", options);
        }

        outputFile = new BufferedWriter(
                new OutputStreamWriter(CompressUtils.createOutputStream(outputFileName)));

        String sourceName = cmd.getOptionValue("source_type");

        if (sourceName == null)
            Usage("Specify document source type", options);

        boolean reparseXML = options.hasOption("reparse_xml");

        DocumentSource inpDocSource = SourceFactory.createDocumentSource(sourceName, inputFileName);
        DocumentEntry inpDoc = null;
        TextCleaner textCleaner = new TextCleaner(
                new DictNoComments(new File("data/stopwords.txt"), true /* lower case */), USE_STANFORD,
                USE_LEMMATIZER);

        Map<String, String> outputMap = new HashMap<String, String>();

        outputMap.put(UtilConst.XML_FIELD_DOCNO, null);
        outputMap.put(UtilConst.XML_FIELD_TEXT, null);

        XmlHelper xmlHlp = new XmlHelper();

        if (reparseXML)
            System.out.println("Will reparse every XML entry to verify correctness!");

        while ((inpDoc = inpDocSource.next()) != null) {
            ++docNum;

            ArrayList<String> toks = textCleaner.cleanUp(inpDoc.mDocText);
            ArrayList<String> goodToks = new ArrayList<String>();
            for (String s : toks)
                if (s.length() <= MAX_WORD_LEN && // Exclude long and short words
                        s.length() >= MIN_WORD_LEN && isGoodWord(s))
                    goodToks.add(USE_STEMMER ? stemmer.stem(s) : s);

            String partlyCleanedText = spaceJoin.join(goodToks);
            String cleanText = XmlHelper.removeInvaildXMLChars(partlyCleanedText);
            // isGoodWord combiend with Stanford tokenizer should be quite restrictive already
            //cleanText = replaceSomePunct(cleanText);

            outputMap.replace(UtilConst.XML_FIELD_DOCNO, inpDoc.mDocId);
            outputMap.replace(UtilConst.XML_FIELD_TEXT, cleanText);

            String xml = xmlHlp.genXMLIndexEntry(outputMap);

            if (reparseXML) {
                try {
                    XmlHelper.parseDocWithoutXMLDecl(xml);
                } catch (Exception e) {
                    System.err.println("Error re-parsing xml for document ID: " + inpDoc.mDocId);
                    System.exit(1);
                }
            }

            /*
            {
              System.out.println(inpDoc.mDocId);
              System.out.println("=====================");
              System.out.println(partlyCleanedText);
              System.out.println("=====================");
              System.out.println(cleanText);
            } 
            */

            try {
                outputFile.write(xml);
                outputFile.write(NL);
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println("Error processing/saving a document!");
            }

            if (docNum % 1000 == 0)
                System.out.println(String.format("Processed %d documents", docNum));
        }

    } catch (ParseException e) {
        e.printStackTrace();
        Usage("Cannot parse arguments" + e, options);
    } catch (Exception e) {
        System.err.println("Terminating due to an exception: " + e);
        System.exit(1);
    } finally {
        System.out.println(String.format("Processed %d documents", docNum));

        try {
            if (null != outputFile) {
                outputFile.close();
                System.out.println("Output file is closed! all seems to be fine...");
            }
        } catch (IOException e) {
            System.err.println("IO exception: " + e);
            e.printStackTrace();
        }
    }
}

From source file:com.aerospike.examples.tweetaspike.Program.java

public static void main(String[] args) throws AerospikeException {
    try {// www .j  a v  a  2  s.c  om
        Options options = new Options();
        options.addOption("h", "host", true, "Server hostname (default: 12)");
        options.addOption("p", "port", true, "Server port (default: 3000)");
        options.addOption("n", "namespace", true, "Namespace (default: test)");
        options.addOption("s", "set", true, "Set (default: demo)");
        options.addOption("u", "usage", false, "Print usage.");

        CommandLineParser parser = new PosixParser();
        CommandLine cl = parser.parse(options, args, false);

        String host = cl.getOptionValue("h", "127.0.0.1");
        String portString = cl.getOptionValue("p", "3000");
        int port = Integer.parseInt(portString);
        String namespace = cl.getOptionValue("n", "test");
        String set = cl.getOptionValue("s", "demo");
        log.debug("Host: " + host);
        log.debug("Port: " + port);
        log.debug("Namespace: " + namespace);
        log.debug("Set: " + set);

        @SuppressWarnings("unchecked")
        List<String> cmds = cl.getArgList();
        if (cmds.size() == 0 && cl.hasOption("u")) {
            logUsage(options);
            return;
        }

        Program as = new Program(host, port, namespace, set);

        as.work();

    } catch (Exception e) {
        log.error("Critical error", e);
    }
}

From source file:com.ingby.socbox.bischeck.test.JDBCtest.java

static public void main(String[] args)
        throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {

    CommandLineParser parser = new GnuParser();
    CommandLine line = null;/*from w ww  .  ja  va 2  s  .  com*/

    // create the Options
    Options options = new Options();
    options.addOption("u", "usage", false, "show usage.");
    options.addOption("c", "connection", true, "the connection url");
    options.addOption("s", "sql", true, "the sql statement to run");
    options.addOption("m", "meta", true, "get the table meta data");
    options.addOption("C", "columns", true, "the number of columns to display, default 1");
    options.addOption("d", "driver", true, "the driver class");
    options.addOption("v", "verbose", false, "verbose outbout");

    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (org.apache.commons.cli.ParseException e) {
        System.out.println("Command parse error:" + e.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("JDBCtest", options);
        Util.ShellExit(1);
    }

    if (line.hasOption("verbose")) {
        verbose = true;
    }

    if (line.hasOption("usage")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("Bischeck", options);
        Util.ShellExit(0);
    }

    String driverclassname = null;
    if (!line.hasOption("driver")) {
        System.out.println("Driver class must be set");
        Util.ShellExit(1);
    } else {
        driverclassname = line.getOptionValue("driver");
        outputln("DriverClass: " + driverclassname);
    }

    String connectionname = null;
    if (!line.hasOption("connection")) {
        System.out.println("Connection url must be set");
        Util.ShellExit(1);
    } else {
        connectionname = line.getOptionValue("connection");
        outputln("Connection: " + connectionname);
    }

    String sql = null;
    String tablename = null;

    if (line.hasOption("sql")) {
        sql = line.getOptionValue("sql");
        outputln("SQL: " + sql);

    }

    if (line.hasOption("meta")) {
        tablename = line.getOptionValue("meta");
        outputln("Table: " + tablename);
    }

    int nrColumns = 1;
    if (line.hasOption("columns")) {
        nrColumns = new Integer(line.getOptionValue("columns"));
    }

    long execStart = 0l;
    long execEnd = 0l;
    long openStart = 0l;
    long openEnd = 0l;
    long metaStart = 0l;
    long metaEnd = 0l;

    Class.forName(driverclassname).newInstance();
    openStart = System.currentTimeMillis();
    Connection conn = DriverManager.getConnection(connectionname);
    openEnd = System.currentTimeMillis();

    if (tablename != null) {
        ResultSet rsCol = null;
        metaStart = System.currentTimeMillis();
        DatabaseMetaData md = conn.getMetaData();
        metaEnd = System.currentTimeMillis();

        rsCol = md.getColumns(null, null, tablename, null);
        if (verbose) {
            tabular("COLUMN_NAME");
            tabular("TYPE_NAME");
            tabular("COLUMN_SIZE");
            tabularlast("DATA_TYPE");
            outputln("");
        }

        while (rsCol.next()) {
            tabular(rsCol.getString("COLUMN_NAME"));
            tabular(rsCol.getString("TYPE_NAME"));
            tabular(rsCol.getString("COLUMN_SIZE"));
            tabularlast(rsCol.getString("DATA_TYPE"));
            outputln("", true);
        }
    }

    if (sql != null) {
        Statement stat = conn.createStatement();
        stat.setQueryTimeout(10);

        execStart = System.currentTimeMillis();
        ResultSet res = stat.executeQuery(sql);
        ResultSetMetaData rsmd = res.getMetaData();
        execEnd = System.currentTimeMillis();

        if (verbose) {
            for (int i = 1; i < nrColumns + 1; i++) {
                if (i != nrColumns)
                    tabular(rsmd.getColumnName(i));
                else
                    tabularlast(rsmd.getColumnName(i));
            }
            outputln("");
        }
        while (res.next()) {
            for (int i = 1; i < nrColumns + 1; i++) {
                if (i != nrColumns)
                    tabular(res.getString(i));
                else
                    tabularlast(res.getString(i));
            }
            outputln("", true);
        }

        stat.close();
        res.close();
    }

    conn.close();

    // Print the execution times
    outputln("Open time: " + (openEnd - openStart) + " ms");

    if (line.hasOption("meta")) {
        outputln("Meta time: " + (metaEnd - metaStart) + " ms");
    }

    if (line.hasOption("sql")) {
        outputln("Exec time: " + (execEnd - execStart) + " ms");
    }
}

From source file:GIST.IzbirkomExtractor.IzbirkomExtractor.java

/**
 * @param args/*w ww  .  j  a  va2 s. c  o m*/
 */
public static void main(String[] args) {

    // process command-line options
    Options options = new Options();
    options.addOption("n", "noaddr", false, "do not do any address matching (for testing)");
    options.addOption("i", "info", false, "create and populate address information table");
    options.addOption("h", "help", false, "this message");

    // database connection
    options.addOption("s", "server", true, "database server to connect to");
    options.addOption("d", "database", true, "OSM database name");
    options.addOption("u", "user", true, "OSM database user name");
    options.addOption("p", "pass", true, "OSM database password");

    // logging options
    options.addOption("l", "logdir", true, "log file directory (default './logs')");
    options.addOption("e", "loglevel", true, "log level (default 'FINEST')");

    // automatically generate the help statement
    HelpFormatter help_formatter = new HelpFormatter();

    // database URI for connection
    String dburi = null;

    // Information message for help screen
    String info_msg = "IzbirkomExtractor [options] <html_directory>";

    try {
        CommandLineParser parser = new GnuParser();
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption('h') || cmd.getArgs().length != 1) {
            help_formatter.printHelp(info_msg, options);
            System.exit(1);
        }

        /* prohibit n and i together */
        if (cmd.hasOption('n') && cmd.hasOption('i')) {
            System.err.println("Options 'n' and 'i' cannot be used together.");
            System.exit(1);
        }

        /* require database arguments without -n */
        if (cmd.hasOption('n')
                && (cmd.hasOption('s') || cmd.hasOption('d') || cmd.hasOption('u') || cmd.hasOption('p'))) {
            System.err.println("Options 'n' and does not need any databse parameters.");
            System.exit(1);
        }

        /* require all 4 database options to be used together */
        if (!cmd.hasOption('n')
                && !(cmd.hasOption('s') && cmd.hasOption('d') && cmd.hasOption('u') && cmd.hasOption('p'))) {
            System.err.println(
                    "For database access all of the following arguments have to be specified: server, database, user, pass");
            System.exit(1);
        }

        /* useful variables */
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm");
        String dateString = formatter.format(new Date());

        /* setup logging */
        File logdir = new File(cmd.hasOption('l') ? cmd.getOptionValue('l') : "logs");
        FileUtils.forceMkdir(logdir);
        File log_file_name = new File(
                logdir + "/" + IzbirkomExtractor.class.getName() + "-" + formatter.format(new Date()) + ".log");
        FileHandler log_file = new FileHandler(log_file_name.getPath());

        /* create "latest" link to currently created log file */
        Path latest_log_link = Paths.get(logdir + "/latest");
        Files.deleteIfExists(latest_log_link);
        Files.createSymbolicLink(latest_log_link, Paths.get(log_file_name.getName()));

        log_file.setFormatter(new SimpleFormatter());
        LogManager.getLogManager().reset(); // prevents logging to console
        logger.addHandler(log_file);
        logger.setLevel(cmd.hasOption('e') ? Level.parse(cmd.getOptionValue('e')) : Level.FINEST);

        // open directory with HTML files and create file list
        File dir = new File(cmd.getArgs()[0]);
        if (!dir.isDirectory()) {
            System.err.println("Unable to find directory '" + cmd.getArgs()[0] + "', exiting");
            System.exit(1);
        }
        PathMatcher pmatcher = FileSystems.getDefault()
                .getPathMatcher("glob:?  * ?*.html");
        ArrayList<File> html_files = new ArrayList<>();
        for (Path file : Files.newDirectoryStream(dir.toPath()))
            if (pmatcher.matches(file.getFileName()))
                html_files.add(file.toFile());
        if (html_files.size() == 0) {
            System.err.println("No matching HTML files found in '" + dir.getAbsolutePath() + "', exiting");
            System.exit(1);
        }

        // create csvResultSink
        FileOutputStream csvout_file = new FileOutputStream("parsed_addresses-" + dateString + ".csv");
        OutputStreamWriter csvout = new OutputStreamWriter(csvout_file, "UTF-8");
        ResultSink csvResultSink = new CSVResultSink(csvout, new CSVStrategy('|', '"', '#'));

        // Connect to DB and osmAddressMatcher
        AddressMatcher osmAddressMatcher;
        DBSink dbSink = null;
        DBInfoSink dbInfoSink = null;
        if (cmd.hasOption('n')) {
            osmAddressMatcher = new DummyAddressMatcher();
        } else {
            dburi = "jdbc:postgresql://" + cmd.getOptionValue('s') + "/" + cmd.getOptionValue('d');
            Connection con = DriverManager.getConnection(dburi, cmd.getOptionValue('u'),
                    cmd.getOptionValue('p'));
            osmAddressMatcher = new OsmAddressMatcher(con);
            dbSink = new DBSink(con);
            if (cmd.hasOption('i'))
                dbInfoSink = new DBInfoSink(con);
        }

        /* create resultsinks */
        SinkMultiplexor sm = SinkMultiplexor.newSinkMultiplexor();
        sm.addResultSink(csvResultSink);
        if (dbSink != null) {
            sm.addResultSink(dbSink);
            if (dbInfoSink != null)
                sm.addResultSink(dbInfoSink);
        }

        // create tableExtractor
        TableExtractor te = new TableExtractor(osmAddressMatcher, sm);

        // TODO: printout summary of options: processing date/time, host, directory of HTML files, jdbc uri, command line with parameters

        // iterate through files
        logger.info("Start processing " + html_files.size() + " files in " + dir);
        for (int i = 0; i < html_files.size(); i++) {
            System.err.println("Parsing #" + i + ": " + html_files.get(i));
            te.processHTMLfile(html_files.get(i));
        }

        System.err.println("Processed " + html_files.size() + " HTML files");
        logger.info("Finished processing " + html_files.size() + " files in " + dir);

    } catch (ParseException e1) {
        System.err.println("Failed to parse CLI: " + e1.getMessage());
        help_formatter.printHelp(info_msg, options);
        System.exit(1);
    } catch (IOException e) {
        System.err.println("I/O Exception: " + e.getMessage());
        e.printStackTrace();
        System.exit(1);
    } catch (SQLException e) {
        System.err.println("Database '" + dburi + "': " + e.getMessage());
        System.exit(1);
    } catch (ResultSinkException e) {
        System.err.println("Failed to initialize ResultSink: " + e.getMessage());
        System.exit(1);
    } catch (TableExtractorException e) {
        System.err.println("Failed to initialize Table Extractor: " + e.getMessage());
        System.exit(1);
    } catch (CloneNotSupportedException | IllegalAccessException | InstantiationException e) {
        System.err.println("Something really odd happened: " + e.getMessage());
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:net.mybox.mybox.Server.java

/**
 * Handle the command line args and instantiate the Server
 * @param args/*from   ww w .j a  v a 2  s  .co  m*/
 */
public static void main(String args[]) {

    Options options = new Options();
    options.addOption("c", "config", true, "configuration file");
    //    options.addOption("d", "database", true, "accounts database file"); // TODO: handle in config?
    options.addOption("a", "apphome", true, "application home directory");
    options.addOption("h", "help", false, "show help screen");
    options.addOption("V", "version", false, "print the Mybox version");

    CommandLineParser line = new GnuParser();
    CommandLine cmd = null;

    try {
        cmd = line.parse(options, args);
    } catch (ParseException exp) {
        System.err.println(exp.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(Server.class.getName(), options);
        return;
    }

    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(Server.class.getName(), options);
        return;
    }

    if (cmd.hasOption("V")) {
        Client.printMessage("version " + Common.appVersion);
        return;
    }

    if (cmd.hasOption("a")) {
        String appHomeDir = cmd.getOptionValue("a");
        try {
            Common.updatePaths(appHomeDir);
        } catch (FileNotFoundException e) {
            printErrorExit(e.getMessage());
        }

        updatePaths();
    }

    String configFile = defaultConfigFile;
    //    String accountsDBfile = defaultAccountsDbFile;

    if (cmd.hasOption("c")) {
        configFile = cmd.getOptionValue("c");
    }

    File fileCheck = new File(configFile);
    if (!fileCheck.isFile())
        Server.printErrorExit("Config not found: " + configFile + "\nPlease run ServerSetup");

    //    if (cmd.hasOption("d")){
    //      accountsDBfile = cmd.getOptionValue("d");
    //    }
    //
    //    fileCheck = new File(accountsDBfile);
    //    if (!fileCheck.isFile())
    //      Server.printErrorExit("Error: account database not found " + accountsDBfile);

    Server server = new Server(configFile);
}

From source file:com.zimbra.cs.session.WaitSetValidator.java

/**
 * @param args/*ww  w.  ja v  a 2  s.c o  m*/
 */
public static void main(String[] args) {
    CliUtil.toolSetup();

    WaitSetValidator t = new WaitSetValidator();

    CommandLineParser parser = new PosixParser();
    Options options = new Options();

    options.addOption("i", "id", true, "Wait Set ID");
    options.addOption("h", "host", true, "Hostname");
    options.addOption("u", "user", true, "Username");
    options.addOption("p", "pw", true, "Password");
    options.addOption("?", "help", false, "Help");
    options.addOption("v", "verbose", false, "Verbose");

    CommandLine cl = null;
    boolean err = false;

    String[] hosts;
    String[] ids;

    try {
        cl = parser.parse(options, args, true);
    } catch (ParseException pe) {
        printError("error: " + pe.getMessage());
        err = true;
    }

    if (err || cl.hasOption('?')) {
        t.usage();
    }

    String id = null, host = null, user = null, pw = null;

    if (!cl.hasOption('i'))
        t.usage();

    id = cl.getOptionValue('i');

    if (cl.hasOption('h'))
        host = cl.getOptionValue('h');
    else
        host = "http://localhost:7071/service/admin";

    if (cl.hasOption('u'))
        user = cl.getOptionValue('u');
    else
        user = "admin";

    if (cl.hasOption('p'))
        pw = cl.getOptionValue('p');
    else
        pw = "test123";

    if (cl.hasOption('v'))
        t.setVerbose(true);

    hosts = host.split(",");
    ids = id.split(",");

    if (hosts.length != ids.length) {
        System.err.println("If multiple hosts or ids are specified, the same number is required of each");
        System.exit(3);
    }

    for (int i = 0; i < hosts.length; i++) {
        if (i > 0)
            System.out.println("\n\n");
        System.out.println("Checking server " + hosts[i] + " waitsetId=" + ids[i]);
        t.run(ids[i], hosts[i], user, pw);
    }
}

From source file:apps.LuceneIndexer.java

public static void main(String[] args) {
    Options options = new Options();

    options.addOption("i", null, true, "input file");
    options.addOption("o", null, true, "output directory");
    options.addOption("r", null, true, "optional output TREC-format QREL file");

    options.addOption("bm25_b", null, true, "BM25 parameter: b");
    options.addOption("bm25_k1", null, true, "BM25 parameter: k1");
    options.addOption("bm25fixed", null, false, "use the fixed BM25 similarity");

    Joiner commaJoin = Joiner.on(',');
    Joiner spaceJoin = Joiner.on(' ');

    options.addOption("source_type", null, true,
            "document source type: " + commaJoin.join(SourceFactory.getDocSourceList()));

    // If you increase this value, you may need to modify the following line in *.sh file
    // export MAVEN_OPTS="-Xms8192m -server"
    double ramBufferSizeMB = 1024 * 8; // 8 GB

    CommandLineParser parser = new org.apache.commons.cli.GnuParser();

    IndexWriter indexWriter = null;/*from   w w w .j a  v a2 s.c  om*/
    BufferedWriter qrelWriter = null;

    int docNum = 0;

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

        String inputFileName = null, outputDirName = null, qrelFileName = null;

        if (cmd.hasOption("i")) {
            inputFileName = cmd.getOptionValue("i");
        } else {
            Usage("Specify 'input file'", options);
        }

        if (cmd.hasOption("o")) {
            outputDirName = cmd.getOptionValue("o");
        } else {
            Usage("Specify 'index directory'", options);
        }

        if (cmd.hasOption("r")) {
            qrelFileName = cmd.getOptionValue("r");
        }

        String sourceName = cmd.getOptionValue("source_type");

        if (sourceName == null)
            Usage("Specify document source type", options);

        if (qrelFileName != null)
            qrelWriter = new BufferedWriter(new FileWriter(qrelFileName));

        File outputDir = new File(outputDirName);
        if (!outputDir.exists()) {
            if (!outputDir.mkdirs()) {
                System.out.println("couldn't create " + outputDir.getAbsolutePath());
                System.exit(1);
            }
        }
        if (!outputDir.isDirectory()) {
            System.out.println(outputDir.getAbsolutePath() + " is not a directory!");
            System.exit(1);
        }
        if (!outputDir.canWrite()) {
            System.out.println("Can't write to " + outputDir.getAbsolutePath());
            System.exit(1);
        }

        boolean useFixedBM25 = cmd.hasOption("bm25fixed");

        float bm25_k1 = UtilConst.BM25_K1_DEFAULT, bm25_b = UtilConst.BM25_B_DEFAULT;

        if (cmd.hasOption("bm25_k1")) {
            try {
                bm25_k1 = Float.parseFloat(cmd.getOptionValue("bm25_k1"));
            } catch (NumberFormatException e) {
                Usage("Wrong format for 'bm25_k1'", options);
            }
        }

        if (cmd.hasOption("bm25_b")) {
            try {
                bm25_b = Float.parseFloat(cmd.getOptionValue("bm25_b"));
            } catch (NumberFormatException e) {
                Usage("Wrong format for 'bm25_b'", options);
            }
        }

        EnglishAnalyzer analyzer = new EnglishAnalyzer();
        FSDirectory indexDir = FSDirectory.open(Paths.get(outputDirName));
        IndexWriterConfig indexConf = new IndexWriterConfig(analyzer);

        /*
            OpenMode.CREATE creates a new index or overwrites an existing one.
            https://lucene.apache.org/core/6_0_0/core/org/apache/lucene/index/IndexWriterConfig.OpenMode.html#CREATE
        */
        indexConf.setOpenMode(OpenMode.CREATE);
        indexConf.setRAMBufferSizeMB(ramBufferSizeMB);

        System.out.println(String.format("BM25 parameters k1=%f b=%f ", bm25_k1, bm25_b));

        if (useFixedBM25) {
            System.out.println(String.format("Using fixed BM25Simlarity, k1=%f b=%f", bm25_k1, bm25_b));
            indexConf.setSimilarity(new BM25SimilarityFix(bm25_k1, bm25_b));
        } else {
            System.out.println(String.format("Using Lucene BM25Similarity, k1=%f b=%f", bm25_k1, bm25_b));
            indexConf.setSimilarity(new BM25Similarity(bm25_k1, bm25_b));
        }

        indexWriter = new IndexWriter(indexDir, indexConf);

        DocumentSource inpDocSource = SourceFactory.createDocumentSource(sourceName, inputFileName);
        DocumentEntry inpDoc = null;
        TextCleaner textCleaner = new TextCleaner(null);

        while ((inpDoc = inpDocSource.next()) != null) {
            ++docNum;

            Document luceneDoc = new Document();
            ArrayList<String> cleanedToks = textCleaner.cleanUp(inpDoc.mDocText);
            String cleanText = spaceJoin.join(cleanedToks);

            //        System.out.println(inpDoc.mDocId);
            //        System.out.println(cleanText);
            //        System.out.println("==============================");

            luceneDoc.add(new StringField(UtilConst.FIELD_ID, inpDoc.mDocId, Field.Store.YES));
            luceneDoc.add(new TextField(UtilConst.FIELD_TEXT, cleanText, Field.Store.YES));
            indexWriter.addDocument(luceneDoc);

            if (inpDoc.mIsRel != null && qrelWriter != null) {
                saveQrelOneEntry(qrelWriter, inpDoc.mQueryId, inpDoc.mDocId, inpDoc.mIsRel ? MAX_GRADE : 0);
            }
            if (docNum % 1000 == 0)
                System.out.println(String.format("Indexed %d documents", docNum));

        }

    } catch (ParseException e) {
        e.printStackTrace();
        Usage("Cannot parse arguments" + e, options);
    } catch (Exception e) {
        System.err.println("Terminating due to an exception: " + e);
        System.exit(1);
    } finally {
        System.out.println(String.format("Indexed %d documents", docNum));

        try {
            if (null != indexWriter)
                indexWriter.close();
            if (null != qrelWriter)
                qrelWriter.close();
        } catch (IOException e) {
            System.err.println("IO exception: " + e);
            e.printStackTrace();
        }
    }
}

From source file:com.thimbleware.jmemcached.Main.java

public static void main(String[] args) throws Exception {
    // look for external log4j.properties

    // setup command line options
    Options options = new Options();
    options.addOption("h", "help", false, "print this help screen");
    options.addOption("bl", "block-store", false, "use external (from JVM) heap");
    options.addOption("f", "mapped-file", false, "use external (from JVM) heap through a memory mapped file");
    options.addOption("bs", "block-size", true,
            "block size (in bytes) for external memory mapped file allocator.  default is 8 bytes");
    options.addOption("i", "idle", true, "disconnect after idle <x> seconds");
    options.addOption("p", "port", true, "port to listen on");
    options.addOption("m", "memory", true,
            "max memory to use; in bytes, specify K, kb, M, GB for larger units");
    options.addOption("c", "ceiling", true,
            "ceiling memory to use; in bytes, specify K, kb, M, GB for larger units");
    options.addOption("l", "listen", true, "Address to listen on");
    options.addOption("s", "size", true, "max items");
    options.addOption("b", "binary", false, "binary protocol mode");
    options.addOption("V", false, "Show version number");
    options.addOption("v", false, "verbose (show commands)");

    // read command line options
    CommandLineParser parser = new PosixParser();
    CommandLine cmdline = parser.parse(options, args);

    if (cmdline.hasOption("help") || cmdline.hasOption("h")) {
        System.out.println("Memcached Version " + MemCacheDaemon.memcachedVersion);
        System.out.println("http://thimbleware.com/projects/memcached\n");

        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java -jar memcached.jar", options);
        return;/* w w w  .j  a va2s. c o  m*/
    }

    if (cmdline.hasOption("V")) {
        System.out.println("Memcached Version " + MemCacheDaemon.memcachedVersion);
        return;
    }

    int port = 11211;
    if (cmdline.hasOption("p")) {
        port = Integer.parseInt(cmdline.getOptionValue("p"));
    } else if (cmdline.hasOption("port")) {
        port = Integer.parseInt(cmdline.getOptionValue("port"));
    }

    InetSocketAddress addr = new InetSocketAddress(port);
    if (cmdline.hasOption("l")) {
        addr = new InetSocketAddress(cmdline.getOptionValue("l"), port);
    } else if (cmdline.hasOption("listen")) {
        addr = new InetSocketAddress(cmdline.getOptionValue("listen"), port);
    }

    int max_size = 1000000;
    if (cmdline.hasOption("s"))
        max_size = (int) Bytes.valueOf(cmdline.getOptionValue("s")).bytes();
    else if (cmdline.hasOption("size"))
        max_size = (int) Bytes.valueOf(cmdline.getOptionValue("size")).bytes();

    System.out.println("Setting max cache elements to " + String.valueOf(max_size));

    int idle = -1;
    if (cmdline.hasOption("i")) {
        idle = Integer.parseInt(cmdline.getOptionValue("i"));
    } else if (cmdline.hasOption("idle")) {
        idle = Integer.parseInt(cmdline.getOptionValue("idle"));
    }

    boolean memoryMapped = false;
    if (cmdline.hasOption("f")) {
        memoryMapped = true;
    } else if (cmdline.hasOption("mapped-file")) {
        memoryMapped = true;
    }

    boolean blockStore = false;
    if (cmdline.hasOption("bl")) {
        blockStore = true;
    } else if (cmdline.hasOption("block-store")) {
        blockStore = true;
    }

    boolean verbose = false;
    if (cmdline.hasOption("v")) {
        verbose = true;
    }

    long ceiling;
    if (cmdline.hasOption("c")) {
        ceiling = Bytes.valueOf(cmdline.getOptionValue("c")).bytes();
        System.out.println("Setting ceiling memory size to " + Bytes.bytes(ceiling).megabytes() + "M");
    } else if (cmdline.hasOption("ceiling")) {
        ceiling = Bytes.valueOf(cmdline.getOptionValue("ceiling")).bytes();
        System.out.println("Setting ceiling memory size to " + Bytes.bytes(ceiling).megabytes() + "M");
    } else if (!memoryMapped) {
        ceiling = 1024000;
        System.out.println(
                "Setting ceiling memory size to default limit of " + Bytes.bytes(ceiling).megabytes() + "M");
    } else {
        System.out
                .println("ERROR : ceiling memory size mandatory when external memory mapped file is specified");

        return;
    }

    boolean binary = false;
    if (cmdline.hasOption("b")) {
        binary = true;
    }

    int blockSize = 8;
    if (!memoryMapped && (cmdline.hasOption("bs") || cmdline.hasOption("block-size"))) {
        System.out.println(
                "WARN : block size option is only valid for memory mapped external heap storage; ignoring");
    } else if (cmdline.hasOption("bs")) {
        blockSize = Integer.parseInt(cmdline.getOptionValue("bs"));
    } else if (cmdline.hasOption("block-size")) {
        blockSize = Integer.parseInt(cmdline.getOptionValue("block-size"));
    }

    long maxBytes;
    if (cmdline.hasOption("m")) {
        maxBytes = Bytes.valueOf(cmdline.getOptionValue("m")).bytes();
        System.out.println("Setting max memory size to " + Bytes.bytes(maxBytes).gigabytes() + "GB");
    } else if (cmdline.hasOption("memory")) {
        maxBytes = Bytes.valueOf(cmdline.getOptionValue("memory")).bytes();
        System.out.println("Setting max memory size to " + Bytes.bytes(maxBytes).gigabytes() + "GB");
    } else if (!memoryMapped) {
        maxBytes = Runtime.getRuntime().maxMemory();
        System.out
                .println("Setting max memory size to JVM limit of " + Bytes.bytes(maxBytes).gigabytes() + "GB");
    } else {
        System.out.println(
                "ERROR : max memory size and ceiling size are mandatory when external memory mapped file is specified");
        return;
    }

    if (!memoryMapped && !blockStore && maxBytes > Runtime.getRuntime().maxMemory()) {
        System.out.println("ERROR : JVM heap size is not big enough. use '-Xmx"
                + String.valueOf(maxBytes / 1024000) + "m' java argument before the '-jar' option.");
        return;
    } else if ((memoryMapped || !blockStore) && maxBytes > Integer.MAX_VALUE) {
        System.out.println(
                "ERROR : when external memory mapped, memory size may not exceed the size of Integer.MAX_VALUE ("
                        + Bytes.bytes(Integer.MAX_VALUE).gigabytes() + "GB");
        return;
    }

    // create daemon and start it
    final MemCacheDaemon<LocalCacheElement> daemon = new MemCacheDaemon<LocalCacheElement>();

    CacheStorage<Key, LocalCacheElement> storage;
    if (blockStore) {
        BlockStoreFactory blockStoreFactory = ByteBufferBlockStore.getFactory();

        storage = new BlockStorageCacheStorage(8, (int) ceiling, blockSize, maxBytes, max_size,
                blockStoreFactory);
    } else if (memoryMapped) {
        BlockStoreFactory blockStoreFactory = MemoryMappedBlockStore.getFactory();

        storage = new BlockStorageCacheStorage(8, (int) ceiling, blockSize, maxBytes, max_size,
                blockStoreFactory);
    } else {
        storage = ConcurrentLinkedHashMap.create(ConcurrentLinkedHashMap.EvictionPolicy.FIFO, max_size,
                maxBytes);
    }

    daemon.setCache(new CacheImpl(storage));
    daemon.setBinary(binary);
    daemon.setAddr(addr);
    daemon.setIdleTime(idle);
    daemon.setVerbose(verbose);
    daemon.start();

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        public void run() {
            if (daemon.isRunning())
                daemon.stop();
        }
    }));
}

From source file:ca.ualberta.exemplar.evaluation.BenchmarkBinary.java

public static void main(String[] rawArgs) throws FileNotFoundException, UnsupportedEncodingException {

    CommandLineParser parser = new BasicParser();

    Options options = new Options();
    options.addOption("p", "parser", true, "Which parser to use. (stanford | malt)");

    CommandLine line = null;/* w  w w .  j a  va2 s.c o m*/

    try {
        line = parser.parse(options, rawArgs);
    } catch (ParseException exp) {
        System.err.println(exp.getMessage());
        System.exit(1);
    }

    String[] args = line.getArgs();
    String parserName = line.getOptionValue("parser", "stanford");

    System.out.println("Using " + parserName + " parser");

    File evaluationFile = new File(args[0]);
    File outputFile = new File(args[1]);

    BenchmarkBinary evaluation = new BenchmarkBinary(evaluationFile, outputFile, parserName);

    //read evaluation file with sentences annotated with golden statements
    // and run Exemplar over them.
    evaluation.runAndTime();

}