WordNet Research 7 -- JWS (Java WordNet similarity) semantic similarity calculation

Source: Internet
Author: User

JWS -- Java WordNet similarity is an open-source project developed by David hope of the University of Sussex to calculate semantic similarity between Java and WordNet.It implements many classical semantic similarityAlgorithm. It is an open-source tool for semantic similarity calculation.

 

JWS is the Java implementation version of WordNet: similarity (a Perl version of The WordNet Similarity comparison Package). If you want to use Java to implement WordNet to compare word similarity, you are lucky !! Procedure:

 

1. Download WordNet (Win, 2.1): http://wordnet.princeton.edu/wordnet/download /;

 

2. Download WordNet-infocontent (2.1): http://wn-similarity.sourceforge.net/or http://www.d.umn.edu /~ Tpederse/data /;

 

3. Download JWS (available version: beta.11.01): http://www.cogs.susx.ac.uk/users/drh21 /;

 

4. Install WordNet;

 

5. Unzip the WordNet-InfoContent-2.1 and copy the folder to the WordNet Directory D:/program files/WordNet/2.1;

 

6. Copy two jar packages in JWS: edu. MIT. jwi_2.1.4.jar and edu. Sussex. NLP. JWS. beta.11.jar to the lib directory of Java and set the environment variables;

 

7. Example of running JWS in eclipseProgram: Testexamples

 

Note: Since the downloaded WordNet version is 2.1, there are several changes in the program.

 

String dir = "C:/program files/WordNet"; // specify the WordNet installation path and modify it according to the actual installation path.

 

JWS Ws = new JWS (Dir, "3.0"); // change 3.0 to 2.1.

Program instance:

 

 1   Import  Java. util. treemap;  2   Import Java. Text .* ;  3   Import Edu. Sussex. NLP. JWS .* ;  4   5   6   //  'Testexamples ': How to Use Java WordNet: similarity  7   //  David Hope, 2008  8   Public   Class  Testexamples 9   {  10        Public   Static   Void  Main (string [] ARGs)  11   {  12   13   //  1. Set up:  14   // Let's make it easy for the user. So, rather than set pointers in 'Environment variables 'etc. let's allow the user to define exactly where they have put WordNet (s)  15 String dir = "E:/commonly application/WordNet /" ;  16   //  That is, you may have version 3.0 sitting in the above directory e.g. C:/program files/WordNet/3.0/dict  17   //  The corresponding IC Files Folder shocould be in this same directory e.g. C:/program files/WordNet/3.0/WordNet-InfoContent-3.0  18  19   //  Option 1 (Perl default): Specify the version of WordNet you want to use (assuming that you have a copy of it) and use the default IC file [ic-semcor.dat]  20 JWS = New JWS (Dir, "2.1" );  21   //  Option 2: Specify the version of WordNet you want to use and the participating IC file that you wish to apply  22           // JWS Ws = new JWS (Dir, "3.0", "ic-bnc-resnik-add1.dat ");  23   24   25   //  2. Examples of use:  26   27   //  2.1 [Jiang & conrath measure]  28 Jiangandconrath jcn = WS. getjiangandconrath ();  29           // System. Out. println ("Jiang & conrath \ n ");  30   //  All senses  31 Treemap <string, double> scores1 = jcn. jcn ("apple", "banana", "n "); //  All senses  32           //  Treemap <string, double> scores1 = jcn. jcn ("apple", 1, "banana", "n ");  //  Fixed; all  33          //  Treemap <string, double> scores1 = jcn. jcn ("apple", "banana", 2, "N ");  //  All; fixed  34           For  (String S: scores1.keyset ())  35 System. Out. println (S + "\ t" + Scores1.get (s ));  36   //  Specific senses  37           // System. out. println ("\ nspecific pair \ t = \ t" + jcn. jcn ("apple", 1, "banana", 1, "N") + "\ n ");  38   //  Max.  39           //  /System. out. println ("\ nhighest Score \ t = \ t" + jcn. max ("Java", "best", "n") + "\ n ");  40   41   //  */  42   //  2.2 [Lin measure]  43 Lin = WS. getlin ();  44           //  /System. Out. println ("Lin \ n ");  45   //  All senses  46 Treemap <string, double> scores2 = Lin. Lin ("like", "love", "n "); //  All senses  47           //  Treemap <string, double> scores2 = Lin. Lin ("kid", "child", "n "); //  Fixed; all  48           //  Treemap <string, double> scores2 = Lin. Lin ("apple", "banana", 2, "N ");  //  All; fixed  49           //  For (string S: scores2.keyset ())  50               //  System. Out. println (S + "\ t" + scores2.get (s ));  51   // Specific senses  52 System. out. println ("\ nspecific pair \ t = \ t" + lin. lin ("like", 1, "love", 1, "N") + "\ n" );  53   //  Max.  54 System. out. println ("\ nhighest Score \ t = \ t" + lin. max ("from", "date", "n") + "\ n" );  55   56   //  ... And so on for any other measure  57  }  58 } //  EOF 

 

 

A simple JWS-Based Semantic similarity calculation program, for example:

 1   Import  Edu. Sussex. NLP. JWS. JWS;  2   Import  Edu. Sussex. NLP. JWS. Lin;  3   4   5   Public   Class Similar {  6   7       Private  String str1;  8       Private  String str2;  9       Private String dir = "E:/commonly application/WordNet /" ;  10       Private JWS = New JWS (Dir, "2.1" ); 11       12       Public  Similar (string str1, string str2 ){  13           This . Str1 = Str1;  14           This . Str2 = Str2;  15   }  16       17       Public   Double Getsimilarity (){  18 String [] strs1 = Splitstring (str1 );  19 String [] strs2 = Splitstring (str2 );  20           Double Sums = 0.0 ;  21           For  (String S1: strs1 ){  22               For  (String S2: strs2 ){  23                  Double SC = Maxscoreoflin (S1, S2 );  24 Sum + = SC;  25 System. Out. println ("current calculation:" + S1 + "vs" + S2 + "similarity:" + SC );  26   }  27   }  28           Double Similarity = sum/(strs1.length * Strs2.length );  29 Sum = 0 ;  30           Return  Similarity;  31   }  32       33       Private  String [] splitstring (string Str ){  34 String [] ret = Str. Split ("" );  35           Return  RET;  36  }  37       38       Private   Double  Maxscoreoflin (string str1, string str2 ){  39 Lin = WS. getlin ();  40           Double SC = Lin. Max (str1, str2, "N" );  41           If (SC = 0 ){  42 SC = Lin. Max (str1, str2, "V" );  43   }  44           Return  SC;  45   }  46       47       Public   Static   Void  Main (string ARGs []) {  48 String S1 = "departure" ; 49 String S2 = "leaving from" ;  50 Similar Sm = New  Similar (S1, S2 );  51   System. Out. println (Sm. getsimilarity ());  52   }  53 }

At that time, I came across the idea of processing Semantic Analysis Based on protege + WordNet. So I came into contact with JWS, but I didn't have much time to study it in depth. It was a great pity that I hoped to have some friends who studied it, send a blog URL for your reference!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.