CosineSimilarity implements Similarity { @Override public double computeScore(Collection<TermId> query, Collection<TermId> target) { final Set<TermId> termIdsQuery = ontology.getAllAncestorTermIds(query, false); final Set<TermId> termIdsTarget = ontology.getAllAncestorTermIds(target, false); return Sets.intersection(termIdsQuery, termIdsTarget).size() / (Math.sqrt(termIdsQuery.size()) * Math.sqrt(termIdsTarget.size())); } CosineSimilarity(Ontology ontology);  CosineSimilarity(Ontology ontology, boolean oppositeAware); @Override String getName(); @Override String getParameters(); @Override boolean isSymmetric(); @Override double computeScore(Collection<TermId> query, Collection<TermId> target);  }
@Test void testComputeSimilarities() { assertEquals( 0.408, similarity.computeScore(Lists.newArrayList(idBeet), Lists.newArrayList(idCarrot)), 0.01); assertEquals( 0.816, similarity.computeScore(Lists.newArrayList(idBlueCarrot), Lists.newArrayList(idCarrot)), 0.01); assertEquals( 0.50, similarity.computeScore(Lists.newArrayList(idPumpkin), Lists.newArrayList(idCarrot)), 0.01); assertEquals( 0.0, similarity.computeScore(Lists.newArrayList(idLeafVegetable), Lists.newArrayList(idCarrot)), 0.01); }