mahout-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sro...@apache.org
Subject svn commit: r807309 - /lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/similarity/GenericItemSimilarity.java
Date Mon, 24 Aug 2009 17:35:02 GMT
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;
     }
 



Mime
View raw message