I am currently trying to semantically match two sentences in ws4j. I implemented the concept at a word level but am having trouble implementing the same at a sentence level and get an output in the form of a matrix like it shows on the online demo. How to develop a code to do the same?
import java.util.List;
import edu.cmu.lti.ws4j.impl.Lesk;
import edu.cmu.lti.jawjaw.pobj.POS;
import edu.cmu.lti.lexical_db.ILexicalDatabase;
import edu.cmu.lti.lexical_db.NictWordNet;
import edu.cmu.lti.lexical_db.data.Concept;
import edu.cmu.lti.ws4j.Relatedness;
import edu.cmu.lti.ws4j.RelatednessCalculator;
public class WordMatcher1 {
public static void main(String[] args)
{
String word1="rifle";
String word2="gun";
ILexicalDatabase db = new NictWordNet();
RelatednessCalculator lesk = new Lesk(db);
List<POS[]> posPairs = lesk.getPOSPairs();
double maxScore = -1D;
for(POS[] posPair: posPairs)
{
String p1 = null,p2 = null;
List<Concept> synsets1 = (List<Concept>)db.getAllConcepts(word1, posPair[0].toString());
List<Concept> synsets2 = (List<Concept>)db.getAllConcepts(word2, posPair[1].toString());
for(Concept ss1: synsets1)
{
for (Concept ss2: synsets2)
{
p1 = ss1.getPos().toString();
p2 = ss2.getPos().toString();
Relatedness relatedness = lesk.calcRelatednessOfSynset(ss1, ss2);
double score = relatedness.getScore();
if (score > maxScore)
{
maxScore = score;
}
}
}
if (maxScore == -1D)
{
maxScore = 0.0;
}
System.out.println("sim('" + word1 +" "+ p1 +"', '" + p2 +" "+ word2+ "') = " + maxScore);
}
}
I had a similar problem, and this example worked: