@Test public void testHasNCommonPackedSets() { assertThat(hasNCommonItems(packedSet(1L, 3L, 5L, 7L), packedSet(2L, 3L, 4L, 5L, 6L), 1), equalTo(true)); assertThat(hasNCommonItems(packedSet(1L, 3L, 5L, 7L), packedSet(2L, 3L, 4L, 5L, 6L), 2), equalTo(true)); assertThat(hasNCommonItems(packedSet(1L, 3L, 5L, 7L), packedSet(2L, 3L, 4L, 5L, 6L), 3), equalTo(false)); }
@Test public void testHasNCommonSingleton() { assertThat(hasNCommonItems(LongSortedSets.singleton(52), LongSortedSets.singleton(52), 1), equalTo(true)); assertThat(hasNCommonItems(LongSortedSets.singleton(52), LongSortedSets.singleton(42), 1), equalTo(false)); assertThat(hasNCommonItems(LongSortedSets.singleton(42), LongSortedSets.singleton(52), 1), equalTo(false)); assertThat(hasNCommonItems(LongSortedSets.singleton(52), LongSortedSets.singleton(52), 2), equalTo(false)); }
private IdBox<Long2DoubleMap> makeSimilarityRow(IdBox<Long2DoubleSortedMap> item) { long itemId1 = item.getId(); LongIterator itemIter = neighborStrategy.neighborIterator(buildContext, itemId1, itemSimilarity.isSymmetric()); Long2DoubleSortedMap vec1 = item.getValue(); Long2DoubleMap row = new Long2DoubleOpenHashMap(); while (itemIter.hasNext()) { long itemId2 = itemIter.nextLong(); if (itemId1 != itemId2) { Long2DoubleSortedMap vec2 = buildContext.itemVector(itemId2); if (!LongUtils.hasNCommonItems(vec1.keySet(), vec2.keySet(), minCommonUsers)) { // items have insufficient users in common, skip them continue; } double sim = itemSimilarity.similarity(itemId1, vec1, itemId2, vec2); if (threshold.retain(sim)) { row.put(itemId2, sim); } } } return IdBox.create(itemId1, row); }
private IdBox<Long2DoubleMap> makeSimilarityRow(IdBox<Long2DoubleSortedMap> item) { long itemId1 = item.getId(); LongIterator itemIter = neighborStrategy.neighborIterator(buildContext, itemId1, itemSimilarity.isSymmetric()); Long2DoubleSortedMap vec1 = item.getValue(); Long2DoubleMap row = new Long2DoubleOpenHashMap(); while (itemIter.hasNext()) { long itemId2 = itemIter.nextLong(); if (itemId1 != itemId2) { Long2DoubleSortedMap vec2 = buildContext.itemVector(itemId2); if (!LongUtils.hasNCommonItems(vec1.keySet(), vec2.keySet(), minCommonUsers)) { // items have insufficient users in common, skip them continue; } double sim = itemSimilarity.similarity(itemId1, vec1, itemId2, vec2); if (threshold.retain(sim)) { row.put(itemId2, sim); } } } return IdBox.create(itemId1, row); }