TreeClusteringRecommender extends AbstractRecommender implements ClusteringRecommender { @Override public List<RecommendedItem> recommend(long userID, int howMany, IDRescorer rescorer) throws TasteException { Preconditions.checkArgument(howMany >= 1, "howMany must be at least 1"); buildClusters(); log.debug("Recommending items for user ID '{}'", userID); List<RecommendedItem> recommended = topRecsByUserID.get(userID); if (recommended == null) { return Collections.emptyList(); } DataModel dataModel = getDataModel(); List<RecommendedItem> rescored = Lists.newArrayListWithCapacity(recommended.size()); for (RecommendedItem recommendedItem : recommended) { long itemID = recommendedItem.getItemID(); if (rescorer != null && rescorer.isFiltered(itemID)) { continue; } if (dataModel.getPreferenceValue(userID, itemID) == null && (rescorer == null || !Double.isNaN(rescorer.rescore(itemID, recommendedItem.getValue())))) { rescored.add(recommendedItem); } } Collections.sort(rescored, new ByRescoreComparator(rescorer)); return rescored; } TreeClusteringRecommender(DataModel dataModel, ClusterSimilarity clusterSimilarity, int numClusters);  TreeClusteringRecommender(DataModel dataModel,
                                   ClusterSimilarity clusterSimilarity,
                                   int numClusters,
                                   double samplingRate);  TreeClusteringRecommender(DataModel dataModel,
                                   ClusterSimilarity clusterSimilarity,
                                   double clusteringThreshold);  TreeClusteringRecommender(DataModel dataModel,
                                   ClusterSimilarity clusterSimilarity,
                                   double clusteringThreshold,
                                   double samplingRate); @Override List<RecommendedItem> recommend(long userID, int howMany, IDRescorer rescorer); @Override float estimatePreference(long userID, long itemID); @Override FastIDSet getCluster(long userID); @Override FastIDSet[] getClusters(); @Override void refresh(Collection<Refreshable> alreadyRefreshed); @Override String toString();  }
@Test public void testRescorer() throws Exception { DataModel dataModel = getDataModel( new long[] {1, 2, 3}, new Double[][] { {0.1, 0.2}, {0.2, 0.3, 0.3, 0.6}, {0.4, 0.4, 0.5, 0.9}, }); UserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel); ClusterSimilarity clusterSimilarity = new FarthestNeighborClusterSimilarity(similarity); Recommender recommender = new TreeClusteringRecommender(dataModel, clusterSimilarity, 2); List<RecommendedItem> originalRecommended = recommender.recommend(1, 2); List<RecommendedItem> rescoredRecommended = recommender.recommend(1, 2, new ReversingRescorer<Long>()); assertNotNull(originalRecommended); assertNotNull(rescoredRecommended); assertEquals(2, originalRecommended.size()); assertEquals(2, rescoredRecommended.size()); assertEquals(originalRecommended.get(0).getItemID(), rescoredRecommended.get(1).getItemID()); assertEquals(originalRecommended.get(1).getItemID(), rescoredRecommended.get(0).getItemID()); }
@Test public void testBestRating() throws Exception { DataModel dataModel = getDataModel( new long[] {1, 2, 3, 4}, new Double[][] { {0.1, 0.3}, {0.2, 0.3, 0.3}, {0.4, 0.3, 0.5}, {0.7, 0.3, 0.8}, }); UserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel); ClusterSimilarity clusterSimilarity = new FarthestNeighborClusterSimilarity(similarity); Recommender recommender = new TreeClusteringRecommender(dataModel, clusterSimilarity, 2); List<RecommendedItem> recommended = recommender.recommend(1, 1); assertNotNull(recommended); assertEquals(1, recommended.size()); RecommendedItem firstRecommended = recommended.get(0); assertEquals(2, firstRecommended.getItemID()); assertEquals(0.3, firstRecommended.getValue(), EPSILON); }