NormalizedGoogleDistanceLikeComparator extends TermSimilarityMeasureBase { @Override public double getSimilarity(String term1, String term2) throws SimilarityException { Vector v1 = sourceA.getVector(term1); if (v1 == null) { if (log.isDebugEnabled()) { log.debug("Cutting short on term ["+term1+"] without concept vector"); } return 0.0; } Vector v2 = sourceB.getVector(term2); if (v2 == null) { return 0.0; } long freq1 = 0; long freq2 = 0; long freq1and2 = 0; for (int i=0; i<v1.size(); i++) { if (v1.get(i) > 0 && v2.get(i) > 0) { freq1and2++; freq1++; freq2++; } else if (v1.get(i) > 0) { freq1++; } else if (v2.get(i) > 0) { freq2++; } } System.out.println(freq1); System.out.println(freq2); System.out.println(freq1and2); double logMax = Math.max(Math.log(freq1), Math.log(freq2)); double logMin = Math.min(Math.log(freq1), Math.log(freq2)); double logM = Math.log(v1.size()); return (logMax - Math.log(freq1and2)) / (logM - logMin); } NormalizedGoogleDistanceLikeComparator(VectorReader aIndex);  NormalizedGoogleDistanceLikeComparator(VectorReader aIndexA, VectorReader aIndexB); @Override double getSimilarity(String term1, String term2); @Override String getName();  }
@Test public void ngdComparatorTest() throws SimilarityException { LuceneVectorReader vSrc = new LuceneVectorReader(new File("src/test/resources/vsm/test_index_token")); vSrc.setVectorAggregation(VectorAggregation.CENTROID); vSrc.setWeightingThreshold(0.0f); vSrc.setVectorLengthThreshold(0.0f); vSrc.setWeightingModeTf(WeightingModeTf.binary); vSrc.setWeightingModeIdf(WeightingModeIdf.constantOne); vSrc.setNorm(VectorNorm.NONE); NormalizedGoogleDistanceLikeComparator ngdComparator = new NormalizedGoogleDistanceLikeComparator(vSrc); assertEquals(0.0, ngdComparator.getSimilarity("another", "another"), EPSILON); assertEquals(0.63093, ngdComparator.getSimilarity("another", "example"), EPSILON); assertEquals(1.0, ngdComparator.getSimilarity("example", "sentence"), EPSILON); assertEquals(Double.POSITIVE_INFINITY, ngdComparator.getSimilarity("another", "just"), EPSILON); }