You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by is...@apache.org on 2009/10/09 15:19:58 UTC

svn commit: r823545 - /lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/TasteOptionParser.java

Author: isabel
Date: Fri Oct  9 13:19:57 2009
New Revision: 823545

URL: http://svn.apache.org/viewvc?rev=823545&view=rev
Log:
MAHOUT-138 - Missing parser class.

Added:
    lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/TasteOptionParser.java

Added: lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/TasteOptionParser.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/TasteOptionParser.java?rev=823545&view=auto
==============================================================================
--- lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/TasteOptionParser.java (added)
+++ lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/TasteOptionParser.java Fri Oct  9 13:19:57 2009
@@ -0,0 +1,60 @@
+package org.apache.mahout.cf.taste.example;
+
+import java.io.File;
+
+import org.apache.commons.cli2.CommandLine;
+import org.apache.commons.cli2.Group;
+import org.apache.commons.cli2.Option;
+import org.apache.commons.cli2.OptionException;
+import org.apache.commons.cli2.builder.ArgumentBuilder;
+import org.apache.commons.cli2.builder.DefaultOptionBuilder;
+import org.apache.commons.cli2.builder.GroupBuilder;
+import org.apache.commons.cli2.commandline.Parser;
+import org.apache.mahout.common.CommandLineUtil;
+import org.apache.mahout.common.commandline.DefaultOptionCreator;
+
+/**
+ * This class provides a common implementation for parsing input parameters for
+ * all taste examples. Currently they only need the path to the recommendations
+ * file as input.
+ * 
+ * The class is safe to be used in threaded contexts.
+ */
+public class TasteOptionParser {
+
+  /**
+   * Parse the given command line arguments.
+   * @param args the arguments as given to the application.
+   * @return the input file if a file was given on the command line, null otherwise. 
+   * */
+  public File getRatings(final String[] args) throws OptionException {
+    File file = null;
+    DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
+    ArgumentBuilder abuilder = new ArgumentBuilder();
+    GroupBuilder gbuilder = new GroupBuilder();
+
+    Option inputOpt = obuilder.withLongName("input").withRequired(false)
+        .withShortName("i").withArgument(
+            abuilder.withName("input").withMinimum(1).withMaximum(1).create())
+        .withDescription("The Path for input data directory.").create();
+
+    Option helpOpt = DefaultOptionCreator.helpOption(obuilder);
+
+    Group group = gbuilder.withName("Options").withOption(inputOpt).withOption(
+        helpOpt).create();
+
+    Parser parser = new Parser();
+    parser.setGroup(group);
+    CommandLine cmdLine = parser.parse(args);
+
+    if (cmdLine.hasOption(helpOpt)) {
+      CommandLineUtil.printHelp(group);
+      System.exit(0);
+    }
+
+    String prefsFile = cmdLine.getValue(inputOpt).toString();
+    file = new File(prefsFile);
+    return file;
+  }
+
+}