diff --git a/src/fr/inrialpes/exmo/align/ling/JWNLDistances.java b/src/fr/inrialpes/exmo/align/ling/JWNLDistances.java index 9ab59472f1f932960928bcf5234d500bfe113227..c64cc42eb61474cec4ef04dd342f747d537f87a3 100644 --- a/src/fr/inrialpes/exmo/align/ling/JWNLDistances.java +++ b/src/fr/inrialpes/exmo/align/ling/JWNLDistances.java @@ -89,5 +89,241 @@ if (index!=null){ return Dists1s2; } +/** + * Compute a distance between 2 strings using WordNet synonym + * Get all the synonym for s1 , syno1 + * Get all the synonym for s2, syno2 + * compute intersection syno1(inter)syno2, I + * compute union syno1(union)syno2, U + * The distance = card I /card U + * + * @param s1 + * @param s2 + * @return Distance between s1 & s2 + */ + +public double SynonymDistance (String s1,String s2){ + double result=0.000000; + int cardI=0,cardU=0; + int i=0,j=0,k=0; + String Syno1[]; + String Syno2[]; + String I[]=null; + String U[]=null; + int nbmot1; + int nbmot2; + + /** Build the list of synonym for s1 & s2 **/ + nbmot1=CountMaxSens(s1); + nbmot2=CountMaxSens(s2); + Syno1=new String[nbmot1]; + Syno2=new String[nbmot2]; + Syno1=GetAllSyno(s1); + Syno2=GetAllSyno(s2); + /** Compute intersection between the list of synonym : I**/ +I=Inter (Syno1,Syno2); + /** Compute union between the list of synonym U **/ +U=Union (Syno1,Syno2); + /** compute card I / card U **/ +for (i=0;i<I.length;i++){if (I[i]!=null) {cardI++;}} +for (i=0;i<U.length;i++){if (U[i]!=null) {cardU++;}} + if(cardU!=0){result=(double)cardI /(double) cardU;} + return (result); +} +/** + * + * @param S + * @return Number of sens for the word s + */ +private int CountMaxSens (String S){ + IndexWord index=null; + Synset S1[]=null; + int i=0; + int sens1=0; + int nbmot1=0; + + try { index = Dictionary.getInstance().lookupIndexWord(POS.NOUN,S);} + catch(Exception ex){ex.printStackTrace(); + System.exit(-1);} + if (index!=null){ + try {S1=index.getSenses(); + sens1= index.getSenseCount();} + catch (JWNLException e) {e.printStackTrace();} + } + for(i=0;i<sens1;i++){nbmot1=nbmot1+S1[i].getWordsSize();} + + try { index = Dictionary.getInstance().lookupIndexWord(POS.VERB,S);} + catch(Exception ex){ex.printStackTrace(); + System.exit(-1);} + if (index!=null){ + try {S1=index.getSenses(); + sens1= index.getSenseCount();} + catch (JWNLException e) {e.printStackTrace();} + } + for(i=0;i<sens1;i++){nbmot1=nbmot1+S1[i].getWordsSize();} + + try { index = Dictionary.getInstance().lookupIndexWord(POS.ADJECTIVE,S);} + catch(Exception ex){ex.printStackTrace(); + System.exit(-1);} + if (index!=null){ + try {S1=index.getSenses(); + sens1= index.getSenseCount();} + catch (JWNLException e) {e.printStackTrace();} + } + for(i=0;i<sens1;i++){nbmot1=nbmot1+S1[i].getWordsSize();} + + try { index = Dictionary.getInstance().lookupIndexWord(POS.ADVERB,S);} + catch(Exception ex){ex.printStackTrace(); + System.exit(-1);} + if (index!=null){ + try {S1=index.getSenses(); + sens1= index.getSenseCount();} + catch (JWNLException e) {e.printStackTrace();} + } + for(i=0;i<sens1;i++){nbmot1=nbmot1+S1[i].getWordsSize();} + + return (nbmot1); + +} +/** + * + * @param S + * @return The tab with all the synonym of the word s + */ +private String[] GetAllSyno(String S){ + String Syno[]=null; + IndexWord index=null; + Synset[] Synso=null; + int sens=0; + int nbmot=0; + int i=0,j=0,k=0,l=0; + boolean trouve=false; + int strcomp=0; + + nbmot=CountMaxSens(S); + Syno=new String[nbmot]; + + try { index = Dictionary.getInstance().lookupIndexWord(POS.NOUN,S);} + catch(Exception ex){ex.printStackTrace(); + System.exit(-1);} + if (index!=null){ + try {Synso=index.getSenses(); + sens= index.getSenseCount();} + catch (JWNLException e) {e.printStackTrace();} + } + + for (i=0;i<sens;i++){ + for(j=0;j<Synso[i].getWordsSize();j++){ + for(l=0;l<k;l++){ + if (Synso[i].getWord(j).getLemma().compareTo(Syno[l])==0){trouve=true;} + } + if (trouve!=true){Syno[k++] = Synso[i].getWord(j).getLemma();} + trouve=false; + } + } + try { index = Dictionary.getInstance().lookupIndexWord(POS.VERB,S);} + catch(Exception ex){ex.printStackTrace(); + System.exit(-1);} + if (index!=null){ + try {Synso=index.getSenses(); + sens= index.getSenseCount();} + catch (JWNLException e) {e.printStackTrace();} + } + + for (i=0;i<sens;i++){ + for(j=0;j<Synso[i].getWordsSize();j++){ + for(l=0;l<k;l++){ + if (Synso[i].getWord(j).getLemma().compareTo(Syno[l])==0){trouve=true;} + } + if (trouve!=true){ + Syno[k++] = Synso[i].getWord(j).getLemma();} + trouve=false; + } + } + + try { index = Dictionary.getInstance().lookupIndexWord(POS.ADJECTIVE,S);} + catch(Exception ex){ex.printStackTrace(); + System.exit(-1);} + if (index!=null){ + try {Synso=index.getSenses(); + sens= index.getSenseCount();} + catch (JWNLException e) {e.printStackTrace();} + } + + for (i=0;i<sens;i++){ + for(j=0;j<Synso[i].getWordsSize();j++){ + for(l=0;l<k;l++){ + if (Synso[i].getWord(j).getLemma().compareTo(Syno[l])==0){trouve=true;} + } + if (trouve!=true){ + Syno[k++] = Synso[i].getWord(j).getLemma();} + trouve=false; + } + } + + try { index = Dictionary.getInstance().lookupIndexWord(POS.ADVERB,S);} + catch(Exception ex){ex.printStackTrace(); + System.exit(-1);} + if (index!=null){ + try {Synso=index.getSenses(); + sens= index.getSenseCount();} + catch (JWNLException e) {e.printStackTrace();} + } + for (i=0;i<sens;i++){ + for(j=0;j<Synso[i].getWordsSize();j++){ + for(l=0;l<k;l++){ + if (Synso[i].getWord(j).getLemma().compareTo(Syno[l])==0){trouve=true;} + } + if (trouve!=true){ + Syno[k++] = Synso[i].getWord(j).getLemma();} + trouve=false; + } + } + return(Syno); +} +private String[] Inter(String[] S1, String[] S2){ + String result[]=null; + int i=0,j=0,k=0,l=0; + boolean trouve=false; + + i=S1.length; + j=S2.length; + if(i<j){result = new String[j];} + else result=new String[i]; + + for (i=0;i<S1.length;i++){ + for(j=0;j<S2.length;j++){ + if(S1[i]!=null && S2[j]!=null){ + if (S1[i].compareTo(S2[j])==0){trouve=true;} + } + } + if (trouve==true){result[k++]=S1[i];} + trouve=false; + } + return(result); +} + +private String[] Union(String[] S1,String[] S2){ + String result[]=null; + int i=0,j=0,k=0,l=0; + boolean trouve=false; + + i=S1.length; + j=S2.length; + result = new String[i+j]; + + for (i=0;i<S1.length;i++){result[k++]=S1[i];} + for(j=0;j<S2.length;j++){ + for(l=0;l<S1.length;l++){ + + if(S1[l]!=null && S2[j]!=null){ + if (S2[j].compareTo(S1[l])==0){trouve=true;} + } + } + if(trouve==false){result[k++]=S2[j];} + trouve=false; + } + return(result); +} } \ No newline at end of file