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 2009/08/24 19:35:02 UTC

svn commit: r807309 - /lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/similarity/GenericItemSimilarity.java

Author: srowen
Date: Mon Aug 24 17:35:01 2009
New Revision: 807309

URL: http://svn.apache.org/viewvc?rev=807309&view=rev
Log:
Bug fix: don't return an ItemItemSimilarity from iterator whose value is NaN. And generally improve the iterator.

Modified:
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/similarity/GenericItemSimilarity.java

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/similarity/GenericItemSimilarity.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/similarity/GenericItemSimilarity.java?rev=807309&r1=807308&r2=807309&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/similarity/GenericItemSimilarity.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/similarity/GenericItemSimilarity.java Mon Aug 24 17:35:01 2009
@@ -244,45 +244,54 @@
 
     private final ItemSimilarity otherSimilarity;
     private final long[] itemIDs;
-    private final int size;
     private int i;
     private long itemID1;
     private int j;
+    private ItemItemSimilarity next;
 
     private DataModelSimilaritiesIterator(ItemSimilarity otherSimilarity, long[] itemIDs) {
       this.otherSimilarity = otherSimilarity;
       this.itemIDs = itemIDs;
-      this.size = itemIDs.length;
       i = 0;
       itemID1 = itemIDs[0];
       j = 1;
+      goToNext();
+    }
+
+    private void goToNext() {
+      next = null;
+      int size = itemIDs.length;
+      while (next == null && i < size - 1) {
+        long itemID2 = itemIDs[j];
+        double similarity;
+        try {
+          similarity = otherSimilarity.itemSimilarity(itemID1, itemID2);
+        } catch (TasteException te) {
+          // ugly:
+          throw new RuntimeException(te);
+        }
+        if (!Double.isNaN(similarity)) {
+          next = new ItemItemSimilarity(itemID1, itemID2, similarity);
+        }
+        if (++j == size) {
+          itemID1 = itemIDs[++i];
+          j = i + 1;
+        }
+      }
     }
 
     @Override
     public boolean hasNext() {
-      return i < size - 1;
+      return next != null;
     }
 
     @Override
     public ItemItemSimilarity next() {
-      if (!hasNext()) {
+      if (next == null) {
         throw new NoSuchElementException();
       }
-      long itemID2 = itemIDs[j];
-      double similarity;
-      try {
-        similarity = otherSimilarity.itemSimilarity(itemID1, itemID2);
-      } catch (TasteException te) {
-        // ugly:
-        throw new RuntimeException(te);
-      }
-      ItemItemSimilarity result = new ItemItemSimilarity(itemID1, itemID2, similarity);
-      j++;
-      if (j == size) {
-        i++;
-        itemID1 = itemIDs[i];
-        j = i + 1;
-      }
+      ItemItemSimilarity result = next;
+      goToNext();
       return result;
     }