Author: srowen
Date: Thu Aug 7 20:36:55 2008
New Revision: 683829
URL: http://svn.apache.org/viewvc?rev=683829&view=rev
Log:
Improving caching possibilities by adding a caching wrapper for correlations
Added:
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/correlation/CachingItemCorrelation.java
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/correlation/CachingUserCorrelation.java
Added:
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/correlation/CachingItemCorrelation.java
URL:
http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/correlation/CachingItemCorrelation.java?rev=683829&view=auto
==============================================================================
---
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/correlation/CachingItemCorrelation.java
(added)
+++
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/correlation/CachingItemCorrelation.java
Thu Aug 7 20:36:55 2008
@@ -0,0 +1,68 @@
+/**
+ * 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.correlation;
+
+import org.apache.mahout.cf.taste.correlation.ItemCorrelation;
+import org.apache.mahout.cf.taste.model.Item;
+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;
+
+/**
+ * Caches the results from an underlying [EMAIL PROTECTED] ItemCorrelation}
implementation.
+ */
+public final class CachingItemCorrelation implements ItemCorrelation {
+
+ private final ItemCorrelation correlation;
+ private final Cache<Pair<Item, Item>, Double> correlationCache;
+
+ public CachingItemCorrelation(ItemCorrelation correlation) {
+ if (correlation == null) {
+ throw new IllegalArgumentException("correlation is null");
+ }
+ this.correlation = correlation;
+ this.correlationCache = new Cache<Pair<Item, Item>, Double>(new
CorrelationRetriever(correlation));
+ }
+
+ public double itemCorrelation(Item item1, Item item2) throws TasteException {
+ Pair<Item, Item> key;
+ if (item1.compareTo(item2) < 0) {
+ key = new Pair<Item, Item>(item1, item2);
+ } else {
+ key = new Pair<Item, Item>(item2, item1);
+ }
+ return correlationCache.get(key);
+ }
+
+ public void refresh() {
+ correlationCache.clear();
+ correlation.refresh();
+ }
+
+ private static final class CorrelationRetriever implements
Retriever<Pair<Item, Item>, Double> {
+ private final ItemCorrelation correlation;
+ private CorrelationRetriever(ItemCorrelation correlation) {
+ this.correlation = correlation;
+ }
+ public Double get(Pair<Item, Item> key) throws TasteException {
+ return correlation.itemCorrelation(key.getFirst(), key.getSecond());
+ }
+ }
+
+}
\ No newline at end of file
Added:
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/correlation/CachingUserCorrelation.java
URL:
http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/correlation/CachingUserCorrelation.java?rev=683829&view=auto
==============================================================================
---
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/correlation/CachingUserCorrelation.java
(added)
+++
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/correlation/CachingUserCorrelation.java
Thu Aug 7 20:36:55 2008
@@ -0,0 +1,74 @@
+/**
+ * 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.correlation;
+
+import org.apache.mahout.cf.taste.correlation.UserCorrelation;
+import org.apache.mahout.cf.taste.correlation.PreferenceInferrer;
+import org.apache.mahout.cf.taste.model.User;
+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;
+
+/**
+ * Caches the results from an underlying [EMAIL PROTECTED] UserCorrelation}
implementation.
+ */
+public final class CachingUserCorrelation implements UserCorrelation {
+
+ private final UserCorrelation correlation;
+ private final Cache<Pair<User, User>, Double> correlationCache;
+
+ public CachingUserCorrelation(UserCorrelation correlation) {
+ if (correlation == null) {
+ throw new IllegalArgumentException("correlation is null");
+ }
+ this.correlation = correlation;
+ this.correlationCache = new Cache<Pair<User, User>, Double>(new
CorrelationRetriever(correlation));
+ }
+
+ public double userCorrelation(User user1, User user2) throws TasteException {
+ Pair<User, User> key;
+ if (user1.compareTo(user2) < 0) {
+ key = new Pair<User, User>(user1, user2);
+ } else {
+ key = new Pair<User, User>(user2, user1);
+ }
+ return correlationCache.get(key);
+ }
+
+ public void setPreferenceInferrer(PreferenceInferrer inferrer) {
+ correlationCache.clear();
+ correlation.setPreferenceInferrer(inferrer);
+ }
+
+ public void refresh() {
+ correlationCache.clear();
+ correlation.refresh();
+ }
+
+ private static final class CorrelationRetriever implements
Retriever<Pair<User, User>, Double> {
+ private final UserCorrelation correlation;
+ private CorrelationRetriever(UserCorrelation correlation) {
+ this.correlation = correlation;
+ }
+ public Double get(Pair<User, User> key) throws TasteException {
+ return correlation.userCorrelation(key.getFirst(), key.getSecond());
+ }
+ }
+
+}