mahout-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sro...@apache.org
Subject svn commit: r735537 - in /lucene/mahout/trunk: core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java examples/src/main/java/org/apache/mahout/cf/taste/example/jester/JesterDataModel.java
Date Sun, 18 Jan 2009 20:49:13 GMT
Author: srowen
Date: Sun Jan 18 12:49:13 2009
New Revision: 735537

URL: http://svn.apache.org/viewvc?rev=735537&view=rev
Log: (empty)

Modified:
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java
    lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/jester/JesterDataModel.java

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java?rev=735537&r1=735536&r2=735537&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java
(original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java
Sun Jan 18 12:49:13 2009
@@ -40,6 +40,8 @@
 import java.util.Map;
 import java.util.Timer;
 import java.util.TimerTask;
+import java.util.Iterator;
+import java.util.Collections;
 import java.util.concurrent.locks.ReentrantLock;
 
 /**
@@ -103,7 +105,10 @@
     try {
       Map<String, List<Preference>> data = new FastMap<String, List<Preference>>();
 
-      processFile(data);
+      processFile(dataFile, data);
+      for (File updateFile : findUpdateFiles()) {
+        processFile(updateFile, data);
+      }
 
       List<User> users = new ArrayList<User>(data.size());
       for (Map.Entry<String, List<Preference>> entries : data.entrySet()) {
@@ -118,10 +123,22 @@
     }
   }
 
-  protected void processFile(Map<String, List<Preference>> data) {
+  private Iterable<File> findUpdateFiles() {
+    File parentDir = dataFile.getParentFile();
+    List<File> updateFiles = new ArrayList<File>();
+    for (File updateFile : parentDir.listFiles()) {
+      if (!updateFile.getName().equals(dataFile.getName())) {
+        updateFiles.add(updateFile);
+      }
+    }
+    Collections.sort(updateFiles);
+    return updateFiles;
+  }
+
+  protected void processFile(File dataOrUpdateFile, Map<String, List<Preference>>
data) {
     log.info("Reading file info...");
     Map<String, Item> itemCache = new FastMap<String, Item>(1001);
-    for (String line : new FileLineIterable(dataFile, false)) {
+    for (String line : new FileLineIterable(dataOrUpdateFile, false)) {
       if (line.length() > 0) {
         log.debug("Read line: {}", line);
         processLine(line, data, itemCache);
@@ -154,19 +171,34 @@
     }
     String userID = line.substring(0, commaOne);
     String itemID = line.substring(commaOne + 1, commaTwo);
-    double preferenceValue = Double.parseDouble(line.substring(commaTwo + 1));
+    String preferenceValueString = line.substring(commaTwo + 1);
     List<Preference> prefs = data.get(userID);
     if (prefs == null) {
       prefs = new ArrayList<Preference>();
       data.put(userID, prefs);
     }
-    Item item = itemCache.get(itemID);
-    if (item == null) {
-      item = buildItem(itemID);
-      itemCache.put(itemID, item);
+
+    if (preferenceValueString.length() == 0) {
+      // remove pref
+      Iterator<Preference> prefsIterator = prefs.iterator();
+      while (prefsIterator.hasNext()) {
+        Preference pref = prefsIterator.next();
+        if (pref.getItem().getID().equals(itemID)) {
+          prefsIterator.remove();
+          break;
+        }
+      }
+    } else {
+      // add pref -- assume it does not already exist
+      double preferenceValue = Double.parseDouble(preferenceValueString);
+      Item item = itemCache.get(itemID);
+      if (item == null) {
+        item = buildItem(itemID);
+        itemCache.put(itemID, item);
+      }
+      log.debug("Read item '{}' for user ID '{}'", item, userID);
+      prefs.add(buildPreference(null, item, preferenceValue));
     }
-    log.debug("Read item '{}' for user ID '{}'", item, userID);
-    prefs.add(buildPreference(null, item, preferenceValue));
   }
 
   private void checkLoaded() {

Modified: lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/jester/JesterDataModel.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/jester/JesterDataModel.java?rev=735537&r1=735536&r2=735537&view=diff
==============================================================================
--- lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/jester/JesterDataModel.java
(original)
+++ lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/jester/JesterDataModel.java
Sun Jan 18 12:49:13 2009
@@ -39,7 +39,7 @@
 
   /**
    * @param ratingsFile Jester ratings file in CSV format
-   * @throws java.io.IOException if an error occurs while reading or writing files
+   * @throws IOException if an error occurs while reading or writing files
    */
   public JesterDataModel(File ratingsFile) throws IOException {
     super(ratingsFile);



Mime
View raw message