Example usage for org.apache.commons.cli CommandLine getOptions

List of usage examples for org.apache.commons.cli CommandLine getOptions

Introduction

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

Prototype

public Option[] getOptions() 

Source Link

Document

Returns an array of the processed Option s.

Usage

From source file:pt.ua.tm.neji.evaluation.EvaluateMain.java

public static void main(String... args) {
    boolean performExactChunks = false, performLeftChunks = false, performRightChunks = false,
            performSharedChunks = false, performOverlapChunks = false, performExactAnn = false,
            performLeftAnn = false, performRightAnn = false, performSharedAnn = false,
            performOverlapAnn = false;// w  ww.  j av a 2 s .c  o  m

    CommandLineParser parser = new GnuParser();
    Options mainOptions = new Options();
    mainOptions.addOption("h", "help", false, "Print this usage information.");

    mainOptions.addOption("g", "gold", true, "Folder with gold files.");
    mainOptions.addOption("s", "silver", true, "Folder with silver files.");
    mainOptions.addOption("gf", "gold-filter", true, "Wildcard to filter files in gold folder");
    mainOptions.addOption("sf", "silver-filter", true, "Wildcard to filter files in silver folder");
    mainOptions.addOption("m", "mappers", true, "Folder with mapper files.");

    int numOfAvailableTests = TestType.values().length;
    Option o = new Option("tnorm", "test-normalization", true, "Set identifiers evaluations.");
    o.setArgs(numOfAvailableTests);
    mainOptions.addOption(o);
    o = new Option("tner", "test-ner", true, "Set chunk evaluations.");
    o.setArgs(numOfAvailableTests);
    mainOptions.addOption(o);

    CommandLine commandLine;
    try {
        // Parse the program arguments
        commandLine = parser.parse(mainOptions, args);
    } catch (ParseException ex) {
        logger.error("There was a problem processing the input arguments.", ex);
        return;
    }

    // Show help text
    if (commandLine.hasOption('h') || commandLine.getOptions().length == 0) {
        printHelp(mainOptions, "");
        return;
    }

    // Get gold folder
    String goldFolderPath;
    if (commandLine.hasOption('g')) {
        goldFolderPath = commandLine.getOptionValue('g');
    } else {
        printHelp(mainOptions, "Please specify the gold folder.");
        return;
    }

    File goldFolder = new File(goldFolderPath);
    if (!goldFolder.isDirectory() || !goldFolder.canRead()) {
        logger.error("The specified path is not a folder or is not readable.");
        return;
    }

    // Get gold folder filter
    String goldFolderWildcard = null;
    if (commandLine.hasOption("gf")) {
        goldFolderWildcard = commandLine.getOptionValue("gf");
    }

    // Get silver folder
    String silverFolderPath;
    if (commandLine.hasOption('s')) {
        silverFolderPath = commandLine.getOptionValue('s');
    } else {
        printHelp(mainOptions, "Please specify the silver folder.");
        return;
    }

    File silverFolder = new File(silverFolderPath);
    if (!silverFolder.isDirectory() || !silverFolder.canRead()) {
        logger.error("The specified path is not a folder or is not readable.");
        return;
    }

    // Get silver folder filter
    String silverFolderWildcard = null;
    if (commandLine.hasOption("sf")) {
        silverFolderWildcard = commandLine.getOptionValue("sf");
    }

    // Get mappers folder
    mappersFolderPath = null;
    if (commandLine.hasOption('m')) {
        mappersFolderPath = commandLine.getOptionValue('m');
        File mappersFolder = new File(mappersFolderPath);
        if (!mappersFolder.isDirectory() || !mappersFolder.canRead()) {
            logger.error("The specified path is not a folder or is not readable.");
            return;
        }
        mappersFolderPath = mappersFolder.getAbsolutePath() + File.separator;
    }

    // Get what evaluation tests to perform
    if (commandLine.hasOption("tnorm")) {
        for (String arg : commandLine.getOptionValues("tnorm")) {
            try {
                TestType type = TestType.valueOf(arg.toLowerCase());
                switch (type) {
                case all:
                    performExactAnn = true;
                    performLeftAnn = true;
                    performRightAnn = true;
                    performSharedAnn = true;
                    performOverlapAnn = true;
                    break;
                case exact:
                    performExactAnn = true;
                    break;
                case left:
                    performLeftAnn = true;
                    break;
                case right:
                    performRightAnn = true;
                    break;
                case shared:
                    performSharedAnn = true;
                    break;
                case overlap:
                    performOverlapAnn = true;
                    break;
                }
            } catch (Exception ex) {
                logger.error("Invalid test name \"" + arg + "\" at \"-tnorm\" command.");
                return;
            }
        }
    }

    if (commandLine.hasOption("tner")) {
        for (String arg : commandLine.getOptionValues("tner")) {
            try {
                TestType type = TestType.valueOf(arg.toLowerCase());
                switch (type) {
                case all:
                    performExactChunks = true;
                    performLeftChunks = true;
                    performRightChunks = true;
                    performSharedChunks = true;
                    performOverlapChunks = true;
                    break;
                case exact:
                    performExactChunks = true;
                    break;
                case left:
                    performLeftChunks = true;
                    break;
                case right:
                    performRightChunks = true;
                    break;
                case shared:
                    performSharedChunks = true;
                    break;
                case overlap:
                    performOverlapChunks = true;
                    break;
                }
            } catch (Exception ex) {
                logger.error("Invalid test name \"" + arg + "\" at \"-tner\" command.");
                return;
            }
        }
    }

    if (!performExactChunks && !performLeftChunks && !performRightChunks && !performSharedChunks
            && !performOverlapChunks && !performExactAnn && !performLeftAnn && !performRightAnn
            && !performSharedAnn && !performOverlapAnn) {
        logger.error("Please specify at least one evaluation test to perform.");
        return;
    }

    //        String goldFolderPath = "resources/corpus/bionlp2011/dev/";
    //        String silverFolderPath = "resources/corpus/bionlp2011/dev/silver/dictionaries/";
    //        String silverFolderPath = "resources/corpus/bionlp2011/dev/silver/ml/";

    // CRAFT
    //        String goldFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/craft/gold/";
    //        String silverFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/craft/silver/";

    //        args = new String[]{"resources/corpus/craft2/silver/","resources/corpus/craft2/silver/"};
    //        args = new String[]{"resources/corpus/craft2/gold/","resources/corpus/craft2/silver/"};
    //        args = new String[]{"resources/corpus/craft2/gold/","resources/temp/whatizit/craft/silver/a1/"};

    // CRAFT2
    //        String goldFolderPath = args[0];
    //        String silverFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/craft2/silver/";
    //        String silverFolderPath = "/Volumes/data/Backups/2013-04-11_desktop/Downloads/whatizit/craft/ukpmc/a1/";
    //        String silverFolderPath = args[1];
    //String silverFolderPath = "resources/temp/whatizit/craft/silver/a1/";
    //        String silverFolderPath = "/Users/david/Downloads/whatizit/craft/ukpmc/a1";
    //        String silverFolderPath = "/Users/david/Downloads/Craft1.0Annotations/";

    // ANEM COCOA
    //        String goldFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/anem/test/gold/";
    //        String silverFolderPath = "/Users/david/Downloads/anem_craft/test/";

    // MLEE
    //        String goldFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/mlee/gold/";
    //        String silverFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/mlee/silver/";

    // CellFinder
    //        String goldFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/cellfinder/gold/";
    //        String silverFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/cellfinder/silver/";

    // Arizona
    //        String goldFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/arizona/gold/";
    //        String silverFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/arizona/silver/";

    // NCBI Disease
    //        String silverFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/ncbi/silver/";
    //        String goldFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/ncbi/gold/";
    //        String silverFolderPath = "/Users/david/Downloads/whatizit/ncbi/whatizitDiseaseUMLSDict/a1/";

    // ANEM
    //        String goldFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/anem/test/gold/";
    //        String silverFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/anem/test/silver/";

    // SCAI
    //        String goldFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/scai/test/gold/";
    //        String silverFolderPath = "/Volumes/data/Dropbox/PhD/work/platform/code/neji/resources/corpus/scai/test/silver/";

    // Get a lists of gold and silver files
    File[] goldFiles;
    if (goldFolderWildcard == null)
        goldFiles = goldFolder.listFiles();
    else
        goldFiles = goldFolder.listFiles(new FileUtil.Filter(new String[] { goldFolderWildcard }));

    File[] silverFiles;
    if (silverFolderWildcard == null)
        silverFiles = silverFolder.listFiles();
    else
        silverFiles = silverFolder.listFiles(new FileUtil.Filter(new String[] { silverFolderWildcard }));

    List<File> goldFilesList = Arrays.asList(goldFiles);
    List<File> goldFilesList2 = new ArrayList<>();
    for (File f : goldFilesList) {
        if (!f.isDirectory())
            goldFilesList2.add(f);
    }
    Collections.sort(goldFilesList2);
    goldFiles = goldFilesList2.toArray(new File[goldFilesList2.size()]);

    List<File> silverFilesList = Arrays.asList(silverFiles);
    List<File> silverFilesList2 = new ArrayList<>();
    for (File f : silverFilesList) {
        if (!f.isDirectory())
            silverFilesList2.add(f);
    }
    Collections.sort(silverFilesList2);
    silverFiles = silverFilesList2.toArray(new File[silverFilesList2.size()]);

    if (goldFiles.length != silverFiles.length) {
        throw new RuntimeException("Folders are not compatible (the number of files is not equal).");
    }

    // Perform tests
    if (performExactChunks || performExactAnn) {
        logger.info("#####################");
        logger.info("EXACT");
        logger.info("#####################");
    }
    if (performExactChunks) {
        logger.info("\tIdentifier matching: NONE");
        getCompleteEvaluator(goldFiles, silverFiles, CompleteEvaluator.EvaluationType.Exact,
                IdentifierMatch.NONE);
    }
    if (performExactAnn) {
        logger.info("\tIdentifier matching: EXACT");
        getCompleteEvaluator(goldFiles, silverFiles, CompleteEvaluator.EvaluationType.Exact,
                IdentifierMatch.EXACT);
        logger.info("\tIdentifier matching: CONTAINS");
        getCompleteEvaluator(goldFiles, silverFiles, CompleteEvaluator.EvaluationType.Exact,
                IdentifierMatch.CONTAIN);
    }

    if (performLeftChunks || performLeftAnn) {
        logger.info("#####################");
        logger.info("LEFT");
        logger.info("#####################");
    }
    if (performLeftChunks) {
        logger.info("\tIdentifier matching: NONE");
        getCompleteEvaluator(goldFiles, silverFiles, CompleteEvaluator.EvaluationType.Left,
                IdentifierMatch.NONE);
    }
    if (performLeftAnn) {
        logger.info("\tIdentifier matching: EXACT");
        getCompleteEvaluator(goldFiles, silverFiles, CompleteEvaluator.EvaluationType.Left,
                IdentifierMatch.EXACT);
        logger.info("\tIdentifier matching: CONTAINS");
        getCompleteEvaluator(goldFiles, silverFiles, CompleteEvaluator.EvaluationType.Left,
                IdentifierMatch.CONTAIN);
    }

    if (performRightChunks || performRightAnn) {
        logger.info("#####################");
        logger.info("RIGHT");
        logger.info("#####################");
    }
    if (performRightChunks) {
        logger.info("\tIdentifier matching: NONE");
        getCompleteEvaluator(goldFiles, silverFiles, CompleteEvaluator.EvaluationType.Right,
                IdentifierMatch.NONE);
    }
    if (performRightAnn) {
        logger.info("\tIdentifier matching: EXACT");
        getCompleteEvaluator(goldFiles, silverFiles, CompleteEvaluator.EvaluationType.Right,
                IdentifierMatch.EXACT);
        logger.info("\tIdentifier matching: CONTAINS");
        getCompleteEvaluator(goldFiles, silverFiles, CompleteEvaluator.EvaluationType.Right,
                IdentifierMatch.CONTAIN);
    }

    if (performSharedChunks || performSharedAnn) {
        logger.info("#####################");
        logger.info("SHARED");
        logger.info("#####################");
    }
    if (performSharedChunks) {
        logger.info("\tIdentifier matching: NONE");
        getCompleteEvaluator(goldFiles, silverFiles, CompleteEvaluator.EvaluationType.Shared,
                IdentifierMatch.NONE);
    }
    if (performSharedAnn) {
        logger.info("\tIdentifier matching: EXACT");
        getCompleteEvaluator(goldFiles, silverFiles, CompleteEvaluator.EvaluationType.Shared,
                IdentifierMatch.EXACT);
        logger.info("\tIdentifier matching: CONTAINS");
        getCompleteEvaluator(goldFiles, silverFiles, CompleteEvaluator.EvaluationType.Shared,
                IdentifierMatch.CONTAIN);
    }

    if (performOverlapChunks || performOverlapAnn) {
        logger.info("#####################");
        logger.info("OVERLAP");
        logger.info("#####################");
    }
    if (performOverlapChunks) {
        logger.info("\tIdentifier matching: NONE");
        getCompleteEvaluator(goldFiles, silverFiles, CompleteEvaluator.EvaluationType.Overlap,
                IdentifierMatch.NONE);
    }
    if (performOverlapAnn) {
        logger.info("\tIdentifier matching: EXACT");
        getCompleteEvaluator(goldFiles, silverFiles, CompleteEvaluator.EvaluationType.Overlap,
                IdentifierMatch.EXACT);
        logger.info("\tIdentifier matching: CONTAINS");
        getCompleteEvaluator(goldFiles, silverFiles, CompleteEvaluator.EvaluationType.Overlap,
                IdentifierMatch.CONTAIN);
    }

    /*
    logger.info("Num. UNIPROTS: {}", CompleteEvaluator.numUNIPROTS);
    logger.info("Num. Uniprots mapped: {}", CompleteEvaluator.numUNIPROTSmapped);
    logger.info("Ratio: {}%", ((double)CompleteEvaluator.numUNIPROTSmapped/(double)CompleteEvaluator.numUNIPROTS)*100);
    logger.info("");
    logger.info("Num. PRGE concept names: {}", CompleteEvaluator.numPRGEnames);
    logger.info("Num. PRGE mapped: {}", CompleteEvaluator.numPRGEmapped);
    logger.info("Ratio: {}%", ((double)CompleteEvaluator.numPRGEmapped/(double)CompleteEvaluator.numPRGEnames)*100);
            
    logger.info("");
    logger.info("");
    logger.info("Num. Cell UMLS IDs: {}", CompleteEvaluator.numUMLSCL);
    logger.info("Num. Cell UMLS IDs mapped: {}", CompleteEvaluator.numUMLSCLmapped);
    logger.info("Ratio: {}%", ((double)CompleteEvaluator.numUMLSCLmapped/(double)CompleteEvaluator.numUMLSCL)*100);
    logger.info("");
    logger.info("Num. Cell concept names: {}", CompleteEvaluator.numCellNames);
    logger.info("Num. Cell concept names mapped: {}", CompleteEvaluator.numCellMapped);
    logger.info("Ratio: {}%", ((double)CompleteEvaluator.numCellMapped/(double)CompleteEvaluator.numCellNames)*100);
            
    logger.info("");
    logger.info("");
    logger.info("Total PROC_FUNC: {}", CompleteEvaluator.numPROCFUNC);
    logger.info("Total PROC_FUNC with IDs: {}", CompleteEvaluator.numPROCFUNCwithIDs);
    logger.info("Ratio: {}%", ((double)CompleteEvaluator.numPROCFUNCwithIDs/(double)CompleteEvaluator.numPROCFUNC)*100);
    logger.info("");
    logger.info("Num. PROC_FUNC UMLS IDs: {}", CompleteEvaluator.numUMLSPROCFUNC);
    logger.info("Num. PROC_FUNC UMLS IDs mapped: {}", CompleteEvaluator.numUMLSPROCFUNCmapped);
    logger.info("Ratio: {}%", ((double)CompleteEvaluator.numUMLSPROCFUNCmapped/(double)CompleteEvaluator.numUMLSPROCFUNC)*100);
    logger.info("");
    logger.info("Num. Cell concept names: {}", CompleteEvaluator.numPROCFUNCNames);
    logger.info("Num. Cell concept names mapped: {}", CompleteEvaluator.numPROCFUNCMapped);
    logger.info("Ratio: {}%", ((double)CompleteEvaluator.numPROCFUNCMapped/(double)CompleteEvaluator.numPROCFUNCNames)*100);
    */
}

From source file:pt.ua.tm.neji.train.cli.TrainMain.java

public static void main(String[] args) {

    CommandLineParser parser = new GnuParser();
    Options options = new Options();
    options.addOption("h", "help", false, "Print this usage information.");

    options.addOption("c", "sentences-input", true, "File with corpus sentences.");
    options.addOption("a", "annotations-input", true, "File with corpus annotations.");
    options.addOption("if", "input-format", true, "BC2, A1, JNLPBA or SERIALIZED.");
    options.addOption("f", "features", true, "Features.");
    options.addOption("o", "model output path", true, "Path to save the model.");
    options.addOption("m", "model name", true, "Name of the model.");
    options.addOption("d", "dictionaires", true, "Folder that contains the dictionaries.");
    options.addOption("t", "threads", true, "Number of threads. By default, is 1.");
    options.addOption("s", "serialize", true, "File to save the serialized corpus.");

    CommandLine commandLine = null;
    try {/*from ww w .ja  v a 2 s.  c om*/
        // Parse the program arguments
        commandLine = parser.parse(options, args);
    } catch (ParseException ex) {
        logger.error("There was a problem processing the input arguments.", ex);
        return;
    }

    // Show help text
    if (commandLine.hasOption('h')) {
        printHelp(options, "");
        return;
    }

    // No options
    if (commandLine.getOptions().length == 0) {
        printHelp(options, "");
        return;
    }

    String fileSentencesIn = null;
    InputFormat inputFormat;
    String fileAnnotationsIn = null;

    // Get Input format        
    if (commandLine.hasOption("if")) {
        inputFormat = InputFormat.valueOf(commandLine.getOptionValue("if"));
    } else {
        printHelp(options, "Please specify the input format.");
        return;
    }

    // Get sentences file or folder for input        
    if (commandLine.hasOption('c')) {

        fileSentencesIn = commandLine.getOptionValue('c');
        File test = new File(fileSentencesIn);

        if (inputFormat.equals(InputFormat.BC2) || inputFormat.equals(InputFormat.SERIALIZED)
                || inputFormat.equals(InputFormat.JNLPBA)) { // File formats or SERIALIZED format            

            if (!test.isFile() || !test.canRead()) {
                logger.error("The specified path is not a file or is not readable.");
                return;
            }

        } else { // Folder formats

            if (!test.isDirectory() || !test.canRead()) {
                logger.error("The specified path is not a folder or is not readable.");
                return;
            }

            // Verify if corpus is not empty
            A1Pairs aiPairs = A1Utils.separateTextAnnotations(test.listFiles());
            if (aiPairs.getAnnotations().length == 0) {
                String m = "The provided sentences directory does not " + "contain annotations files.";
                logger.error(m);
                return;
            }
        }

        fileSentencesIn = test.getAbsolutePath();
        fileSentencesIn += File.separator;

    } else {
        printHelp(options, "Please specify the senteces file or folder.");
        return;
    }

    // Get annotations file for input
    if (inputFormat.equals(InputFormat.BC2) && commandLine.hasOption('a')) {
        fileAnnotationsIn = commandLine.getOptionValue('a');
        File test = new File(fileAnnotationsIn);
        if (test.isDirectory() || !test.canRead()) {
            logger.error("The specified path is not a file or is not readable.");
            return;
        }
        fileAnnotationsIn = test.getAbsolutePath();
        fileAnnotationsIn += File.separator;
    }

    // Get model output path (path where model should be saved)
    String modelOutputPath;
    if (commandLine.hasOption("o")) {
        modelOutputPath = commandLine.getOptionValue('o');
        File test = new File(modelOutputPath);
        if (test.isFile() || !test.canRead()) {
            logger.error("The specified path is not a folder or is not readable.");
            return;
        }
        modelOutputPath = test.getAbsolutePath();
        modelOutputPath += File.separator;
    } else {
        printHelp(options, "Please specify the model output path.");
        return;
    }

    // Get model name (name for the model)
    String modelName;
    if (commandLine.hasOption('m')) {
        modelName = commandLine.getOptionValue('m');
    } else {
        printHelp(options, "Please specify the model name.");
        return;
    }
    if (modelName.contains(".gz"))
        modelName = modelName.substring(0, modelName.lastIndexOf(".gz"));
    String modelFolderPath = modelOutputPath + modelName + File.separator;

    // Get features
    String featuresFilePath;
    if (commandLine.hasOption('f')) {
        featuresFilePath = commandLine.getOptionValue('f');

        File test = new File(featuresFilePath);
        if (test.isDirectory() || !test.canRead()) {
            logger.error("The specified features file path is not a file or is not readable.");
            return;
        }
        featuresFilePath = test.getAbsolutePath();
        featuresFilePath += File.separator;

    } else {
        printHelp(options, "Please specify the model configuration file.");
        return;
    }

    // Read features file (contains parsing and entity)
    logger.info("Loading config...");
    ModelConfig config = new ModelConfig(featuresFilePath);

    // Get dictionaries folder
    String dictionariesFolder = null;
    if (commandLine.hasOption('d')) {
        dictionariesFolder = commandLine.getOptionValue('d');

        File test = new File(dictionariesFolder);
        if (!test.isDirectory() || !test.canRead()) {
            logger.error("The specified dictionaries path is not a folder or is not readable.");
            return;
        }

        dictionariesFolder = test.getAbsolutePath();
        dictionariesFolder += File.separator;
    }

    // Get threads
    int numThreads = 1;
    if (commandLine.hasOption('t')) {
        String threadsText = commandLine.getOptionValue('t');
        numThreads = Integer.parseInt(threadsText);
        if (numThreads <= 0 || numThreads > 32) {
            logger.error("Illegal number of threads. Must be between 1 and 32.");
            return;
        }
    }

    // Get serialization info (file to save the serialized corpus)
    String serializedFilePath = null;
    if (commandLine.hasOption('s')) {
        serializedFilePath = commandLine.getOptionValue('s');

        File test = (new File(serializedFilePath)).getParentFile();
        if (test != null) {
            if (test.isFile() || !test.canRead()) {
                logger.error("The specified serialize file path is not a folder or is not readable.");
                return;
            }
            serializedFilePath = test.getAbsolutePath() + File.separator
                    + (new File(serializedFilePath)).getName();
        }
        if (!serializedFilePath.endsWith(".gz"))
            serializedFilePath += ".gz";
    }

    // Parsing variables
    ParserTool parsingTool = ParserTool.GDEP;
    ParserLanguage parsingLanguage = ParserLanguage.ENGLISH;
    ParserLevel parsingLevel;

    // Get parsing
    if (config.isNLP())
        parsingLevel = ParserLevel.DEPENDENCY;
    else if (config.isChunk())
        parsingLevel = ParserLevel.CHUNKING;
    else if (config.isPos())
        parsingLevel = ParserLevel.POS;
    else if (config.isLemma())
        parsingLevel = ParserLevel.LEMMATIZATION;
    else
        parsingLevel = ParserLevel.TOKENIZATION;

    if (!inputFormat.equals(InputFormat.SERIALIZED)) {

        // -- Phase 1 (First pipeline) --

        // Set output formats
        List<OutputFormat> outputFormats = new ArrayList<>();
        outputFormats.add(OutputFormat.GZCORPUS);

        // Define context configuration
        ContextConfiguration descriptor = null;
        try {
            descriptor = new ContextConfiguration.Builder().withInputFormat(inputFormat)
                    .withOutputFormats(outputFormats).withParserTool(parsingTool)
                    .withParserLanguage(parsingLanguage).withParserLevel(parsingLevel).trainBuildPhase1();

        } catch (NejiException ex) {
            ex.printStackTrace();
            System.exit(1);
        }

        // Create context
        TrainContext context = new TrainContext(descriptor, config, dictionariesFolder, modelFolderPath, null,
                1);

        try {
            BatchExecutor batchExecutor = new TrainBatchExecutor(fileSentencesIn, fileAnnotationsIn,
                    numThreads);
            batchExecutor.run(TrainProcessor.class, context);
        } catch (Exception ex) {
            logger.error("There was a problem running the batch.", ex);
        }

        // -- Phase 2 (Merge corpus) --
        if (inputFormat.equals(InputFormat.A1)) { // Folder formats

            logger.info("Merging corpus...");

            try {
                Utils.mergeGZCorpus(modelFolderPath, modelFolderPath + File.separator + modelName + ".gz");
            } catch (NejiException ex) {
                System.out.println("Error: An error ocurred while merging the corpus. " + ex.getMessage());
            }
        }
    }

    // -- Phase 3 (Second pipeline) --

    // Set serialized corpus path
    String corpusLocationPath = null;
    if (inputFormat.equals(InputFormat.A1)) { // Folder formats
        corpusLocationPath = modelFolderPath + File.separator + modelName + ".gz";
    } else if (inputFormat.equals(InputFormat.BC2) || inputFormat.equals(InputFormat.JNLPBA)) { // File formats
        corpusLocationPath = modelFolderPath + File.separator
                + FilenameUtils.getBaseName((new File(fileSentencesIn)).getName()) + ".gz";
    } else { // Serialized format
        corpusLocationPath = fileSentencesIn;
    }

    // Save serialized corpus
    if (serializedFilePath != null) {
        logger.info("Saving serialized corpus...");
        File gzCorpusFile = new File(corpusLocationPath);
        File serializeFile = new File(serializedFilePath);

        try {
            Files.copy(gzCorpusFile, serializeFile);
        } catch (IOException ex) {
            System.out.println("Error: There was a problem saving the serialized corpus. " + ex.getMessage());
        }
    }

    // Set output formats
    List<OutputFormat> outputFormats = new ArrayList<>();
    outputFormats.add(OutputFormat.MODEL);

    // Define context configuration
    ContextConfiguration descriptor = null;
    try {
        descriptor = new ContextConfiguration.Builder().withInputFormat(InputFormat.SERIALIZED)
                .withOutputFormats(outputFormats).trainBuildPhase2();

    } catch (NejiException ex) {
        ex.printStackTrace();
        System.exit(1);
    }

    // Create context
    TrainContext context = new TrainContext(descriptor, config, dictionariesFolder, corpusLocationPath,
            modelFolderPath, 2);

    try {
        BatchExecutor batchExecutor = new TrainBatchExecutor();
        batchExecutor.run(TrainProcessor.class, context);
    } catch (Exception ex) {
        logger.error("There was a problem running the batch.", ex);
    }

    // Delete corpus file
    if (!inputFormat.equals(InputFormat.SERIALIZED)) {
        (new File(corpusLocationPath)).delete();
    }

    // Delete tmp file (if it exists)
    (new File("tmp.txt")).delete();

}

From source file:pw.phylame.gaf.cli.CApplication.java

private void parseOptions() {
    CommandLine cmd = null;
    try {// w ww  . j  a  v a 2 s.  c o  m
        cmd = getCommandLineParser().parse(options, getArguments());
    } catch (ParseException ex) {
        onOptionError(ex);
        exit(-1);
    }
    assert cmd != null;
    for (Option option : cmd.getOptions()) {
        CAction action = actions.get(option);
        if (action instanceof CInitializer) {
            ((CInitializer) action).perform(this, cmd);
        } else {
            commands.add((CCommand) action);

        }
    }
    inputs = cmd.getArgs();
}

From source file:ro.cs.products.Executor.java

private static void printCommandLine(CommandLine cmd) {
    Logger.getRootLogger().debug("Executing with the following arguments:");
    for (Option option : cmd.getOptions()) {
        if (option.hasArgs()) {
            Logger.getRootLogger().debug(option.getOpt() + "=" + String.join(" ", option.getValues()));
        } else if (option.hasArg()) {
            Logger.getRootLogger().debug(option.getOpt() + "=" + option.getValue());
        } else {//from  w  w w  . jav a 2  s.  c  o  m
            Logger.getRootLogger().debug(option.getOpt());
        }
    }
}

From source file:ru.objective.jni.tasks.TaskManager.java

public void run(CommandLine cmd) throws Exception {
    String[] classPaths = cmd.getOptionValues("classpath");
    String[] excludes = cmd.getOptionValues("exclude");
    String[] excludesPackages = cmd.getOptionValues("excludepackage");
    String[] classes = cmd.getOptionValues("class");
    String[] packages = cmd.getOptionValues("package");

    String output = cmd.getOptionValue("output", ".");
    String prefix = cmd.getOptionValue("prefix", "");

    ITask task = null;/*from  w ww.j a v a  2 s.c  o m*/

    if (cmd.getOptions().length == 0 || cmd.hasOption("help")) {
        task = new HelpTask();
    } else if (cmd.hasOption("version")) {
        task = new VersionTask();
    } else {
        OJNIClassLoader.getInstance().setClassPaths(Utils.classPathsFromStrings(classPaths));

        task = new DefaultTask(classPaths, excludes, excludesPackages, classes, packages, output, prefix);
    }

    task.run();
}

From source file:salomon.engine.Starter.java

private void parseOptions(String[] args) throws ParseException {
    BasicParser parser = new BasicParser();
    CommandLine cmdLine = parser.parse(_options, args);

    // if any options provided process them
    // otherwise run application in standard way
    if (cmdLine.getArgs().length > 0 || cmdLine.getOptions().length > 0) {
        if (cmdLine.hasOption("h")) {
            usage();/*  w  w  w.ja v a  2  s. c  om*/
        } else if (cmdLine.hasOption("f")) {
            try {
                executeBatch(cmdLine.getOptionValue("f"));
            } catch (PlatformException e) {
                LOGGER.fatal(e.getLocalizedMessage());
            }
        } else {
            usage();
        }
    } else {
        LOGGER.info("### Application started ###");
        startLocal();
    }
}

From source file:sinlin.Main.java

public static void main(String[] args) {
    boolean toOutStream = false;//use System.out if true and file otherwise
    boolean silent = false;//no times out
    long t = System.currentTimeMillis();
    BufferedReader bufferedReader;
    String line;//from ww w. j  ava  2 s .c  o  m
    StringFacadeIF stringFacadeIF;
    TagHandler tagHandler = new TagHandler();
    Exporter exporter = new Exporter();

    String[] d;

    String versionWithLicense = version + "\n"
            + "License GPLv3: GNU GPL version 3 <http://gnu.org/licenses/gpl.html>.\n"
            + "This is free software: you are free to change and redistribute it.\n"
            + "There is NO WARRANTY, to the extent permitted by law.\n\n" + "Written by Artur Stepankevich.";

    String help = "SYNOPSIS\n\n" + "java -jar sinlin.jar [options].\n\n" + "OPTIONS\n" + "\n"
            + "-i file\n\tPath to input file (svg or xml).\n\n" + "-d file\n\tPath to data file (ods).\n\n"
            + "-o file\n\tPath and begin of name of output file." + "\n\t\"_out\" will add to end of file"
            + "\n\tOutput file extension will the same, that root tag in source file."
            + "\n\tIf this option is absent, then input file path "
            + "\n\tand data filename (if present) and _out is used.\n\n" + "-p\n"
            + "--print\n\tExport to System.out. Option -o ignored\n" + "\tTimes not prints.\n\n" + "-m number\n"
            + "\tExport only [number] of first exemplars for each tag if one has more.\n\n" + "-g\t"
            + "\n\tGenerate data sequence from string." + "\n\tSymbol \"$\" must be replaced by \"\\$\"."
            + "\n\tWith option -d can get data from ods files.\n\n" + "-r"
            + "\n\tPrint names of all cell ranges in ods file, defined with option -d.\n\n" + "-V"
            + "\n\tPrint version.\n\n" + "-h" + "\n\tPrint help.\n\n" + "-b"
            + "\n\tDebug. Exports also sinlin attributes (exist, onenode, etc).\n\n" + "BIGGEST KNOWN BUGS\n\n"
            + //program has littler bugs
            "1.Error when get range with one cell from .ods files.\n"
            + "\nSee code at <https://github.com/step-artur87/sinlin> (Java, GPLv3),\n"
            + "wiki at <https://github.com/step-artur87/sinlin/wiki> \n"
            + "and binaries at <http://sourceforge.net/projects/sinlin/>.\n"
            + "Twitter <https://twitter.com/sinlinSVG>.";

    CommandLine commandLine;

    String prefix;

    Data data;

    //define options
    Options options = new Options();
    options.addOption("i", true, "input");
    options.addOption("d", true, "data");
    options.addOption("o", true, "output");
    options.addOption("V", false, "version");
    options.addOption("h", false, "help");
    options.addOption("g", true, "generate");
    options.addOption("r", false, "ranges");
    options.addOption("m", true, "limit");
    options.addOption("b", false, "debug");
    options.addOption("p", "print", false, "print");

    //there order of ifs is significant
    try {
        commandLine = (new DefaultParser()).parse(options, args);

        //if no argument, print help and exit
        if (commandLine.getOptions().length == 0) {
            System.out.println(help);
            System.exit(0);
        }

        //print version and exit
        if (commandLine.hasOption("V")) {
            System.out.println(versionWithLicense);
            System.exit(0);
        }

        //print help and exit
        if (commandLine.hasOption("h")) {
            System.out.println(help);
            System.exit(0);
        }

        //set output stream to System.out
        if (commandLine.hasOption("p")) {
            toOutStream = true;
            silent = true;
        }

        //if defined data file, load it
        if (commandLine.hasOption("d")) {
            if (!silent)
                System.out.println(
                        "Before data load time = " + ((System.currentTimeMillis() - t)) / 1000. + " s");
            data = new OdfData(commandLine.getOptionValue("d"));
            Fn.setData(data);
            if (!silent)
                System.out
                        .println("After data load time = " + ((System.currentTimeMillis() - t)) / 1000. + " s");
            if (commandLine.hasOption("r")) {
                ((OdfData) data).printRanges();
                System.exit(0);
            }
        }

        //if generate mode, print needed and exit
        if (commandLine.hasOption("g")) {
            stringFacadeIF = StringFacadeBuilder.create(commandLine.getOptionValue("g"));
            for (int i = 0; i < stringFacadeIF.getSize(); i++) {
                //no concat all with "\n" because Windows EOL diffs
                System.out.println(stringFacadeIF.getValue(null, i));
            }
            System.exit(0);
        }

        //if normal mode, do needed
        if (commandLine.hasOption("i")) {

            //if debug, set it
            if (commandLine.hasOption("b")) {
                Tag.setDebug(true);
            }

            //parse input file
            if (!silent)
                System.out
                        .println("Before parsing time = " + ((System.currentTimeMillis() - t)) / 1000. + " s");
            SaxParsing.parse(tagHandler, commandLine.getOptionValue("i"));
            if (!silent)
                System.out.println("After parsing time = " + ((System.currentTimeMillis() - t)) / 1000. + " s");

            //define prefix
            if (commandLine.hasOption("o")) {
                //!!if no _out added and no filename (only path) - not works
                prefix = commandLine.getOptionValue("o") + "_out";
            } else {
                if (commandLine.hasOption("d")) {
                    d = commandLine.getOptionValue("d").split("/");
                    prefix = commandLine.getOptionValue("i") + "__" + d[d.length - 1] + "__" + "out";
                } else {
                    prefix = commandLine.getOptionValue("i") + "__" + "out";
                }
            }

            //if defined max export exemplar count , set it
            if (commandLine.hasOption("m")) {
                exporter.setLimit(Integer.parseInt(commandLine.getOptionValue("m")));
            }

            //export and exit
            if (!silent)
                System.out.println("Before export time = " + ((System.currentTimeMillis() - t)) / 1000. + " s");
            Tag r = tagHandler.getRootTag();
            exporter.writeAllXml(r, prefix, toOutStream);
            if (!silent)
                System.out.println("After export time  = " + ((System.currentTimeMillis() - t)) / 1000. + " s");
            System.exit(0);
        }

        //if undefined argument combination print help (end of main)
        System.out.println(help);

        //print only exception name, not stackTrace (if these exception)
    } catch (ParseException | NumberFormatException e) {
        Util.handleException(e);
    }
}

From source file:texnlp.apps.CatCombine.java

public static void main(String[] args) {

    CommandLineParser optparse = new PosixParser();

    Options options = new Options();
    options.addOption("s", "tagset", true, "the file containing the categories");
    options.addOption("o", "output", true, "the file containing the categories");

    try {//  ww  w. j a v  a2  s  .c om
        CommandLine cline = optparse.parse(options, args);

        String tagsetFile = "";
        String outputType = "boolean";
        for (Option option : cline.getOptions()) {
            String value = option.getValue();
            switch (option.getOpt().charAt(0)) {
            case 's':
                tagsetFile = value;
                break;
            case 'o':
                outputType = value;
                break;
            }
        }

        TObjectIntHashMap<String> states = new TObjectIntHashMap<String>();
        String[] stateNames;
        int numStates = 0;

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(tagsetFile)));

        int stateID = 0;
        String line = null;
        while ((line = br.readLine()) != null)
            states.put(line.trim(), stateID++);

        states.trimToSize();

        numStates = stateID;
        stateNames = new String[numStates];
        for (TObjectIntIterator<String> it = states.iterator(); it.hasNext();) {
            it.advance();
            stateNames[it.value()] = it.key();
        }

        int numTags = numStates;
        CategoryParser catp = new CategoryParser();
        Cat[] cats = new Cat[numTags];

        for (int i = 0; i < numTags; i++) {
            String catString = Cat.changeNonCats(stateNames[i]);
            // String catString = stateNames[i];
            Cat c = catp.parse(catString);
            cats[i] = c;
        }

        TransitionPrior tprior = new TransitionPrior(stateNames, states);

        for (int i = 0; i < numTags; i++) {
            for (int j = 0; j < numTags; j++) {
                String outputVal = "0";
                if (outputType.equals("weighted"))
                    outputVal = "1";

                if (Rules.canCombine(cats[i], cats[j])) {
                    outputVal = "1";
                    if (outputType.equals("weighted"))
                        outputVal = "10";
                }
                if (outputType.equals("prior"))
                    outputVal = Double.toString(tprior.tt[i][j]);

                System.out.println(outputVal + " " + stateNames[i] + " " + stateNames[j]);
            }

            String outputForEOS = "0";
            if (outputType.equals("weighted"))
                outputForEOS = "1";

            if (cats[i].notSeekingDir(Slash.R)) {
                outputForEOS = "1";
                if (outputType.equals("weighted"))
                    outputForEOS = "10";
            }

            if (outputType.equals("prior"))
                outputForEOS = Double.toString(tprior.tFinal[i]);

            System.out.println(outputForEOS + " " + stateNames[i] + " EOS");

        }

        for (int j = 0; j < numTags; j++) {
            String outputForBOS = "0";
            if (outputType.equals("weighted"))
                outputForBOS = "1";

            if (cats[j].notSeekingDir(Slash.L)) {
                outputForBOS = "1";
                if (outputType.equals("weighted"))
                    outputForBOS = "10";
            }

            if (outputType.equals("prior"))
                outputForBOS = Double.toString(tprior.tInitial[j]);

            System.out.println(outputForBOS + " BOS " + stateNames[j]);
        }

    } catch (ParseException exp) {
        System.out.println("Unexpected exception parsing command line options:" + exp.getMessage());
    } catch (IOException exp) {
        System.out.println("IOException:" + exp.getMessage());
        System.exit(0);
    }

}

From source file:texnlp.util.TaggerOptions.java

public TaggerOptions(CommandLine cline) throws IOException {

    boolean outputDirSet = false;
    for (Option option : cline.getOptions()) {
        String value = option.getValue();
        switch (option.getOpt().charAt(0)) {
        case 'a':
            setNumMachines(Integer.parseInt(value));
            break;
        case 'b':
            setBeta(Double.parseDouble(value));
            break;
        case 'c':
            setCutoffTD(Double.parseDouble(value));
            break;
        case 'd':
            setDevFile(value);/*from  w w  w  .  j  a v  a 2  s.  c o m*/
            break;
        case 'e':
            setEvalFile(value);
            break;
        case 'f':
            setFormat(value);
            break;
        case 'g':
            setTagdictTraining(true);
            break;
        case 'i':
            setNumIterations(Integer.parseInt(value));
            break;
        case 'j':
            setDirichletTransition(true);
            break;
        case 'k':
            setDirichletEmission(true);
            break;
        case 'l':
            setMachineFile(value);
            break;
        case 'm':
            setModelType(value);
            break;
        case 'n':
            setMultitag(true);
            break;
        case 'o':
            setOutputDir(value);
            outputDirSet = true;
            break;
        case 'p':
            setPriorAmount(Double.parseDouble(value));
            break;
        case 'q':
            setLambda(Double.parseDouble(value));
            break;
        case 'r':
            setRawFile(value);
            break;
        case 's':
            setTagsetFile(value);
            break;
        case 't':
            setTaggedFile(value);
            break;
        case 'u':
            setUnconstrainedByTagdict(true);
            break;
        case 'v':
            setValidTagsForUnknownsMinCount(Integer.parseInt(value));
            break;
        case 'w':
            setContextGen(value);
            break;
        case 'z':
            setTolerance(Double.parseDouble(value));
            break;
        }
    }

    if (!outputDirSet)
        createOutputDir();
}

From source file:tikka.bhmm.apps.CommandLineOptions.java

/**
 *
 * @param cline//from w  ww.j av  a  2s  .co  m
 * @throws IOException
 */
public CommandLineOptions(CommandLine cline) throws IOException {

    String opt = null;

    for (Option option : cline.getOptions()) {
        String value = option.getValue();
        switch (option.getOpt().charAt(0)) {
        case 'a':
            alpha = Double.parseDouble(value);
            break;
        case 'b':
            beta = Double.parseDouble(value);
            break;
        case 'c':
            if (value.equals("conll2k")) {
                dataFormat = DataFormatEnum.DataFormat.CONLL2K;
            } else if (value.equals("hashslash")) {
                dataFormat = DataFormatEnum.DataFormat.HASHSLASH;
            } else if (value.equals("pipesep")) {
                dataFormat = DataFormatEnum.DataFormat.PIPESEP;
            } else if (value.equals("raw")) {
                dataFormat = DataFormatEnum.DataFormat.RAW;
            } else {
                System.err.println("\"" + value + "\" is an unknown data format option.");
                System.exit(1);
            }
            break;
        case 'd':
            if (value.endsWith("" + File.separator)) {
                trainDataDir = value.substring(0, value.length() - 1);
            } else {
                trainDataDir = value;
            }
            break;
        case 'e':
            experimentModel = value;
            break;
        case 'f':
            if (value.endsWith("" + File.separator)) {
                testDataDir = value.substring(0, value.length() - 1);
            } else {
                testDataDir = value;
            }
            break;
        case 'g':
            gamma = Double.parseDouble(value);
            break;
        case 'i':
            opt = option.getOpt();
            if (opt.equals("itr")) {
                numIterations = Integer.parseInt(value);
            } else if (opt.equals("ite")) {
                testSetBurninIterations = Integer.parseInt(value);
            }
            break;
        case 'j':
            if (value.endsWith("" + File.separator)) {
                annotatedTestTextOutDir = value.substring(0, value.length() - 1);
            } else {
                annotatedTestTextOutDir = value;
            }
            break;
        case 'k':
            opt = option.getOpt();
            if (opt.equals("ks")) {
                samples = Integer.parseInt(value);
            } else if (opt.equals("kl")) {
                lag = Integer.parseInt(value);
            }
            break;
        case 'l':
            modelInputPath = value;
            break;
        case 'm':
            modelOutputPath = value;
            break;
        case 'n':
            if (value.endsWith("" + File.separator)) {
                annotatedTrainTextOutDir = value.substring(0, value.length() - 1);
            } else {
                annotatedTrainTextOutDir = value;
            }
            break;
        case 'o':
            opt = option.getOpt();
            if (opt.equals("ot")) {
                tabularOutputFilename = value;
                tabulatedOutput = new BufferedWriter(
                        new OutputStreamWriter(new FileOutputStream(tabularOutputFilename)));
            } else if (opt.equals("oste")) {
                testEvaluationOutputFilename = value;
                testEvaluationOutput = new BufferedWriter(
                        new OutputStreamWriter(new FileOutputStream(testEvaluationOutputFilename)));
            } else if (opt.equals("ostr")) {
                trainDataSampleScoreOutputFilename = value;
                trainDataSampleScoreOutput = new BufferedWriter(
                        new OutputStreamWriter(new FileOutputStream(trainDataSampleScoreOutputFilename)));
            } else if (opt.equals("oe")) {
                evaluationOutputFilename = value;
                evaluationOutput = new BufferedWriter(
                        new OutputStreamWriter(new FileOutputStream(evaluationOutputFilename)));
            }
            break;
        case 'p':
            opt = option.getOpt();
            if (opt.equals("pi")) {
                initialTemperature = Double.parseDouble(value);
            } else if (opt.equals("pd")) {
                temperatureDecrement = Double.parseDouble(value);
            } else if (opt.equals("pt")) {
                targetTemperature = Double.parseDouble(value);
            }
            break;
        case 'q':
            delta = Double.parseDouble(value);
            break;
        case 'r':
            if (value.equals("0")) {
                randomSeed = -1;
            } else {
                randomSeed = Integer.valueOf(value);
            }
            break;
        case 's':
            opt = option.getOpt();
            if (opt.equals("sc")) {
                contentstates = Integer.parseInt(value);
            } else if (opt.equals("sf")) {
                functionstates = Integer.parseInt(value);
            }
            break;
        case 't':
            topics = Integer.parseInt(value);
            break;
        case 'u':
            opt = option.getOpt();
            if (opt.equals("ut")) {
                if (value.equals("b")) {
                    tagSet = TagSetEnum.TagSet.BROWN;
                } else if (value.equals("p")) {
                    tagSet = TagSetEnum.TagSet.PTB;
                } else if (value.equals("t")) {
                    tagSet = TagSetEnum.TagSet.TIGER;
                } else if (value.equals("u")) {
                    tagSet = TagSetEnum.TagSet.USP;
                } else if (value.equals("f")) {
                    tagSet = TagSetEnum.TagSet.FLORESTA;
                } else if (value.equals("n")) {
                    tagSet = TagSetEnum.TagSet.NONE;
                }
            } else if (opt.equals("ur")) {
                int val = Integer.parseInt(value);
                if (val == 0) {
                    reductionLevel = TagSetEnum.ReductionLevel.FULL;
                } else {
                    reductionLevel = TagSetEnum.ReductionLevel.REDUCED;
                }
            }
            break;
        case 'w':
            outputPerClass = Integer.parseInt(value);
            break;
        case 'x':
            opt = option.getOpt();
            if (opt.equals("xd")) {
                posDictPath = value;
            } else if (opt.equals("xt")) {
                useTrigram = true;
            } else if (opt.equals("xthread")) {
                numThread = Integer.parseInt(value);
            } else if (opt.equals("xc")) {
                chunks = Integer.parseInt(value);
            } else if (opt.equals("xp")) {
                phash = Double.parseDouble(value);
            }
            break;
        }
    }
}