Skip to content

Commit

Permalink
update MITWordNet
Browse files Browse the repository at this point in the history
  • Loading branch information
dmeoli committed Sep 4, 2018
1 parent 9517b8d commit f8d1d04
Show file tree
Hide file tree
Showing 16 changed files with 34 additions and 71 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ and declare this github repo as a dependency:
<dependency>
<groupId>com.github.DonatoMeoli</groupId>
<artifactId>WS4J</artifactId>
<version>-SNAPSHOT</version>
<version>x.y.z</version>
</dependency>
</dependencies>

Expand Down
24 changes: 0 additions & 24 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,6 @@
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>

<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>edu.uniba.di.lacam.kdde.ws4j.demo.SimilarityCalculationDemo</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>

<resources>
<resource>
<directory>${project.basedir}/src/main/config</directory>
Expand Down
41 changes: 18 additions & 23 deletions src/main/java/edu/uniba/di/lacam/kdde/lexical_db/MITWordNet.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@

public class MITWordNet implements ILexicalDatabase {

private static final String WORDNET_PATH = System.getProperty("user.dir") + File.separator + "dict";

private static IRAMDictionary dict;
private static ConcurrentMap<String, List<String>> cache;

private static final String WORDNET_PATH = System.getProperty("user.dir") + File.separator + "dict";
private static final ILexicalDatabase db = new MITWordNet();

public MITWordNet(IRAMDictionary dict) {
MITWordNet.dict = dict;
}

public MITWordNet() {
private MITWordNet() {
try {
if (WS4JConfiguration.getInstance().useMemoryDB()) {
Log.info("Loading WordNet into memory...");
Expand All @@ -49,8 +47,8 @@ public MITWordNet() {
if (WS4JConfiguration.getInstance().useCache()) cache = new ConcurrentHashMap<>();
}

public static IRAMDictionary getDict() {
return dict;
public static ILexicalDatabase getInstance() {
return db;
}

@Override
Expand All @@ -62,28 +60,25 @@ public Concept getConcept(String lemma, POS pos, int sense) {
@Override
public List<Concept> getAllConcepts(String lemma, POS pos) {
IIndexWord indexWord = dict.getIndexWord(lemma, edu.mit.jwi.item.POS.getPartOfSpeech(pos.getTag()));
return indexWord != null ? indexWord.getWordIDs().stream().map(iWordID -> new Concept(
iWordID.getSynsetID().toString(), pos, lemma)).collect(Collectors.toList()) : Collections.emptyList();
return indexWord != null ? indexWord.getWordIDs().stream().map(wordID -> new Concept(
wordID.getSynsetID().toString(), pos, lemma)).collect(Collectors.toList()) : Collections.emptyList();
}

@Override
public List<Concept> getLinkedSynsets(Concept concept, Link link) {
List<Concept> linkedSynsets = new ArrayList<>();
if (link == null || link.equals(Link.SYNSET)) {
linkedSynsets.add(concept);
} else {
ISynsetID iSynsetID = SynsetID.parseSynsetID(concept.getSynsetID());
if (link == null || link.equals(Link.SYNSET)) linkedSynsets.add(concept);
else {
ISynsetID synsetID = SynsetID.parseSynsetID(concept.getSynsetID());
if (link.equals(Link.MERONYM)) {
linkedSynsets.addAll(getRelatedSynsets(iSynsetID, Link.MERONYM_MEMBER));
linkedSynsets.addAll(getRelatedSynsets(iSynsetID, Link.MERONYM_SUBSTANCE));
linkedSynsets.addAll(getRelatedSynsets(iSynsetID, Link.MERONYM_PART));
linkedSynsets.addAll(getRelatedSynsets(synsetID, Link.MERONYM_MEMBER));
linkedSynsets.addAll(getRelatedSynsets(synsetID, Link.MERONYM_SUBSTANCE));
linkedSynsets.addAll(getRelatedSynsets(synsetID, Link.MERONYM_PART));
} else if (link.equals(Link.HOLONYM)) {
linkedSynsets.addAll(getRelatedSynsets(iSynsetID, Link.HOLONYM_MEMBER));
linkedSynsets.addAll(getRelatedSynsets(iSynsetID, Link.HOLONYM_SUBSTANCE));
linkedSynsets.addAll(getRelatedSynsets(iSynsetID, Link.HOLONYM_PART));
} else {
linkedSynsets.addAll(getRelatedSynsets(iSynsetID, link));
}
linkedSynsets.addAll(getRelatedSynsets(synsetID, Link.HOLONYM_MEMBER));
linkedSynsets.addAll(getRelatedSynsets(synsetID, Link.HOLONYM_SUBSTANCE));
linkedSynsets.addAll(getRelatedSynsets(synsetID, Link.HOLONYM_PART));
} else linkedSynsets.addAll(getRelatedSynsets(synsetID, link));
}
return linkedSynsets;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/uniba/di/lacam/kdde/ws4j/WS4J.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class WS4J {
private static RelatednessCalculator res;

static {
ILexicalDatabase db = new MITWordNet();
ILexicalDatabase db = MITWordNet.getInstance();
lin = new Lin(db);
wup = new WuPalmer(db);
hso = new HirstStOnge(db);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class SimilarityCalculationDemo {

static {
WS4JConfiguration.getInstance().setTrace(true);
ILexicalDatabase db = new MITWordNet();
ILexicalDatabase db = MITWordNet.getInstance();
rcs = new RelatednessCalculator[]{
new HirstStOnge(db), new LeacockChodorow(db), new Lesk(db), new WuPalmer(db),
new Resnik(db), new JiangConrath(db), new Lin(db), new Path(db)
Expand All @@ -24,7 +24,7 @@ public class SimilarityCalculationDemo {
public static void main(String[] args) {
long t = System.currentTimeMillis();
Arrays.asList(rcs).forEach(rc -> System.out.println(rc.getClass().getName() + "\t" +
rc.calcRelatednessOfWords("cancer", "diagnosis")));
rc.calcRelatednessOfWords("act", "moderate")));
System.out.println("Done in " + (System.currentTimeMillis()-t) + " msec.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private static boolean contains(String[] words1, String[] words2, int begin, int

public static class Overlaps {

private ConcurrentMap<String,Integer> overlapsHash;
private ConcurrentMap<String, Integer> overlapsHash;
private int length1;
private int length2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Traverser {
private static ConcurrentMap<Concept, Set<Concept>> downwardCache;

static {
db = new MITWordNet();
db = MITWordNet.getInstance();
if (WS4JConfiguration.getInstance().useCache()) {
horizonCache = new ConcurrentHashMap<>();
upwardCache = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

abstract public class RelatednessCalculatorTest {

private static ILexicalDatabase db;
protected static ILexicalDatabase db;

static {
WS4JConfiguration.getInstance().setMemoryDB(false);
WS4JConfiguration.getInstance().setLeskNormalize(false);
WS4JConfiguration.getInstance().setMFS(false);
db = new MITWordNet();
db = MITWordNet.getInstance();
}

protected static final String CYCLONE = "cyclone";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.Assert.assertEquals;

import edu.uniba.di.lacam.kdde.lexical_db.MITWordNet;
import edu.uniba.di.lacam.kdde.lexical_db.item.POS;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculator;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculatorTest;
Expand All @@ -21,7 +20,7 @@ public class HirstStOngeTest extends RelatednessCalculatorTest {

@BeforeClass
public static void oneTimeSetUp() {
rc = new HirstStOnge(new MITWordNet());
rc = new HirstStOnge(db);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.Assert.assertEquals;

import edu.uniba.di.lacam.kdde.lexical_db.MITWordNet;
import edu.uniba.di.lacam.kdde.lexical_db.item.POS;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculator;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculatorTest;
Expand All @@ -21,7 +20,7 @@ public class JiangConrathTest extends RelatednessCalculatorTest {

@BeforeClass
public static void oneTimeSetUp() {
rc = new JiangConrath(new MITWordNet());
rc = new JiangConrath(db);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.Assert.assertEquals;

import edu.uniba.di.lacam.kdde.lexical_db.MITWordNet;
import edu.uniba.di.lacam.kdde.lexical_db.item.POS;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculator;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculatorTest;
Expand All @@ -21,7 +20,7 @@ public class LeacockChodorowTest extends RelatednessCalculatorTest {

@BeforeClass
public static void oneTimeSetUp() {
rc = new LeacockChodorow(new MITWordNet());
rc = new LeacockChodorow(db);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.Assert.assertEquals;

import edu.uniba.di.lacam.kdde.lexical_db.MITWordNet;
import edu.uniba.di.lacam.kdde.lexical_db.item.POS;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculator;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculatorTest;
Expand All @@ -21,7 +20,7 @@ public class LeskTest extends RelatednessCalculatorTest {

@BeforeClass
public static void oneTimeSetUp() {
rc = new Lesk(new MITWordNet());
rc = new Lesk(db);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.Assert.assertEquals;

import edu.uniba.di.lacam.kdde.lexical_db.MITWordNet;
import edu.uniba.di.lacam.kdde.lexical_db.item.POS;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculator;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculatorTest;
Expand All @@ -21,7 +20,7 @@ public class LinTest extends RelatednessCalculatorTest {

@BeforeClass
public static void oneTimeSetUp() {
rc = new Lin(new MITWordNet());
rc = new Lin(db);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.Assert.assertEquals;

import edu.uniba.di.lacam.kdde.lexical_db.MITWordNet;
import edu.uniba.di.lacam.kdde.lexical_db.item.POS;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculator;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculatorTest;
Expand All @@ -21,7 +20,7 @@ public class PathTest extends RelatednessCalculatorTest {

@BeforeClass
public static void oneTimeSetUp() {
rc = new Path(new MITWordNet());
rc = new Path(db);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.Assert.assertEquals;

import edu.uniba.di.lacam.kdde.lexical_db.MITWordNet;
import edu.uniba.di.lacam.kdde.lexical_db.item.POS;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculator;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculatorTest;
Expand All @@ -21,7 +20,7 @@ public class ResnikTest extends RelatednessCalculatorTest {

@BeforeClass
public static void oneTimeSetUp() {
rc = new Resnik(new MITWordNet());
rc = new Resnik(db);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.Assert.assertEquals;

import edu.uniba.di.lacam.kdde.lexical_db.MITWordNet;
import edu.uniba.di.lacam.kdde.lexical_db.item.POS;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculator;
import edu.uniba.di.lacam.kdde.ws4j.RelatednessCalculatorTest;
Expand All @@ -21,7 +20,7 @@ public class WuPalmerTest extends RelatednessCalculatorTest {

@BeforeClass
public static void oneTimeSetUp() {
rc = new WuPalmer(new MITWordNet());
rc = new WuPalmer(db);
}

@Test
Expand Down

0 comments on commit f8d1d04

Please sign in to comment.