WuPalmerComparator extends PathBasedComparator { @Override public double getSimilarity(Entity e1, Entity e2) throws SimilarityException, LexicalSemanticResourceException { if (e1.equals(e2)) { return 1.0; } Entity lcs = null; try { lcs = getEntityGraph().getLCS(root, e1, e2); } catch (UnsupportedOperationException e) { throw new LexicalSemanticResourceException(e); } if (lcs == null) { return NO_PATH; } double depthLCS = getEntityGraph().getShortestPathLength(root, lcs, DirectionMode.undirected); double pl1 = getEntityGraph().getShortestPathLength(e1, lcs, DirectionMode.undirected) + 1; double pl2 = getEntityGraph().getShortestPathLength(e2, lcs, DirectionMode.undirected) + 1; double nominator = 2 * depthLCS; double denominator = pl1 + pl2 + 2 * depthLCS; double relatedness = nominator / denominator; return relatedness; } WuPalmerComparator(LexicalSemanticResource lexSemResource);  WuPalmerComparator(LexicalSemanticResource lexSemResource, Entity root); @Override double getSimilarity(Entity e1, Entity e2);  }
@Ignore("There seems to be something wrong with the test database. Error: No category with name Anime-influenced_animatio_(non-commercial) was found.") @Test public void testWuPalmerWikipediaCategory() throws Exception { LexicalSemanticResource wikiResource = ResourceFactory.getInstance().get("wikipedia_category", "test"); LexSemResourceComparator comparator = new WuPalmerComparator(wikiResource); Set<Entity> entitiesAqua = wikiResource.getEntity("AQUA"); Set<Entity> entitiesSir = wikiResource.getEntity("SIR"); Set<Entity> entitiesTk = wikiResource.getEntity("Telecooperation"); assertTrue("AQUA", entitiesAqua.size() > 0); assertTrue("SIR", entitiesSir.size() > 0); assertTrue("Tk", entitiesTk.size() > 0); assertEquals(1.0, comparator.getSimilarity(entitiesAqua, entitiesAqua), epsilon); assertEquals(0.6, comparator.getSimilarity(entitiesAqua, entitiesSir), epsilon); assertEquals(0.0, comparator.getSimilarity(entitiesAqua, entitiesTk), epsilon); assertEquals(comparator.getSimilarity(entitiesTk, entitiesAqua), comparator.getSimilarity(entitiesAqua, entitiesTk), epsilon); }
@Test public void testWuPalmerWordnet() throws Exception { Assume.assumeTrue(Runtime.getRuntime().maxMemory() >= 2000000000); LexicalSemanticResource wordnet = ResourceFactory.getInstance().get("wordnet3", "en"); wordnet.setIsCaseSensitive(false); LexSemResourceComparator comparator = new WuPalmerComparator(wordnet, wordnet.getRoot()); assertEquals("WuPalmerComparator", comparator.getName()); Set<Entity> entities1 = wordnet.getEntity("gem"); Set<Entity> entities2 = wordnet.getEntity("jewel"); assertTrue("gem", entities1.size() > 0); assertTrue("jewel", entities2.size() > 0); assertEquals(1.0, comparator.getSimilarity(entities1, entities2), epsilon); assertEquals(1.0, comparator.getSimilarity(entities2, entities1), epsilon); }