import rita.wordnet.RiWordnet;
public class WordNetConnection
{
RiWordnet wordnet;
String [] result;
String [] synsets = null;
public WordNetConnection()
{
wordnet = new RiWordnet(null,"c:\\WordNet\\2.1\\");
}
public void testDictionary()
{
getLinSimilarity("bag" , "luggage");
}
public int getNumberOfHyponyms(String concept)
{
String pos[];
String hyponyms[];
int totalHyponyms = 0;
pos = wordnet.getPos(concept);
// System.out.println("Pos Length of concept: "+concept+ " is :" +pos.length);
for(int i = 0 ; i < pos.length ; i++)
{
hyponyms = wordnet.getAllHyponyms(concept, pos[i]);
if(hyponyms == null)
continue;
/* for(int j = 0 ; j < hyponyms.length ; j++)
{
System.out.println("Hyponyms : " +j+ " : " +hyponyms[j]);
}*/
totalHyponyms = totalHyponyms + hyponyms.length;
}
return
totalHyponyms;
}
public double getInfoContent(String concept)
{
int hypos_concept = getNumberOfHyponyms(concept);
double info_content = 1 - (Math.log(hypos_concept + 1)/Math.log(117000));
if (hypos_concept == 0)
return 1;
else
return info_content;
}
public double getLinSimilarity(String concept1 , String concept2)
{
String pos = wordnet.getBestPos(concept1);
String [] lcs = null;
double lin_sim;
double ic_lcs , ic_c1 ,ic_c2;
// System.out.println("BEST POS = " +pos);
// get the least common subsumer
try{
lcs = wordnet.getCommonParents(concept1, concept2 , pos);
}catch(Exception e){
}
// if no common parent try with best Pos of concept2
if(lcs == null)
{
pos = wordnet.getBestPos(concept2);
try{
lcs = wordnet.getCommonParents(concept1, concept2 , pos);
}catch(Exception e){
}
}
// if no lcs found then they are dissimilar
if(lcs == null)
{
// System.out.println("NOT SIMILAR");
return 0;
}
// get info content of both words
ic_c1 = getInfoContent(concept1);
ic_c2 = getInfoContent(concept2);
// get info content of lcs
ic_lcs = getInfoContent(lcs[0]);
// System.out.println("INFO CONTENT OF C1: " +ic_c1+ " OF C2: " +ic_c2+ " OF LCS: "+ic_lcs);
// calculate lin_sim
lin_sim = (2 * ic_lcs/(ic_c1 + ic_c2));
// System.out.println("LIN SIM = " +lin_sim);
/* for(int i = 0 ; i < lcs.length ; i++)
{
System.out.println("LCS " +i+ " is " +lcs[i]);
}*/
return lin_sim;
}
}