mahout-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sro...@apache.org
Subject svn commit: r806115 - in /lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing: BookCrossingDataModel.java BookCrossingRecommender.java BookCrossingRecommenderBuilder.java BookCrossingRecommenderEvaluatorRunner.java
Date Thu, 20 Aug 2009 10:53:45 GMT
Author: srowen
Date: Thu Aug 20 10:53:45 2009
New Revision: 806115

URL: http://svn.apache.org/viewvc?rev=806115&view=rev
Log:
Key fixes to the Book Crossing example now that IDs are numeric, and some other adjustments
and polish

Modified:
    lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingDataModel.java
    lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommender.java
    lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderBuilder.java
    lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderEvaluatorRunner.java

Modified: lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingDataModel.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingDataModel.java?rev=806115&r1=806114&r2=806115&view=diff
==============================================================================
--- lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingDataModel.java
(original)
+++ lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingDataModel.java
Thu Aug 20 10:53:45 2009
@@ -27,9 +27,9 @@
 import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
+import java.io.FileNotFoundException;
 import java.nio.charset.Charset;
 
-
 /**
  * See <a href="http://www.informatik.uni-freiburg.de/~cziegler/BX/BX-CSV-Dump.zip">download</a>
for
  * data needed by this class. The BX-Book-Ratings.csv file is needed.
@@ -50,23 +50,29 @@
   }
 
   private static File convertBCFile(File originalFile) throws IOException {
+    if (!originalFile.exists()) {
+      throw new FileNotFoundException(originalFile.toString());
+    }
     File resultFile = new File(new File(System.getProperty("java.io.tmpdir")), "taste.bookcrossing.txt");
-    if (!resultFile.exists()) {
-      PrintWriter writer = null;
-      try {
-        writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(resultFile),
Charset.forName("UTF-8")));
-        for (String line : new FileLineIterable(originalFile, true)) {
-          // Delete commas, make semicolon delimiter into comma delimter, then remove quotes
-          String convertedLine = line.replace(",", "").replace(';', ',').replace("\"", "");
-          writer.println(convertedLine);
+    resultFile.delete();
+    PrintWriter writer = null;
+    try {
+      writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(resultFile), Charset.forName("UTF-8")));
+      for (String line : new FileLineIterable(originalFile, true)) {
+        // Delete replace anything that isn't numeric, or a semicolon delimiter. Make comma
the delimiter.
+        String convertedLine = line.replaceAll("[^0-9;]", "").replace(';', ',');
+        // If this means we deleted an entire ID -- few cases like that -- skip the line
+        if (convertedLine.contains(",,")) {
+          continue;
         }
-        writer.flush();
-      } catch (IOException ioe) {
-        resultFile.delete();
-        throw ioe;
-      } finally {
-        IOUtils.quietClose(writer);
+        writer.println(convertedLine);
       }
+      writer.flush();
+    } catch (IOException ioe) {
+      resultFile.delete();
+      throw ioe;
+    } finally {
+      IOUtils.quietClose(writer);
     }
     return resultFile;
   }

Modified: lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommender.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommender.java?rev=806115&r1=806114&r2=806115&view=diff
==============================================================================
--- lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommender.java
(original)
+++ lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommender.java
Thu Aug 20 10:53:45 2009
@@ -41,10 +41,10 @@
 
   private final Recommender recommender;
 
-  public BookCrossingRecommender(DataModel dataModel, BookCrossingDataModel bcModel) throws
TasteException {
+  public BookCrossingRecommender(DataModel bcModel) throws TasteException {
     UserSimilarity similarity = new PearsonCorrelationSimilarity(bcModel);
-    UserNeighborhood neighborhood = new NearestNUserNeighborhood(5, similarity, dataModel);
-    recommender = new CachingRecommender(new GenericUserBasedRecommender(dataModel, neighborhood,
similarity));
+    UserNeighborhood neighborhood = new NearestNUserNeighborhood(10, 0.0, similarity, bcModel,
0.1);
+    recommender = new CachingRecommender(new GenericUserBasedRecommender(bcModel, neighborhood,
similarity));
   }
 
   @Override

Modified: lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderBuilder.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderBuilder.java?rev=806115&r1=806114&r2=806115&view=diff
==============================================================================
--- lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderBuilder.java
(original)
+++ lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderBuilder.java
Thu Aug 20 10:53:45 2009
@@ -24,15 +24,9 @@
 
 final class BookCrossingRecommenderBuilder implements RecommenderBuilder {
 
-  private final BookCrossingDataModel bcModel;
-
-  BookCrossingRecommenderBuilder(BookCrossingDataModel bcModel) {
-    this.bcModel = bcModel;
-  }
-
   @Override
   public Recommender buildRecommender(DataModel dataModel) throws TasteException {
-    return new BookCrossingRecommender(dataModel, bcModel);
+    return new BookCrossingRecommender(dataModel);
   }
 
 }
\ No newline at end of file

Modified: lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderEvaluatorRunner.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderEvaluatorRunner.java?rev=806115&r1=806114&r2=806115&view=diff
==============================================================================
--- lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderEvaluatorRunner.java
(original)
+++ lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderEvaluatorRunner.java
Thu Aug 20 10:53:45 2009
@@ -20,10 +20,12 @@
 import org.apache.mahout.cf.taste.common.TasteException;
 import org.apache.mahout.cf.taste.eval.RecommenderEvaluator;
 import org.apache.mahout.cf.taste.impl.eval.AverageAbsoluteDifferenceRecommenderEvaluator;
+import org.apache.mahout.cf.taste.model.DataModel;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
+import java.io.File;
 
 public final class BookCrossingRecommenderEvaluatorRunner {
 
@@ -35,12 +37,18 @@
 
   public static void main(String... args) throws IOException, TasteException {
     RecommenderEvaluator evaluator = new AverageAbsoluteDifferenceRecommenderEvaluator();
-    BookCrossingDataModel model = new BookCrossingDataModel();
-    double evaluation = evaluator.evaluate(new BookCrossingRecommenderBuilder(model),
+    DataModel model;
+    if (args.length >= 1) {
+      model = new BookCrossingDataModel(new File(args[0]));
+    } else {
+      model = new BookCrossingDataModel();
+    }
+
+    double evaluation = evaluator.evaluate(new BookCrossingRecommenderBuilder(),
                                            null,
                                            model,
-                                           0.9,
-                                           0.1);
+                                           0.95,
+                                           0.05);
     log.info(String.valueOf(evaluation));
   }
 



Mime
View raw message