mahout-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sro...@apache.org
Subject svn commit: r731349 - /lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/
Date Sun, 04 Jan 2009 20:04:04 GMT
Author: srowen
Date: Sun Jan  4 12:04:03 2009
New Revision: 731349

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

Added:
    lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/GeoUserSimilarity.java
Modified:
    lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingDataModel.java
    lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommender.java
    lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderBuilder.java
    lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderEvaluatorRunner.java
    lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingUser.java

Modified: lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingDataModel.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingDataModel.java?rev=731349&r1=731348&r2=731349&view=diff
==============================================================================
--- lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingDataModel.java
(original)
+++ lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingDataModel.java
Sun Jan  4 12:04:03 2009
@@ -145,8 +145,20 @@
       throw new NoSuchElementException();
     }
     String location = userData[0];
+    String[] locationTokens = location.split(",");
+    for (int i = 0; i < locationTokens.length; i++) {
+      if (locationTokens[i] != null) {
+        locationTokens[i] = locationTokens[i].trim();
+        if (locationTokens[i].length() == 0) {
+          locationTokens[i] = null;
+        }
+      }
+    }
+    String city = locationTokens[0];
+    String state = locationTokens.length > 1 ? locationTokens[1] : null;
+    String country = locationTokens.length > 2 ? locationTokens[2] : null;
     Integer age = userData[1] == null ? null : Integer.valueOf(userData[1]);
-    return new BookCrossingUser(id, prefs, location, age);
+    return new BookCrossingUser(id, prefs, city, state, country, age);
   }
 
   @Override

Modified: lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommender.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommender.java?rev=731349&r1=731348&r2=731349&view=diff
==============================================================================
--- lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommender.java
(original)
+++ lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommender.java
Sun Jan  4 12:04:03 2009
@@ -22,7 +22,6 @@
 import org.apache.mahout.cf.taste.impl.recommender.CachingRecommender;
 import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
 import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
-import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
 import org.apache.mahout.cf.taste.model.DataModel;
 import org.apache.mahout.cf.taste.model.Item;
 import org.apache.mahout.cf.taste.recommender.RecommendedItem;
@@ -31,7 +30,6 @@
 import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
 import org.apache.mahout.cf.taste.similarity.UserSimilarity;
 
-import java.io.IOException;
 import java.util.Collection;
 import java.util.List;
 
@@ -43,12 +41,8 @@
 
   private final Recommender recommender;
 
-  public BookCrossingRecommender() throws IOException, TasteException {
-    this(new BookCrossingDataModel());
-  }
-
-  public BookCrossingRecommender(DataModel dataModel) throws TasteException {
-    UserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
+  public BookCrossingRecommender(DataModel dataModel, BookCrossingDataModel bcModel) throws
TasteException {
+    UserSimilarity similarity = new GeoUserSimilarity(bcModel);
     UserNeighborhood neighborhood = new NearestNUserNeighborhood(5, similarity, dataModel);
     recommender = new CachingRecommender(new GenericUserBasedRecommender(dataModel, neighborhood,
similarity));
   }

Modified: lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderBuilder.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderBuilder.java?rev=731349&r1=731348&r2=731349&view=diff
==============================================================================
--- lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderBuilder.java
(original)
+++ lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderBuilder.java
Sun Jan  4 12:04:03 2009
@@ -24,9 +24,15 @@
 
 final class BookCrossingRecommenderBuilder implements RecommenderBuilder {
 
+  private final BookCrossingDataModel bcModel;
+
+  BookCrossingRecommenderBuilder(BookCrossingDataModel bcModel) {
+    this.bcModel = bcModel;
+  }
+
   @Override
   public Recommender buildRecommender(DataModel dataModel) throws TasteException {
-    return new BookCrossingRecommender(dataModel);
+    return new BookCrossingRecommender(dataModel, bcModel);
   }
 
 }
\ No newline at end of file

Modified: lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderEvaluatorRunner.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderEvaluatorRunner.java?rev=731349&r1=731348&r2=731349&view=diff
==============================================================================
--- lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderEvaluatorRunner.java
(original)
+++ lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingRecommenderEvaluatorRunner.java
Sun Jan  4 12:04:03 2009
@@ -39,8 +39,9 @@
 
   public static void main(String... args) throws IOException, TasteException {
     RecommenderEvaluator evaluator = new AverageAbsoluteDifferenceRecommenderEvaluator();
-    double evaluation = evaluator.evaluate(new BookCrossingRecommenderBuilder(),
-                                                 new BookCrossingDataModel(),
+    BookCrossingDataModel model = new BookCrossingDataModel();
+    double evaluation = evaluator.evaluate(new BookCrossingRecommenderBuilder(model),
+                                                 model,
                                                  0.9,
                                                  0.1);
     log.info(String.valueOf(evaluation));

Modified: lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingUser.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingUser.java?rev=731349&r1=731348&r2=731349&view=diff
==============================================================================
--- lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingUser.java
(original)
+++ lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/BookCrossingUser.java
Sun Jan  4 12:04:03 2009
@@ -24,17 +24,29 @@
 
 final class BookCrossingUser extends GenericUser<String> {
 
-  private final String location;
+  private final String city;
+  private final String state;
+  private final String country;
   private final Integer age;
 
-  BookCrossingUser(String id, List<Preference> prefs, String location, Integer age)
{
+  BookCrossingUser(String id, List<Preference> prefs, String city, String state, String
country, Integer age) {
     super(id, prefs);
-    this.location = location;
+    this.city = city;
+    this.state = state;
+    this.country = country;
     this.age = age;
   }
 
-  String getLocation() {
-    return location;
+  String getCity() {
+    return city;
+  }
+
+  String getState() {
+    return state;
+  }
+
+  String getCountry() {
+    return country;
   }
 
   Integer getAge() {

Added: lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/GeoUserSimilarity.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/GeoUserSimilarity.java?rev=731349&view=auto
==============================================================================
--- lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/GeoUserSimilarity.java
(added)
+++ lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/cf/taste/example/bookcrossing/GeoUserSimilarity.java
Sun Jan  4 12:04:03 2009
@@ -0,0 +1,66 @@
+/**
+ * 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.example.bookcrossing;
+
+import org.apache.mahout.cf.taste.similarity.UserSimilarity;
+import org.apache.mahout.cf.taste.similarity.PreferenceInferrer;
+import org.apache.mahout.cf.taste.model.User;
+import org.apache.mahout.cf.taste.common.Refreshable;
+import org.apache.mahout.cf.taste.common.TasteException;
+
+import java.util.Collection;
+
+public final class GeoUserSimilarity implements UserSimilarity {
+
+  private final BookCrossingDataModel model;
+
+  public GeoUserSimilarity(BookCrossingDataModel model) {
+    this.model = model;
+  }
+
+  @Override
+  public double userSimilarity(User user1, User user2) throws TasteException {
+    BookCrossingUser bcUser1 = user1 instanceof BookCrossingUser ? (BookCrossingUser) user1
: (BookCrossingUser) model.getUser(user1.getID());
+    BookCrossingUser bcUser2 = user2 instanceof BookCrossingUser ? (BookCrossingUser) user2
: (BookCrossingUser) model.getUser(user2.getID());
+    if (notNullAndEqual(bcUser1.getCity(), bcUser2.getCity())) {
+      return 1.0;
+    }
+    if (notNullAndEqual(bcUser1.getState(), bcUser2.getState())) {
+      return 0.5;
+    }
+    if (notNullAndEqual(bcUser1.getCountry(), bcUser2.getCountry())) {
+      return 0.1;
+    }
+    return 0.0;
+  }
+
+  private static boolean notNullAndEqual(Object o1, Object o2) {
+    return o1 != null && o1.equals(o2);
+  }
+
+  @Override
+  public void setPreferenceInferrer(PreferenceInferrer inferrer) {
+    // do nothing
+  }
+
+  @Override
+  public void refresh(Collection<Refreshable> alreadyRefreshed) {
+    // do nothing
+  }
+
+}



Mime
View raw message