IntersectionSimilarity implements
    SimilarityAlgorithm<IntersectionSimilarityRequest, SimilarityResponse> { public IntersectionSimilarity(BipartiteGraph bipartiteGraph, RelatedTweetUpdateNormalization relatedTweetUpdateNormalization, StatsReceiver statsReceiver) { this.bipartiteGraph = bipartiteGraph; this.relatedTweetUpdateNormalization = relatedTweetUpdateNormalization; StatsReceiver scopedStatsReceiver = statsReceiver.scope(this.getClass().getSimpleName()); this.queryNodeDegreeIsZeroCounter = scopedStatsReceiver.counter("lessThanMaxNumNeighbors"); this.lessThanMaxNumNeighborsCounter = scopedStatsReceiver.counter("lessThanMaxNumNeighbors"); this.moreThanMaxNumNeighborsCounter = scopedStatsReceiver.counter("moreThanMaxNumNeighbors"); this.queryNodeNullIteratorCounter = scopedStatsReceiver.counter("queryNodeNullIterator"); } IntersectionSimilarity(BipartiteGraph bipartiteGraph,
      RelatedTweetUpdateNormalization relatedTweetUpdateNormalization,
      StatsReceiver statsReceiver); @Override SimilarityResponse getSimilarNodes(IntersectionSimilarityRequest request, Random random); @Override SimilarityResponse getSimilarNodes(IntersectionSimilarityRequest request, Random random,
                                            RelatedTweetFilterChain filterChain);  }
@Test public void testIntersectionSimilarity() throws Exception { BipartiteGraph bipartiteGraph = BipartiteGraphTestHelper.buildSmallTestBipartiteGraphSegment(); long queryNode = 2; int numResults = 2; LongSet seedSet = new LongOpenHashSet(new long[]{3}); int maxNumNeighbors = 100; int minNeighborDegree = 2; int maxNumSamplesPerNeighbor = 100; int minCooccurrence = 1; int minDegree = 2; double maxLowerMultiplicativeDeviation = 5.0; double maxUpperMultiplicativeDeviation = 5.0; long randomSeed = 5298057403198457L; IntersectionSimilarityRequest intersectionSimilarityRequest = new IntersectionSimilarityRequest( queryNode, numResults, seedSet, maxNumNeighbors, minNeighborDegree, maxNumSamplesPerNeighbor, minCooccurrence, minDegree, maxLowerMultiplicativeDeviation, maxUpperMultiplicativeDeviation, false); final List<SimilarityInfo> expectedCosineSimilarityResults = Lists.newArrayList( new SimilarityInfo(2, 2.1213203435596424, 3, 2), new SimilarityInfo(3, 2.0, 2, 1) ); RelatedTweetUpdateNormalization cosineUpdateNormalization = new CosineUpdateNormalization(); IntersectionSimilarity cosineSimilarity = new IntersectionSimilarity(bipartiteGraph, cosineUpdateNormalization, new NullStatsReceiver()); Random random = new Random(randomSeed); SimilarityResponse similarityResponse = cosineSimilarity.getSimilarNodes(intersectionSimilarityRequest, random); List<SimilarityInfo> cosineSimilarityResults = Lists.newArrayList(similarityResponse.getRankedSimilarNodes()); assertEquals(expectedCosineSimilarityResults, cosineSimilarityResults); final List<SimilarityInfo> expectedFilteredCosineSimilarityResults = Lists.newArrayList( new SimilarityInfo(5, 1.7320508075688776, 3, 3) ); MinEngagementFilter minEngagementFilter = new MinEngagementFilter(3, bipartiteGraph, new NullStatsReceiver()); ArrayList<RelatedTweetFilter> filters = new ArrayList<RelatedTweetFilter>(1); filters.add(minEngagementFilter); RelatedTweetFilterChain filterChain = new RelatedTweetFilterChain(filters); SimilarityResponse filteredSimilarityResponse = cosineSimilarity.getSimilarNodes(intersectionSimilarityRequest, random, filterChain); List<SimilarityInfo> filteredCosineSimilarityResults = Lists.newArrayList(filteredSimilarityResponse.getRankedSimilarNodes()); assertEquals(expectedFilteredCosineSimilarityResults, filteredCosineSimilarityResults); }