mahout-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sro...@apache.org
Subject svn commit: r661920 - in /lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model: BooleanPreference.java GenericPreference.java GenericUser.java SettableUserPreference.java
Date Sat, 31 May 2008 00:18:27 GMT
Author: srowen
Date: Fri May 30 17:18:27 2008
New Revision: 661920

URL: http://svn.apache.org/viewvc?rev=661920&view=rev
Log:
Added BooleanPreference, plus refactorings to support it, for better support of "binary" yes-or-nothing
preferences in data models

Added:
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/BooleanPreference.java
      - copied, changed from r657697, lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericPreference.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/SettableUserPreference.java
      - copied, changed from r657697, lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericUser.java
Modified:
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericPreference.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericUser.java

Copied: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/BooleanPreference.java
(from r657697, lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericPreference.java)
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/BooleanPreference.java?p2=lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/BooleanPreference.java&p1=lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericPreference.java&r1=657697&r2=661920&rev=661920&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericPreference.java
(original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/BooleanPreference.java
Fri May 30 17:18:27 2008
@@ -24,24 +24,21 @@
 import java.io.Serializable;
 
 /**
- * <p>A simple {@link Preference} encapsulating an {@link Item} and preference value.</p>
+ * Encapsulates a simple boolean "preference" for an {@link Item} whose value does not matter
(is fixed
+ * at 1.0). This is appropriate in situations where users conceptually have only a general
"yes" preference
+ * for items, rather than a spectrum of preference values.
  */
-public class GenericPreference implements Preference, Serializable {
+public final class BooleanPreference implements Preference, SettableUserPreference, Serializable
{
 
   private User user;
   private final Item item;
-  private double value;
 
-  public GenericPreference(User user, Item item, double value) {
+  public BooleanPreference(User user, Item item) {
     if (item == null) {
       throw new IllegalArgumentException("item is null");
     }
-    if (Double.isNaN(value)) {
-      throw new IllegalArgumentException("Invalid value: " + value);
-    }
     this.user = user;
     this.item = item;
-    this.value = value;
   }
 
   public User getUser() {
@@ -51,13 +48,7 @@
     return user;
   }
 
-  /**
-   * <p>Let this be set by {@link GenericUser} to avoid a circularity problem -- each
-   * wants a reference to the other in the constructor.</p>
-   *
-   * @param user user whose preference this is
-   */
-  void setUser(User user) {
+  public void setUser(User user) {
     if (user == null) {
       throw new IllegalArgumentException("user is null");
     }
@@ -69,19 +60,16 @@
   }
 
   public double getValue() {
-    return value;
+    return 1.0;
   }
 
   public void setValue(double value) {
-    if (Double.isNaN(value)) {
-      throw new IllegalArgumentException("Invalid value: " + value);
-    }
-    this.value = value;
+    throw new UnsupportedOperationException();
   }
 
   @Override
   public String toString() {
-    return "GenericPreference[user: " + user + ", item:" + item + ", value:" + value + ']';
+    return "BooleanPreference[user: " + user + ", item:" + item + ']';
   }
 
-}
+}
\ No newline at end of file

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericPreference.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericPreference.java?rev=661920&r1=661919&r2=661920&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericPreference.java
(original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericPreference.java
Fri May 30 17:18:27 2008
@@ -26,7 +26,7 @@
 /**
  * <p>A simple {@link Preference} encapsulating an {@link Item} and preference value.</p>
  */
-public class GenericPreference implements Preference, Serializable {
+public class GenericPreference implements Preference, SettableUserPreference, Serializable
{
 
   private User user;
   private final Item item;
@@ -51,13 +51,7 @@
     return user;
   }
 
-  /**
-   * <p>Let this be set by {@link GenericUser} to avoid a circularity problem -- each
-   * wants a reference to the other in the constructor.</p>
-   *
-   * @param user user whose preference this is
-   */
-  void setUser(User user) {
+  public void setUser(User user) {
     if (user == null) {
       throw new IllegalArgumentException("user is null");
     }

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericUser.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericUser.java?rev=661920&r1=661919&r2=661920&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericUser.java
(original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericUser.java
Fri May 30 17:18:27 2008
@@ -54,8 +54,8 @@
       values = preferences.toArray(new Preference[preferences.size()]);
       for (Preference preference : values) {
         // Is this hacky?
-        if (preference instanceof GenericPreference) {
-          ((GenericPreference) preference).setUser(this);
+        if (preference instanceof SettableUserPreference) {
+          ((SettableUserPreference) preference).setUser(this);
         }
         data.put(preference.getItem().getID(), preference);
       }

Copied: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/SettableUserPreference.java
(from r657697, lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericUser.java)
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/SettableUserPreference.java?p2=lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/SettableUserPreference.java&p1=lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericUser.java&r1=657697&r2=661920&rev=661920&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/GenericUser.java
(original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/SettableUserPreference.java
Fri May 30 17:18:27 2008
@@ -17,85 +17,22 @@
 
 package org.apache.mahout.cf.taste.impl.model;
 
-import org.apache.mahout.cf.taste.impl.common.ArrayIterator;
 import org.apache.mahout.cf.taste.model.Preference;
 import org.apache.mahout.cf.taste.model.User;
 
-import java.io.Serializable;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
 /**
- * <p>A simple {@link User} which has simply an ID and some {@link Collection} of
- * {@link Preference}s.</p>
+ * Simple interface that simply exposes a {@link #setUser(User)} method.
+ * This helps unify implementations of {@link Preference} which expose this method.
  */
-public class GenericUser<K extends Comparable<K>> implements User, Serializable
{
-
-  private static final Preference[] NO_PREFS = new Preference[0];
-
-  private final K id;
-  private final Map<Object, Preference> data;
-  // Use an array for maximum performance
-  private final Preference[] values;
-
-  public GenericUser(K id, Collection<Preference> preferences) {
-    if (id == null) {
-      throw new IllegalArgumentException("id is null");
-    }
-    this.id = id;
-    if (preferences == null || preferences.isEmpty()) {
-      data = Collections.emptyMap();
-      values = NO_PREFS;
-    } else {
-      data = new HashMap<Object, Preference>();
-      values = preferences.toArray(new Preference[preferences.size()]);
-      for (Preference preference : values) {
-        // Is this hacky?
-        if (preference instanceof GenericPreference) {
-          ((GenericPreference) preference).setUser(this);
-        }
-        data.put(preference.getItem().getID(), preference);
-      }
-      Arrays.sort(values, ByItemPreferenceComparator.getInstance());
-    }
-  }
-
-  public K getID() {
-    return id;
-  }
-
-  public Preference getPreferenceFor(Object itemID) {
-    return data.get(itemID);
-  }
-
-  public Iterable<Preference> getPreferences() {
-    return new ArrayIterator<Preference>(values);
-  }
-
-  public Preference[] getPreferencesAsArray() {
-    return values;
-  }
-
-  @Override
-  public int hashCode() {
-    return id.hashCode();
-  }
-
-  @Override
-  public boolean equals(Object obj) {
-    return obj instanceof User && ((User) obj).getID().equals(id);
-  }
-
-  @Override
-  public String toString() {
-    return "User[id:" + String.valueOf(id) + ']';
-  }
+public interface SettableUserPreference extends Preference {
 
-  public int compareTo(User o) {
-    return id.compareTo((K) o.getID());
-  }
+  /**
+   * <p>Let this be set by {@link GenericUser} to avoid a circularity problem -- 
+   * implementations want a reference to a {@link User} in the constructor, but so does
+   * {@link GenericUser}.</p>
+   *
+   * @param user user whose preference this is
+   */
+  void setUser(User user);
 
-}
+}
\ No newline at end of file



Mime
View raw message