List of usage examples for org.apache.commons.cli2 CommandLine getValue
Object getValue(final Option option) throws IllegalStateException;
From source file:org.apache.mahout.df.mapreduce.BuildForest.java
@Override public int run(String[] args) throws IOException, ClassNotFoundException, InterruptedException { DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); ArgumentBuilder abuilder = new ArgumentBuilder(); GroupBuilder gbuilder = new GroupBuilder(); Option oobOpt = obuilder.withShortName("oob").withRequired(false) .withDescription("Optional, estimate the out-of-bag error").create(); Option dataOpt = obuilder.withLongName("data").withShortName("d").withRequired(true) .withArgument(abuilder.withName("path").withMinimum(1).withMaximum(1).create()) .withDescription("Data path").create(); Option datasetOpt = obuilder.withLongName("dataset").withShortName("ds").withRequired(true) .withArgument(abuilder.withName("dataset").withMinimum(1).withMaximum(1).create()) .withDescription("Dataset path").create(); Option selectionOpt = obuilder.withLongName("selection").withShortName("sl").withRequired(true) .withArgument(abuilder.withName("m").withMinimum(1).withMaximum(1).create()) .withDescription("Number of variables to select randomly at each tree-node").create(); Option seedOpt = obuilder.withLongName("seed").withShortName("sd").withRequired(false) .withArgument(abuilder.withName("seed").withMinimum(1).withMaximum(1).create()) .withDescription("Optional, seed value used to initialise the Random number generator").create(); Option partialOpt = obuilder.withLongName("partial").withShortName("p").withRequired(false) .withDescription("Optional, use the Partial Data implementation").create(); Option nbtreesOpt = obuilder.withLongName("nbtrees").withShortName("t").withRequired(true) .withArgument(abuilder.withName("nbtrees").withMinimum(1).withMaximum(1).create()) .withDescription("Number of trees to grow").create(); Option outputOpt = obuilder.withLongName("output").withShortName("o").withRequired(true) .withArgument(abuilder.withName("path").withMinimum(1).withMaximum(1).create()) .withDescription("Output path, will contain the Decision Forest").create(); Option helpOpt = obuilder.withLongName("help").withDescription("Print out help").withShortName("h") .create();// w w w. j av a 2s . co m Group group = gbuilder.withName("Options").withOption(oobOpt).withOption(dataOpt).withOption(datasetOpt) .withOption(selectionOpt).withOption(seedOpt).withOption(partialOpt).withOption(nbtreesOpt) .withOption(outputOpt).withOption(helpOpt).create(); try { Parser parser = new Parser(); parser.setGroup(group); CommandLine cmdLine = parser.parse(args); if (cmdLine.hasOption("help")) { CommandLineUtil.printHelp(group); return -1; } isPartial = cmdLine.hasOption(partialOpt); isOob = cmdLine.hasOption(oobOpt); String dataName = cmdLine.getValue(dataOpt).toString(); String datasetName = cmdLine.getValue(datasetOpt).toString(); String outputName = cmdLine.getValue(outputOpt).toString(); m = Integer.parseInt(cmdLine.getValue(selectionOpt).toString()); nbTrees = Integer.parseInt(cmdLine.getValue(nbtreesOpt).toString()); if (cmdLine.hasOption(seedOpt)) { seed = Long.valueOf(cmdLine.getValue(seedOpt).toString()); } log.debug("data : {}", dataName); log.debug("dataset : {}", datasetName); log.debug("output : {}", outputName); log.debug("m : {}", m); log.debug("seed : {}", seed); log.debug("nbtrees : {}", nbTrees); log.debug("isPartial : {}", isPartial); log.debug("isOob : {}", isOob); dataPath = new Path(dataName); datasetPath = new Path(datasetName); outputPath = new Path(outputName); } catch (OptionException e) { log.error("Exception", e); CommandLineUtil.printHelp(group); return -1; } buildForest(); return 0; }
From source file:org.apache.mahout.df.mapreduce.TestForest.java
@Override public int run(String[] args) throws IOException, ClassNotFoundException, InterruptedException { DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); ArgumentBuilder abuilder = new ArgumentBuilder(); GroupBuilder gbuilder = new GroupBuilder(); Option inputOpt = obuilder.withLongName("input").withShortName("i").withRequired(true) .withArgument(abuilder.withName("path").withMinimum(1).withMaximum(1).create()) .withDescription("Test data path").create(); Option datasetOpt = obuilder.withLongName("dataset").withShortName("ds").withRequired(true) .withArgument(abuilder.withName("dataset").withMinimum(1).withMaximum(1).create()) .withDescription("Dataset path").create(); Option modelOpt = obuilder.withLongName("model").withShortName("m").withRequired(true) .withArgument(abuilder.withName("path").withMinimum(1).withMaximum(1).create()) .withDescription("Path to the Decision Forest").create(); Option outputOpt = obuilder.withLongName("output").withShortName("o").withRequired(false) .withArgument(abuilder.withName("output").withMinimum(1).withMaximum(1).create()) .withDescription("Path to generated predictions file").create(); Option analyzeOpt = obuilder.withLongName("analyze").withShortName("a").withRequired(false).create(); Option mrOpt = obuilder.withLongName("mapreduce").withShortName("mr").withRequired(false).create(); Option helpOpt = obuilder.withLongName("help").withDescription("Print out help").withShortName("h") .create();//from ww w .ja va 2s. com Group group = gbuilder.withName("Options").withOption(inputOpt).withOption(datasetOpt).withOption(modelOpt) .withOption(outputOpt).withOption(analyzeOpt).withOption(mrOpt).withOption(helpOpt).create(); try { Parser parser = new Parser(); parser.setGroup(group); CommandLine cmdLine = parser.parse(args); if (cmdLine.hasOption("help")) { CommandLineUtil.printHelp(group); return -1; } String dataName = cmdLine.getValue(inputOpt).toString(); String datasetName = cmdLine.getValue(datasetOpt).toString(); String modelName = cmdLine.getValue(modelOpt).toString(); String outputName = (cmdLine.hasOption(outputOpt)) ? cmdLine.getValue(outputOpt).toString() : null; analyze = cmdLine.hasOption(analyzeOpt); useMapreduce = cmdLine.hasOption(mrOpt); log.debug("inout : {}", dataName); log.debug("dataset : {}", datasetName); log.debug("model : {}", modelName); log.debug("output : {}", outputName); log.debug("analyze : {}", analyze); log.debug("mapreduce : {}", useMapreduce); dataPath = new Path(dataName); datasetPath = new Path(datasetName); modelPath = new Path(modelName); if (outputName != null) { outputPath = new Path(outputName); } } catch (OptionException e) { System.err.println("Exception : " + e); CommandLineUtil.printHelp(group); return -1; } testForest(); return 0; }
From source file:org.apache.mahout.df.tools.Describe.java
public static void main(String[] args) throws IOException, DescriptorException { DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); ArgumentBuilder abuilder = new ArgumentBuilder(); GroupBuilder gbuilder = new GroupBuilder(); Option pathOpt = obuilder.withLongName("path").withShortName("p").withRequired(true) .withArgument(abuilder.withName("path").withMinimum(1).withMaximum(1).create()) .withDescription("Data path").create(); Option descriptorOpt = obuilder.withLongName("descriptor").withShortName("d").withRequired(true) .withArgument(abuilder.withName("descriptor").withMinimum(1).create()) .withDescription("data descriptor").create(); Option descPathOpt = obuilder.withLongName("file").withShortName("f").withRequired(true) .withArgument(abuilder.withName("file").withMinimum(1).withMaximum(1).create()) .withDescription("Path to generated descriptor file").create(); Option helpOpt = obuilder.withLongName("help").withDescription("Print out help").withShortName("h") .create();//from ww w . j ava 2s . c om Group group = gbuilder.withName("Options").withOption(pathOpt).withOption(descPathOpt) .withOption(descriptorOpt).withOption(helpOpt).create(); try { Parser parser = new Parser(); parser.setGroup(group); CommandLine cmdLine = parser.parse(args); if (cmdLine.hasOption(helpOpt)) { CommandLineUtil.printHelp(group); return; } String dataPath = cmdLine.getValue(pathOpt).toString(); String descPath = cmdLine.getValue(descPathOpt).toString(); List<String> descriptor = convert(cmdLine.getValues(descriptorOpt)); log.debug("Data path : {}", dataPath); log.debug("Descriptor path : {}", descPath); log.debug("Descriptor : {}", descriptor); runTool(dataPath, descriptor, descPath); } catch (OptionException e) { log.warn(e.toString()); CommandLineUtil.printHelp(group); } }
From source file:org.apache.mahout.fpm.pfpgrowth.DeliciousTagsExample.java
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException { DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); ArgumentBuilder abuilder = new ArgumentBuilder(); GroupBuilder gbuilder = new GroupBuilder(); Option inputDirOpt = DefaultOptionCreator.inputOption().create(); Option outputOpt = DefaultOptionCreator.outputOption().create(); Option helpOpt = DefaultOptionCreator.helpOption(); Option recordSplitterOpt = obuilder.withLongName("splitterPattern") .withArgument(abuilder.withName("splitterPattern").withMinimum(1).withMaximum(1).create()) .withDescription("Regular Expression pattern used to split given line into fields." + " Default value splits comma or tab separated fields." + " Default Value: \"[ ,\\t]*\\t[ ,\\t]*\" ") .withShortName("regex").create(); Option encodingOpt = obuilder.withLongName("encoding") .withArgument(abuilder.withName("encoding").withMinimum(1).withMaximum(1).create()) .withDescription("(Optional) The file encoding. Default value: UTF-8").withShortName("e").create(); Group group = gbuilder.withName("Options").withOption(inputDirOpt).withOption(outputOpt).withOption(helpOpt) .withOption(recordSplitterOpt).withOption(encodingOpt).create(); try {/* ww w .j a v a 2 s.c o m*/ Parser parser = new Parser(); parser.setGroup(group); CommandLine cmdLine = parser.parse(args); if (cmdLine.hasOption(helpOpt)) { CommandLineUtil.printHelp(group); return; } Parameters params = new Parameters(); if (cmdLine.hasOption(recordSplitterOpt)) { params.set("splitPattern", (String) cmdLine.getValue(recordSplitterOpt)); } String encoding = "UTF-8"; if (cmdLine.hasOption(encodingOpt)) { encoding = (String) cmdLine.getValue(encodingOpt); } params.set("encoding", encoding); String inputDir = (String) cmdLine.getValue(inputDirOpt); String outputDir = (String) cmdLine.getValue(outputOpt); params.set("input", inputDir); params.set("output", outputDir); params.set("groupingFieldCount", "2"); params.set("gfield0", "1"); params.set("gfield1", "2"); params.set("selectedFieldCount", "1"); params.set("field0", "3"); params.set("maxTransactionLength", "100"); KeyBasedStringTupleGrouper.startJob(params); } catch (OptionException ex) { CommandLineUtil.printHelp(group); } }
From source file:org.apache.mahout.fpm.pfpgrowth.FPGrowthDriver.java
/** * Run TopK FPGrowth given the input file, *//* www.java 2 s .c o m*/ public static void main(String[] args) throws Exception { DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); ArgumentBuilder abuilder = new ArgumentBuilder(); GroupBuilder gbuilder = new GroupBuilder(); Option inputDirOpt = obuilder.withLongName("input").withRequired(true) .withArgument(abuilder.withName("input").withMinimum(1).withMaximum(1).create()) .withDescription("The Directory on HDFS containing the transaction files").withShortName("i") .create(); Option outputOpt = DefaultOptionCreator.outputOption().create(); Option helpOpt = DefaultOptionCreator.helpOption(); // minSupport(3), maxHeapSize(50), numGroups(1000) Option minSupportOpt = obuilder.withLongName("minSupport") .withArgument(abuilder.withName("minSupport").withMinimum(1).withMaximum(1).create()) .withDescription("(Optional) Minimum Support. Default Value: 3").withShortName("s").create(); Option maxHeapSizeOpt = obuilder.withLongName("maxHeapSize") .withArgument(abuilder.withName("maxHeapSize").withMinimum(1).withMaximum(1).create()) .withDescription( "(Optional) Maximum Heap Size k, to denote the requirement to mine top K items. Default value: 50") .withShortName("k").create(); Option numGroupsOpt = obuilder.withLongName("numGroups") .withArgument(abuilder.withName("numGroups").withMinimum(1).withMaximum(1).create()) .withDescription( "(Optional) Number of groups the features should be divided in the map-reduce version." + " Doesn't work in sequential version Default Value:1000") .withShortName("g").create(); Option recordSplitterOpt = obuilder.withLongName("splitterPattern") .withArgument(abuilder.withName("splitterPattern").withMinimum(1).withMaximum(1).create()) .withDescription("Regular Expression pattern used to split given string transaction into itemsets." + " Default value splits comma separated itemsets. Default Value:" + " \"[ ,\\t]*[,|\\t][ ,\\t]*\" ") .withShortName("regex").create(); Option treeCacheOpt = obuilder.withLongName("numTreeCacheEntries") .withArgument(abuilder.withName("numTreeCacheEntries").withMinimum(1).withMaximum(1).create()) .withDescription( "(Optional) Number of entries in the tree cache to prevent duplicate tree building. " + "(Warning) a first level conditional FP-Tree might consume a lot of memory, " + "so keep this value small, but big enough to prevent duplicate tree building. " + "Default Value:5 Recommended Values: [5-10]") .withShortName("tc").create(); Option methodOpt = obuilder.withLongName("method").withRequired(true) .withArgument(abuilder.withName("method").withMinimum(1).withMaximum(1).create()) .withDescription("Method of processing: sequential|mapreduce").withShortName("method").create(); Option encodingOpt = obuilder.withLongName("encoding") .withArgument(abuilder.withName("encoding").withMinimum(1).withMaximum(1).create()) .withDescription("(Optional) The file encoding. Default value: UTF-8").withShortName("e").create(); Group group = gbuilder.withName("Options").withOption(minSupportOpt).withOption(inputDirOpt) .withOption(outputOpt).withOption(maxHeapSizeOpt).withOption(numGroupsOpt).withOption(methodOpt) .withOption(encodingOpt).withOption(helpOpt).withOption(treeCacheOpt).withOption(recordSplitterOpt) .create(); try { Parser parser = new Parser(); parser.setGroup(group); CommandLine cmdLine = parser.parse(args); if (cmdLine.hasOption(helpOpt)) { CommandLineUtil.printHelp(group); return; } Parameters params = new Parameters(); if (cmdLine.hasOption(minSupportOpt)) { String minSupportString = (String) cmdLine.getValue(minSupportOpt); params.set("minSupport", minSupportString); } if (cmdLine.hasOption(maxHeapSizeOpt)) { String maxHeapSizeString = (String) cmdLine.getValue(maxHeapSizeOpt); params.set("maxHeapSize", maxHeapSizeString); } if (cmdLine.hasOption(numGroupsOpt)) { String numGroupsString = (String) cmdLine.getValue(numGroupsOpt); params.set("numGroups", numGroupsString); } if (cmdLine.hasOption(treeCacheOpt)) { String numTreeCacheString = (String) cmdLine.getValue(treeCacheOpt); params.set("treeCacheSize", numTreeCacheString); } if (cmdLine.hasOption(recordSplitterOpt)) { String patternString = (String) cmdLine.getValue(recordSplitterOpt); params.set("splitPattern", patternString); } String encoding = "UTF-8"; if (cmdLine.hasOption(encodingOpt)) { encoding = (String) cmdLine.getValue(encodingOpt); } params.set("encoding", encoding); Path inputDir = new Path(cmdLine.getValue(inputDirOpt).toString()); Path outputDir = new Path(cmdLine.getValue(outputOpt).toString()); params.set("input", inputDir.toString()); params.set("output", outputDir.toString()); String classificationMethod = (String) cmdLine.getValue(methodOpt); if (classificationMethod.equalsIgnoreCase("sequential")) { runFPGrowth(params); } else if (classificationMethod.equalsIgnoreCase("mapreduce")) { HadoopUtil.overwriteOutput(outputDir); PFPGrowth.runPFPGrowth(params); } } catch (OptionException e) { CommandLineUtil.printHelp(group); } }
From source file:org.apache.mahout.ga.watchmaker.cd.CDGA.java
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException { DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); ArgumentBuilder abuilder = new ArgumentBuilder(); GroupBuilder gbuilder = new GroupBuilder(); Option inputOpt = DefaultOptionCreator.inputOption().create(); Option labelOpt = obuilder.withLongName("label").withRequired(true).withShortName("l") .withArgument(abuilder.withName("index").withMinimum(1).withMaximum(1).create()) .withDescription("label's index.").create(); Option thresholdOpt = obuilder.withLongName("threshold").withRequired(false).withShortName("t") .withArgument(abuilder.withName("threshold").withMinimum(1).withMaximum(1).create()) .withDescription("Condition activation threshold, default = 0.5.").create(); Option crosspntsOpt = obuilder.withLongName("crosspnts").withRequired(false).withShortName("cp") .withArgument(abuilder.withName("points").withMinimum(1).withMaximum(1).create()) .withDescription("Number of crossover points to use, default = 1.").create(); Option mutrateOpt = obuilder.withLongName("mutrate").withRequired(true).withShortName("m") .withArgument(abuilder.withName("true").withMinimum(1).withMaximum(1).create()) .withDescription("Mutation rate (float).").create(); Option mutrangeOpt = obuilder.withLongName("mutrange").withRequired(false).withShortName("mr") .withArgument(abuilder.withName("range").withMinimum(1).withMaximum(1).create()) .withDescription("Mutation range, default = 0.1 (10%).").create(); Option mutprecOpt = obuilder.withLongName("mutprec").withRequired(false).withShortName("mp") .withArgument(abuilder.withName("precision").withMinimum(1).withMaximum(1).create()) .withDescription("Mutation precision, default = 2.").create(); Option popsizeOpt = obuilder.withLongName("popsize").withRequired(true).withShortName("p") .withArgument(abuilder.withName("size").withMinimum(1).withMaximum(1).create()) .withDescription("Population size.").create(); Option gencntOpt = obuilder.withLongName("gencnt").withRequired(true).withShortName("g") .withArgument(abuilder.withName("count").withMinimum(1).withMaximum(1).create()) .withDescription("Generations count.").create(); Option helpOpt = DefaultOptionCreator.helpOption(); Group group = gbuilder.withName("Options").withOption(inputOpt).withOption(helpOpt).withOption(labelOpt) .withOption(thresholdOpt).withOption(crosspntsOpt).withOption(mutrateOpt).withOption(mutrangeOpt) .withOption(mutprecOpt).withOption(popsizeOpt).withOption(gencntOpt).create(); Parser parser = new Parser(); parser.setGroup(group);// ww w. j av a 2s. c om try { CommandLine cmdLine = parser.parse(args); if (cmdLine.hasOption(helpOpt)) { CommandLineUtil.printHelp(group); return; } String dataset = cmdLine.getValue(inputOpt).toString(); int target = Integer.parseInt(cmdLine.getValue(labelOpt).toString()); double threshold = cmdLine.hasOption(thresholdOpt) ? Double.parseDouble(cmdLine.getValue(thresholdOpt).toString()) : 0.5; int crosspnts = cmdLine.hasOption(crosspntsOpt) ? Integer.parseInt(cmdLine.getValue(crosspntsOpt).toString()) : 1; double mutrate = Double.parseDouble(cmdLine.getValue(mutrateOpt).toString()); double mutrange = cmdLine.hasOption(mutrangeOpt) ? Double.parseDouble(cmdLine.getValue(mutrangeOpt).toString()) : 0.1; int mutprec = cmdLine.hasOption(mutprecOpt) ? Integer.parseInt(cmdLine.getValue(mutprecOpt).toString()) : 2; int popSize = Integer.parseInt(cmdLine.getValue(popsizeOpt).toString()); int genCount = Integer.parseInt(cmdLine.getValue(gencntOpt).toString()); long start = System.currentTimeMillis(); runJob(dataset, target, threshold, crosspnts, mutrate, mutrange, mutprec, popSize, genCount); long end = System.currentTimeMillis(); printElapsedTime(end - start); } catch (OptionException e) { log.error("Error while parsing options", e); CommandLineUtil.printHelp(group); } }
From source file:org.apache.mahout.ga.watchmaker.cd.tool.CDInfosTool.java
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException { GroupBuilder gbuilder = new GroupBuilder(); Option inputOpt = DefaultOptionCreator.inputOption().create(); Option helpOpt = DefaultOptionCreator.helpOption(); Group group = gbuilder.withName("Options").withOption(inputOpt).withOption(helpOpt).create(); Parser parser = new Parser(); parser.setGroup(group);/*from w ww . j a va 2 s . c o m*/ try { CommandLine cmdLine = parser.parse(args); if (cmdLine.hasOption(helpOpt)) { CommandLineUtil.printHelp(group); return; } Path input = new Path(cmdLine.getValue(inputOpt).toString()); Path output = new Path("output"); // TODO surely this should be configurable? FileSystem fs = FileSystem.get(input.toUri(), new Configuration()); log.info("Loading Descriptors..."); Descriptors descriptors = loadDescriptors(fs, input); log.info("Gathering informations..."); List<String> descriptions = Lists.newArrayList(); gatherInfos(descriptors, input, output, descriptions); log.info("Storing Descriptions..."); storeDescriptions(fs, input, descriptors, descriptions); } catch (OptionException e) { log.error("Error while parsing options", e); CommandLineUtil.printHelp(group); } }
From source file:org.apache.mahout.knn.tools.TestNewsGroupsKMeanLogisticRegression.java
boolean parseArgs(String[] args) { DefaultOptionBuilder builder = new DefaultOptionBuilder(); Option help = builder.withLongName("help").withDescription("print this list").create(); ArgumentBuilder argumentBuilder = new ArgumentBuilder(); Option inputFileOption = builder.withLongName("input").withShortName("i").withRequired(true) .withArgument(argumentBuilder.withName("input").withMaximum(1).create()) .withDescription("where to get test data (encoded with tf-idf)").create(); Option modelFileOption = builder.withLongName("model").withShortName("m").withRequired(true) .withArgument(argumentBuilder.withName("model").withMaximum(1).create()) .withDescription("where to get a model").create(); Option centroidsFileOption = builder.withLongName("centroids").withShortName("c").withRequired(true) .withArgument(argumentBuilder.withName("centroids").withMaximum(1).create()) .withDescription("where to get the centroids seqfile").create(); Option labelFileOption = builder.withLongName("labels").withShortName("l").withRequired(true) .withArgument(argumentBuilder.withName("labels").withMaximum(1).create()) .withDescription("CSV file containing the cluster labels").create(); Group normalArgs = new GroupBuilder().withOption(help).withOption(inputFileOption) .withOption(modelFileOption).withOption(centroidsFileOption).withOption(labelFileOption).create(); Parser parser = new Parser(); parser.setHelpOption(help);/*from ww w . j a va 2 s . c o m*/ parser.setHelpTrigger("--help"); parser.setGroup(normalArgs); parser.setHelpFormatter(new HelpFormatter(" ", "", " ", 130)); CommandLine cmdLine = parser.parseAndHelp(args); if (cmdLine == null) { return false; } inputFile = (String) cmdLine.getValue(inputFileOption); modelFile = (String) cmdLine.getValue(modelFileOption); centroidsFile = (String) cmdLine.getValue(centroidsFileOption); labelFile = (String) cmdLine.getValue(labelFileOption); return true; }
From source file:org.apache.mahout.regression.penalizedlinear.Job.java
private static boolean parseJobArgs(String[] args) throws IOException, InterruptedException, ClassNotFoundException { DefaultOptionBuilder builder = new DefaultOptionBuilder(); Option help = builder.withLongName("help").withDescription("print this list").create(); ArgumentBuilder argumentBuilder = new ArgumentBuilder(); Option inputFile = builder.withLongName("input").withRequired(true) .withArgument(argumentBuilder.withName("input").withMaximum(1).create()) .withDescription(/*w w w .j av a 2s . c om*/ "where to get training data (Mahout sequence file of VectorWritable or white-spaced TEXT file); in each line, the first element is response; rest are predictors.") .create(); Option outputFile = builder.withLongName("output").withRequired(true) .withArgument(argumentBuilder.withName("output").withMaximum(1).create()) .withDescription("where to get results").create(); Option lambda = builder.withLongName("lambda") .withArgument(argumentBuilder.withName("lambda").withDefault("0").withMinimum(1).create()) .withDescription("an increasing positive sequence of penalty coefficient, " + "with length n >= 0; if lambda is not specified, the sequence is chosen by algorithm.") .create(); Option alpha = builder.withLongName("alpha") .withArgument( argumentBuilder.withName("alpha").withDefault("1").withMinimum(1).withMaximum(1).create()) .withDescription("the elastic-net coefficient with default value 1 (LASSO)").create(); Option bias = builder.withLongName("bias").withDescription("include a bias term").create(); Option numOfCV = builder.withLongName("numOfCV") .withArgument( argumentBuilder.withName("numOfCV").withDefault("5").withMinimum(0).withMaximum(1).create()) .withDescription("number of cross validation, the rule of thumb is 5 or 10").create(); Option convert = builder.withLongName("convert") .withDescription( "pre-processing step if the input file is not Mahout sequence files of VectorWritable: " + "converting space-delimited TEXT file containing floating point numbers into " + "Mahout sequence files of VectorWritable suitable for input of Map-Reduce job.") .create(); Group normalArgs = new GroupBuilder().withOption(help).withOption(inputFile).withOption(outputFile) .withOption(lambda).withOption(alpha).withOption(bias).withOption(numOfCV).withOption(convert) .create(); Parser parser = new Parser(); parser.setHelpOption(help); parser.setHelpTrigger("--help"); parser.setGroup(normalArgs); parser.setHelpFormatter(new HelpFormatter(" ", "", " ", 130)); CommandLine cmdLine = parser.parseAndHelp(args); if (cmdLine == null) { return false; } Path input = new Path((String) cmdLine.getValue(inputFile)); Path output = new Path((String) cmdLine.getValue(outputFile), DIRECTORY_CONTAINING_CONVERTED_INPUT); if (cmdLine.hasOption(convert)) { jobArgs = new String[args.length - 1]; int index = 0; for (int i = 0; i < args.length; ++i) { if (args[i].equals("--convert")) { continue; } jobArgs[index++] = args[i]; if (args[i].equals("--input")) { args[i + 1] = output.toString(); InputDriver.runJob(input, output, "org.apache.mahout.math.RandomAccessSparseVector"); } if (args[i].equals("--output")) { args[i + 1] = (new Path((String) cmdLine.getValue(outputFile), DIRECTORY_CONTAINING_OUTPUT)) .toString(); } } } else { jobArgs = args; } return true; }
From source file:org.apache.mahout.regression.penalizedlinear.LinearCrossValidation.java
private boolean parseArgs(String[] args) { DefaultOptionBuilder builder = new DefaultOptionBuilder(); Option help = builder.withLongName("help").withDescription("print this list").create(); ArgumentBuilder argumentBuilder = new ArgumentBuilder(); Option inputFile = builder.withLongName("input").withRequired(true) .withArgument(argumentBuilder.withName("input").withMaximum(1).create()) .withDescription("where to get training data (CSV or white-spaced TEXT file)").create(); Option outputFile = builder.withLongName("output").withRequired(true) .withArgument(argumentBuilder.withName("output").withMaximum(1).create()) .withDescription("where to get results").create(); Option dependent = builder.withLongName("dependent").withRequired(true) .withArgument(argumentBuilder.withName("dependent").withMinimum(1).withMaximum(1).create()) .withDescription("the dependent features").create(); Option independent = builder.withLongName("independent").withRequired(true) .withArgument(argumentBuilder.withName("independent").create()) .withDescription("the independent features").create(); Option interaction = builder.withLongName("interaction").withRequired(true) .withArgument(argumentBuilder.withName("interaction").withMinimum(0).create()) .withDescription(/*from w w w .j ava2s . c o m*/ "the interactions of features, the format is: feature1:feature2 (identical features are OK)") .create(); Option bias = builder.withLongName("bias").withDescription("include a bias term").create(); Option lambda = builder.withLongName("lambda") .withArgument(argumentBuilder.withName("lambda").withDefault("0").withMinimum(1).create()) .withDescription("an increasing positive sequence of penalty coefficient, " + "with length n >= 0; if lambda is not specified, the sequence is chosen by algorithm.") .create(); Option alpha = builder.withLongName("alpha") .withArgument( argumentBuilder.withName("alpha").withDefault("1").withMinimum(1).withMaximum(1).create()) .withDescription("the elastic-net coefficient with default value 1 (LASSO)").create(); Option numOfCV = builder.withLongName("numOfCV") .withArgument( argumentBuilder.withName("numOfCV").withDefault("5").withMinimum(0).withMaximum(1).create()) .withDescription("number of cross validation, the rule of thumb is 5 or 10").create(); Group normalArgs = new GroupBuilder().withOption(help).withOption(inputFile).withOption(outputFile) .withOption(dependent).withOption(independent).withOption(interaction).withOption(bias) .withOption(lambda).withOption(alpha).withOption(numOfCV).create(); Parser parser = new Parser(); parser.setHelpOption(help); parser.setHelpTrigger("--help"); parser.setGroup(normalArgs); parser.setHelpFormatter(new HelpFormatter(" ", "", " ", 130)); CommandLine cmdLine = parser.parseAndHelp(args); if (cmdLine == null) { return false; } parameter = new LinearCrossValidationParameter(); parameter.numOfCV = Integer.parseInt((String) cmdLine.getValue(numOfCV)); parameter.alpha = Float.parseFloat((String) cmdLine.getValue(alpha)); parameter.intercept = cmdLine.hasOption(bias); parameter.dependent = (String) cmdLine.getValue(dependent); String independentString = ""; for (Object x : cmdLine.getValues(independent)) { independentString += x.toString() + ","; } parameter.independent = independentString.substring(0, Math.max(independentString.length() - 1, 0)); String interactionString = ""; for (Object x : cmdLine.getValues(interaction)) { interactionString += x.toString() + ","; } parameter.interaction = interactionString.substring(0, Math.max(interactionString.length() - 1, 0)); if (!processLambda(parameter, cmdLine, lambda) || parameter.alpha < 0.0 || parameter.alpha > 1.0 || parameter.numOfCV < 1 || parameter.numOfCV > 20) { log.error( "please make sure the lambda sequence is positive and increasing, and 0.0 <= alphaValue <= 1.0 and 1 <= numofCV <= 20"); return false; } input = (String) cmdLine.getValue(inputFile); output = (String) cmdLine.getValue(outputFile); return true; }