You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2008/08/02 04:49:15 UTC

svn commit: r681930 - in /lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl: common/ correlation/ neighborhood/ recommender/ transforms/

Author: srowen
Date: Fri Aug  1 19:49:15 2008
New Revision: 681930

URL: http://svn.apache.org/viewvc?rev=681930&view=rev
Log:
Refactoring: separate out Cache.Retriever to top level interface and make Cache implement it too for neatness

Added:
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/Retriever.java
Modified:
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/Cache.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/correlation/AveragingPreferenceInferrer.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/NearestNUserNeighborhood.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/ThresholdUserNeighborhood.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/recommender/CachingRecommender.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/transforms/ZScore.java

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/Cache.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/Cache.java?rev=681930&r1=681929&r2=681930&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/Cache.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/Cache.java Fri Aug  1 19:49:15 2008
@@ -29,7 +29,7 @@
  * <p>Thanks to Amila Jayasooriya for helping evaluate performance of the rewrite of this class, as part of a
  * Google Summer of Code 2007 project.</p>
  */
-public final class Cache<K, V> {
+public final class Cache<K, V> implements Retriever<K, V> {
 
   private final FastMap<K, V> cache;
   private final Retriever<? super K, ? extends V> retriever;
@@ -99,7 +99,7 @@
   }
 
   private V getAndCacheValue(K key) throws TasteException {
-    V value = retriever.getValue(key);
+    V value = retriever.get(key);
     synchronized (cache) {
       cache.put(key, value);
     }
@@ -111,17 +111,4 @@
     return "Cache[retriever:" + retriever + ']';
   }
 
-  /**
-   * <p>Implementations can retrieve a value for a given key.</p>
-   */
-  public static interface Retriever<KK, VV> {
-
-    /**
-     * @param key key for which a value should be retrieved
-     * @return value for key
-     * @throws TasteException if an error occurs while retrieving the value
-     */
-    VV getValue(KK key) throws TasteException;
-  }
-
 }

Added: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/Retriever.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/Retriever.java?rev=681930&view=auto
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/Retriever.java (added)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/Retriever.java Fri Aug  1 19:49:15 2008
@@ -0,0 +1,34 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mahout.cf.taste.impl.common;
+
+import org.apache.mahout.cf.taste.common.TasteException;
+
+/**
+ * <p>Implementations can retrieve a value for a given key.</p>
+ */
+public interface Retriever<K,V> {
+
+  /**
+   * @param key key for which a value should be retrieved
+   * @return value for key
+   * @throws TasteException if an error occurs while retrieving the value
+   */
+  V get(K key) throws TasteException;
+
+}

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/correlation/AveragingPreferenceInferrer.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/correlation/AveragingPreferenceInferrer.java?rev=681930&r1=681929&r2=681930&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/correlation/AveragingPreferenceInferrer.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/correlation/AveragingPreferenceInferrer.java Fri Aug  1 19:49:15 2008
@@ -22,6 +22,7 @@
 import org.apache.mahout.cf.taste.impl.common.FullRunningAverage;
 import org.apache.mahout.cf.taste.impl.common.RunningAverage;
 import org.apache.mahout.cf.taste.impl.common.Cache;
+import org.apache.mahout.cf.taste.impl.common.Retriever;
 import org.apache.mahout.cf.taste.model.DataModel;
 import org.apache.mahout.cf.taste.model.Item;
 import org.apache.mahout.cf.taste.model.Preference;
@@ -34,7 +35,7 @@
  */
 public final class AveragingPreferenceInferrer implements PreferenceInferrer {
 
-  private static final Cache.Retriever<User, Double> RETRIEVER = new PrefRetriever();
+  private static final Retriever<User, Double> RETRIEVER = new PrefRetriever();
 
   private final Cache<User, Double> averagePreferenceValue;
 
@@ -54,9 +55,9 @@
     averagePreferenceValue.clear();
   }
 
-  private static final class PrefRetriever implements Cache.Retriever<User, Double> {
+  private static final class PrefRetriever implements Retriever<User, Double> {
 
-    public Double getValue(User key) {
+    public Double get(User key) {
       RunningAverage average = new FullRunningAverage();
       Preference[] prefs = key.getPreferencesAsArray();
       if (prefs.length == 0) {

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/NearestNUserNeighborhood.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/NearestNUserNeighborhood.java?rev=681930&r1=681929&r2=681930&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/NearestNUserNeighborhood.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/NearestNUserNeighborhood.java Fri Aug  1 19:49:15 2008
@@ -84,7 +84,7 @@
   }
 
 
-  private final class Retriever implements Cache.Retriever<Object, Collection<User>> {
+  private final class Retriever implements org.apache.mahout.cf.taste.impl.common.Retriever<Object, Collection<User>> {
 
     private final int n;
 
@@ -92,7 +92,7 @@
       this.n = n;
     }
 
-    public Collection<User> getValue(Object key) throws TasteException {
+    public Collection<User> get(Object key) throws TasteException {
       log.trace("Computing neighborhood around user ID '{}'", key);
 
       DataModel dataModel = getDataModel();

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/ThresholdUserNeighborhood.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/ThresholdUserNeighborhood.java?rev=681930&r1=681929&r2=681930&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/ThresholdUserNeighborhood.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/ThresholdUserNeighborhood.java Fri Aug  1 19:49:15 2008
@@ -87,7 +87,7 @@
   }
 
 
-  private final class Retriever implements Cache.Retriever<Object, Collection<User>> {
+  private final class Retriever implements org.apache.mahout.cf.taste.impl.common.Retriever<Object, Collection<User>> {
 
     private final double threshold;
 
@@ -95,7 +95,7 @@
       this.threshold = threshold;
     }
 
-    public Collection<User> getValue(Object key) throws TasteException {
+    public Collection<User> get(Object key) throws TasteException {
       log.trace("Computing neighborhood around user ID '{}'", key);
 
       DataModel dataModel = getDataModel();

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/recommender/CachingRecommender.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/recommender/CachingRecommender.java?rev=681930&r1=681929&r2=681930&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/recommender/CachingRecommender.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/recommender/CachingRecommender.java Fri Aug  1 19:49:15 2008
@@ -20,6 +20,7 @@
 import org.apache.mahout.cf.taste.common.TasteException;
 import org.apache.mahout.cf.taste.impl.common.Pair;
 import org.apache.mahout.cf.taste.impl.common.Cache;
+import org.apache.mahout.cf.taste.impl.common.Retriever;
 import org.apache.mahout.cf.taste.model.DataModel;
 import org.apache.mahout.cf.taste.model.Item;
 import org.apache.mahout.cf.taste.recommender.RecommendedItem;
@@ -154,7 +155,7 @@
     return "CachingRecommender[recommender:" + recommender + ']';
   }
 
-  private static final class RecommendationRetriever implements Cache.Retriever<Object, Recommendations> {
+  private static final class RecommendationRetriever implements Retriever<Object, Recommendations> {
 
     private final Recommender recommender;
     private final AtomicInteger maxHowMany;
@@ -164,13 +165,13 @@
       this.maxHowMany = maxHowMany;
     }
 
-    public Recommendations getValue(Object key) throws TasteException {
+    public Recommendations get(Object key) throws TasteException {
       log.debug("Retrieving new recommendations for user ID '{}'", key);
       return new Recommendations(Collections.unmodifiableList(recommender.recommend(key, maxHowMany.get())));
     }
   }
 
-  private static final class EstimatedPrefRetriever implements Cache.Retriever<Pair<?, ?>, Double> {
+  private static final class EstimatedPrefRetriever implements Retriever<Pair<?, ?>, Double> {
 
     private final Recommender recommender;
 
@@ -178,7 +179,7 @@
       this.recommender = recommender;
     }
 
-    public Double getValue(Pair<?, ?> key) throws TasteException {
+    public Double get(Pair<?, ?> key) throws TasteException {
       Object userID = key.getFirst();
       Object itemID = key.getSecond();
       log.debug("Retrieving estimated preference for user ID '{}' and item ID '{}'", userID, itemID);

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/transforms/ZScore.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/transforms/ZScore.java?rev=681930&r1=681929&r2=681930&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/transforms/ZScore.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/transforms/ZScore.java Fri Aug  1 19:49:15 2008
@@ -21,6 +21,7 @@
 import org.apache.mahout.cf.taste.impl.common.FullRunningAverageAndStdDev;
 import org.apache.mahout.cf.taste.impl.common.RunningAverageAndStdDev;
 import org.apache.mahout.cf.taste.impl.common.Cache;
+import org.apache.mahout.cf.taste.impl.common.Retriever;
 import org.apache.mahout.cf.taste.model.Preference;
 import org.apache.mahout.cf.taste.model.User;
 import org.apache.mahout.cf.taste.transforms.PreferenceTransform;
@@ -65,9 +66,9 @@
     return "ZScore";
   }
 
-  private static class MeanStdevRetriever implements Cache.Retriever<User, RunningAverageAndStdDev> {
+  private static class MeanStdevRetriever implements Retriever<User, RunningAverageAndStdDev> {
 
-    public RunningAverageAndStdDev getValue(User user) throws TasteException {
+    public RunningAverageAndStdDev get(User user) throws TasteException {
       RunningAverageAndStdDev running = new FullRunningAverageAndStdDev();
       Preference[] prefs = user.getPreferencesAsArray();
       for (int i = 0; i < prefs.length; i++) {