mahout-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sro...@apache.org
Subject svn commit: r1147583 - in /mahout/trunk: core/src/main/java/org/apache/mahout/cf/taste/impl/model/ core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/ core/src/main/java/org/apache/mahout/cf/taste/model/ examples/src/main/java/org/apache/mah...
Date Sun, 17 Jul 2011 11:52:22 GMT
Author: srowen
Date: Sun Jul 17 11:52:21 2011
New Revision: 1147583

URL: http://svn.apache.org/viewvc?rev=1147583&view=rev
Log:
MAHOUT-764 split getNumUsersWithPreferenceFor() into two methods for clarity and efficiency

Modified:
    mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericBooleanPrefDataModel.java
    mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericDataModel.java
    mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/PlusAnonymousUserDataModel.java
    mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java
    mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/model/DataModel.java
    mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/KDDCupDataModel.java
    mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixDataModel.java
    mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixFileDataModel.java
    mahout/trunk/integration/src/main/java/org/apache/mahout/cf/taste/impl/model/jdbc/AbstractJDBCDataModel.java
    mahout/trunk/integration/src/main/java/org/apache/mahout/cf/taste/impl/model/jdbc/ReloadFromJDBCDataModel.java
    mahout/trunk/integration/src/main/java/org/apache/mahout/cf/taste/impl/model/mongodb/MongoDBDataModel.java

Modified: mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericBooleanPrefDataModel.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericBooleanPrefDataModel.java?rev=1147583&r1=1147582&r2=1147583&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericBooleanPrefDataModel.java
(original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericBooleanPrefDataModel.java
Sun Jul 17 11:52:21 2011
@@ -260,39 +260,22 @@ public final class GenericBooleanPrefDat
   }
   
   @Override
-  public int getNumUsersWithPreferenceFor(long... itemIDs) {
-    Preconditions.checkArgument(itemIDs != null, "itemIDs is null");
-    Preconditions.checkArgument(itemIDs.length == 1 || itemIDs.length == 2, "Illegal number
of IDs", itemIDs.length);
-    FastIDSet userIDs1 = preferenceForItems.get(itemIDs[0]);
+  public int getNumUsersWithPreferenceFor(long itemID) {
+    FastIDSet userIDs1 = preferenceForItems.get(itemID);
+    return userIDs1 == null ? 0 : userIDs1.size();
+  }
+
+  @Override
+  public int getNumUsersWithPreferenceFor(long itemID1, long itemID2) {
+    FastIDSet userIDs1 = preferenceForItems.get(itemID1);
     if (userIDs1 == null) {
       return 0;
     }
-
-    if (itemIDs.length == 1) {
-      return userIDs1.size();
-    }
-
-    // itemIDs.length == 2
-    FastIDSet userIDs2 = preferenceForItems.get(itemIDs[1]);
+    FastIDSet userIDs2 = preferenceForItems.get(itemID2);
     if (userIDs2 == null) {
       return 0;
     }
     return userIDs1.intersectionSize(userIDs2);
-
-    /*
-    FastIDSet intersection = new FastIDSet(userIDs1.size());
-    intersection.addAll(userIDs1);
-    int i = 1;
-    while (!intersection.isEmpty() && (i < itemIDs.length)) {
-      userIDs1 = preferenceForItems.get(itemIDs[i]);
-      if (userIDs1 == null) {
-        return 0;
-      }
-      intersection.retainAll(userIDs1);
-      i++;
-    }
-    return intersection.size();
-     */
   }
   
   @Override

Modified: mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericDataModel.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericDataModel.java?rev=1147583&r1=1147582&r2=1147583&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericDataModel.java
(original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericDataModel.java
Sun Jul 17 11:52:21 2011
@@ -273,22 +273,20 @@ public final class GenericDataModel exte
   public int getNumUsers() {
     return userIDs.length;
   }
+
+  @Override
+  public int getNumUsersWithPreferenceFor(long itemID) {
+    PreferenceArray prefs1 = preferenceForItems.get(itemID);
+    return prefs1 == null ? 0 : prefs1.length();
+  }
   
   @Override
-  public int getNumUsersWithPreferenceFor(long... itemIDs) {
-    Preconditions.checkArgument(itemIDs != null, "itemIDs is null");
-    Preconditions.checkArgument(itemIDs.length == 1 || itemIDs.length == 2, "Illegal number
of IDs", itemIDs.length);
-    PreferenceArray prefs1 = preferenceForItems.get(itemIDs[0]);
+  public int getNumUsersWithPreferenceFor(long itemID1, long itemID2) {
+    PreferenceArray prefs1 = preferenceForItems.get(itemID1);
     if (prefs1 == null) {
       return 0;
     }
-
-    if (itemIDs.length == 1) {
-      return prefs1.length();
-    }
-
-    // itemIDs.length == 2
-    PreferenceArray prefs2 = preferenceForItems.get(itemIDs[1]);
+    PreferenceArray prefs2 = preferenceForItems.get(itemID2);
     if (prefs2 == null) {
       return 0;
     }

Modified: mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/PlusAnonymousUserDataModel.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/PlusAnonymousUserDataModel.java?rev=1147583&r1=1147582&r2=1147583&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/PlusAnonymousUserDataModel.java
(original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/PlusAnonymousUserDataModel.java
Sun Jul 17 11:52:21 2011
@@ -211,25 +211,37 @@ public final class PlusAnonymousUserData
   }
   
   @Override
-  public int getNumUsersWithPreferenceFor(long... itemIDs) throws TasteException {
+  public int getNumUsersWithPreferenceFor(long itemID) throws TasteException {
     if (tempPrefs == null) {
-      return delegate.getNumUsersWithPreferenceFor(itemIDs);
+      return delegate.getNumUsersWithPreferenceFor(itemID);
     }
-    boolean hasAll = true;
-    for (long itemID : itemIDs) {
-      boolean found = false;
-      for (int i = 0; i < tempPrefs.length(); i++) {
-        if (tempPrefs.getItemID(i) == itemID) {
-          found = true;
-          break;
-        }
-      }
-      if (!found) {
-        hasAll = false;
+    boolean found = false;
+    for (int i = 0; i < tempPrefs.length(); i++) {
+      if (tempPrefs.getItemID(i) == itemID) {
+        found = true;
         break;
       }
     }
-    return delegate.getNumUsersWithPreferenceFor(itemIDs) + (hasAll ? 1 : 0);
+    return delegate.getNumUsersWithPreferenceFor(itemID) + (found ? 1 : 0);
+  }
+
+  @Override
+  public int getNumUsersWithPreferenceFor(long itemID1, long itemID2) throws TasteException
{
+    if (tempPrefs == null) {
+      return delegate.getNumUsersWithPreferenceFor(itemID1, itemID2);
+    }
+    boolean found1 = false;
+    boolean found2 = false;
+    for (int i = 0; i < tempPrefs.length() && !(found1 && found2); i++)
{
+      long itemID = tempPrefs.getItemID(i);
+      if (itemID == itemID1) {
+        found1 = true;
+      }
+      if (itemID == itemID2) {
+        found2 = true;
+      }
+    }
+    return delegate.getNumUsersWithPreferenceFor(itemID1, itemID2) + (found1 && found2
? 1 : 0);
   }
   
   @Override

Modified: mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java?rev=1147583&r1=1147582&r2=1147583&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java
(original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java
Sun Jul 17 11:52:21 2011
@@ -670,8 +670,13 @@ public class FileDataModel extends Abstr
   }
 
   @Override
-  public int getNumUsersWithPreferenceFor(long... itemIDs) throws TasteException {
-    return delegate.getNumUsersWithPreferenceFor(itemIDs);
+  public int getNumUsersWithPreferenceFor(long itemID) throws TasteException {
+    return delegate.getNumUsersWithPreferenceFor(itemID);
+  }
+
+  @Override
+  public int getNumUsersWithPreferenceFor(long itemID1, long itemID2) throws TasteException
{
+    return delegate.getNumUsersWithPreferenceFor(itemID1, itemID2);
   }
 
   /**

Modified: mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/model/DataModel.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/model/DataModel.java?rev=1147583&r1=1147582&r2=1147583&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/model/DataModel.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/model/DataModel.java Sun Jul
17 11:52:21 2011
@@ -122,14 +122,19 @@ public interface DataModel extends Refre
   int getNumUsers() throws TasteException;
   
   /**
-   * @param itemIDs item IDs to check for
-   * @return the number of users who have expressed a preference for all of the items
+   * @param itemID item ID to check for
+   * @return the number of users who have expressed a preference for the item
    * @throws TasteException if an error occurs while accessing the data
-   * @throws IllegalArgumentException
-   *  if itemIDs is null or empty. Arguments larger than 2 elements are not necessarily supported
-   *  and may result in this exception
    */
-  int getNumUsersWithPreferenceFor(long... itemIDs) throws TasteException;
+  int getNumUsersWithPreferenceFor(long itemID) throws TasteException;
+
+  /**
+   * @param itemID1 first item ID to check for
+   * @param itemID2 second item ID to check for
+   * @return the number of users who have expressed a preference for the items
+   * @throws TasteException if an error occurs while accessing the data
+   */
+  int getNumUsersWithPreferenceFor(long itemID1, long itemID2) throws TasteException;
   
   /**
    * <p>

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/KDDCupDataModel.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/KDDCupDataModel.java?rev=1147583&r1=1147582&r2=1147583&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/KDDCupDataModel.java
(original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/kddcup/KDDCupDataModel.java
Sun Jul 17 11:52:21 2011
@@ -188,8 +188,13 @@ public final class KDDCupDataModel imple
   }
 
   @Override
-  public int getNumUsersWithPreferenceFor(long... itemIDs) throws TasteException {
-    return delegate.getNumUsersWithPreferenceFor(itemIDs);
+  public int getNumUsersWithPreferenceFor(long itemID) throws TasteException {
+    return delegate.getNumUsersWithPreferenceFor(itemID);
+  }
+
+  @Override
+  public int getNumUsersWithPreferenceFor(long itemID1, long itemID2) throws TasteException
{
+    return delegate.getNumUsersWithPreferenceFor(itemID1, itemID2);
   }
 
   @Override

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixDataModel.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixDataModel.java?rev=1147583&r1=1147582&r2=1147583&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixDataModel.java
(original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixDataModel.java
Sun Jul 17 11:52:21 2011
@@ -147,8 +147,13 @@ public final class NetflixDataModel impl
   }
   
   @Override
-  public int getNumUsersWithPreferenceFor(long... itemIDs) throws TasteException {
-    return delegate.getNumUsersWithPreferenceFor(itemIDs);
+  public int getNumUsersWithPreferenceFor(long itemID) throws TasteException {
+    return delegate.getNumUsersWithPreferenceFor(itemID);
+  }
+
+  @Override
+  public int getNumUsersWithPreferenceFor(long itemID1, long itemID2) throws TasteException
{
+    return delegate.getNumUsersWithPreferenceFor(itemID1, itemID2);
   }
   
   @Override

Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixFileDataModel.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixFileDataModel.java?rev=1147583&r1=1147582&r2=1147583&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixFileDataModel.java
(original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/netflix/NetflixFileDataModel.java
Sun Jul 17 11:52:21 2011
@@ -104,15 +104,26 @@ public final class NetflixFileDataModel 
   public int getNumItems() {
     return MovieIDIterator.COUNT;
   }
-  
+
+  /**
+   * @throws UnsupportedOperationException
+   */
   @Override
   public int getNumUsers() {
-    throw new UnsupportedOperationException(); // TODO
+    throw new UnsupportedOperationException();
   }
-  
+
+  /**
+   * @throws UnsupportedOperationException
+   */
+  @Override
+  public int getNumUsersWithPreferenceFor(long itemID) {
+    throw new UnsupportedOperationException();
+  }
+
   @Override
-  public int getNumUsersWithPreferenceFor(long... itemIDs) {
-    throw new UnsupportedOperationException(); // TODO
+  public int getNumUsersWithPreferenceFor(long itemID1, long itemID2) {
+    throw new UnsupportedOperationException();
   }
   
   /**

Modified: mahout/trunk/integration/src/main/java/org/apache/mahout/cf/taste/impl/model/jdbc/AbstractJDBCDataModel.java
URL: http://svn.apache.org/viewvc/mahout/trunk/integration/src/main/java/org/apache/mahout/cf/taste/impl/model/jdbc/AbstractJDBCDataModel.java?rev=1147583&r1=1147582&r2=1147583&view=diff
==============================================================================
--- mahout/trunk/integration/src/main/java/org/apache/mahout/cf/taste/impl/model/jdbc/AbstractJDBCDataModel.java
(original)
+++ mahout/trunk/integration/src/main/java/org/apache/mahout/cf/taste/impl/model/jdbc/AbstractJDBCDataModel.java
Sun Jul 17 11:52:21 2011
@@ -564,14 +564,13 @@ public abstract class AbstractJDBCDataMo
   }
 
   @Override
-  public int getNumUsersWithPreferenceFor(long... itemIDs) throws TasteException {
-    Preconditions.checkArgument(itemIDs != null, "itemIDs is null");
-
-    int length = itemIDs.length;
-    Preconditions.checkArgument(length != 0 && length <= 2, "Illegal number of
item IDs: " + length);
+  public int getNumUsersWithPreferenceFor(long itemID) throws TasteException {
+    return itemPrefCounts.get(itemID);
+  }
 
-    return length == 1 ? itemPrefCounts.get(itemIDs[0]) : getNumThings("user preferring items",
-      getNumPreferenceForItemsSQL, itemIDs);
+  @Override
+  public int getNumUsersWithPreferenceFor(long itemID1, long itemID2) throws TasteException
{
+    return getNumThings("user preferring items", getNumPreferenceForItemsSQL, itemID1, itemID2);
   }
 
   private int getNumThings(String name, String sql, long... args) throws TasteException {

Modified: mahout/trunk/integration/src/main/java/org/apache/mahout/cf/taste/impl/model/jdbc/ReloadFromJDBCDataModel.java
URL: http://svn.apache.org/viewvc/mahout/trunk/integration/src/main/java/org/apache/mahout/cf/taste/impl/model/jdbc/ReloadFromJDBCDataModel.java?rev=1147583&r1=1147582&r2=1147583&view=diff
==============================================================================
--- mahout/trunk/integration/src/main/java/org/apache/mahout/cf/taste/impl/model/jdbc/ReloadFromJDBCDataModel.java
(original)
+++ mahout/trunk/integration/src/main/java/org/apache/mahout/cf/taste/impl/model/jdbc/ReloadFromJDBCDataModel.java
Sun Jul 17 11:52:21 2011
@@ -141,8 +141,13 @@ public final class ReloadFromJDBCDataMod
   }
 
   @Override
-  public int getNumUsersWithPreferenceFor(long... itemIDs) throws TasteException {
-    return delegateInMemory.getNumUsersWithPreferenceFor(itemIDs);
+  public int getNumUsersWithPreferenceFor(long itemID) throws TasteException {
+    return delegateInMemory.getNumUsersWithPreferenceFor(itemID);
+  }
+
+  @Override
+  public int getNumUsersWithPreferenceFor(long itemID1, long itemID2) throws TasteException
{
+    return delegateInMemory.getNumUsersWithPreferenceFor(itemID1, itemID2);
   }
 
   @Override

Modified: mahout/trunk/integration/src/main/java/org/apache/mahout/cf/taste/impl/model/mongodb/MongoDBDataModel.java
URL: http://svn.apache.org/viewvc/mahout/trunk/integration/src/main/java/org/apache/mahout/cf/taste/impl/model/mongodb/MongoDBDataModel.java?rev=1147583&r1=1147582&r2=1147583&view=diff
==============================================================================
--- mahout/trunk/integration/src/main/java/org/apache/mahout/cf/taste/impl/model/mongodb/MongoDBDataModel.java
(original)
+++ mahout/trunk/integration/src/main/java/org/apache/mahout/cf/taste/impl/model/mongodb/MongoDBDataModel.java
Sun Jul 17 11:52:21 2011
@@ -817,8 +817,13 @@ public final class MongoDBDataModel impl
   }
 
   @Override
-  public int getNumUsersWithPreferenceFor(long... itemIDs) throws TasteException {
-    return delegate.getNumUsersWithPreferenceFor(itemIDs);
+  public int getNumUsersWithPreferenceFor(long itemID) throws TasteException {
+    return delegate.getNumUsersWithPreferenceFor(itemID);
+  }
+
+  @Override
+  public int getNumUsersWithPreferenceFor(long itemID1, long itemID2) throws TasteException
{
+    return delegate.getNumUsersWithPreferenceFor(itemID1, itemID2);
   }
 
   @Override



Mime
View raw message