两个词之间的关系有同义、反义、近义(有多近?)、相关(有多相关?)等等。我们如何来判断两个词之间的关系呢?利用计算机能自动找出这种关系吗?当然可以,不仅能找出来,而且还能量化出有多近和有多相关。
本文描述了superword开源项目中的定义相似规则,利用词的定义计算词和词之间的相似性。词的定义使用的是韦氏词典,同时也支持牛津词典。相似性算法使用的是word分词提供的10大相似性算法。
**定义相似规则**主要包括以下6步:
1、获取要计算的词的定义:
String wordDefinition = MySQLUtils.getWordDefinition(word, WordLinker.Dictionary.WEBSTER.name());
2、获取分级词汇,分级词汇的具体定义见这里:
Set<Word> words = (Set<Word>)application.getAttribute("words_"+request.getAttribute("words_type"));
3、获取分级词汇的定义,代码见这里:
List<String> allWordDefinition = MySQLUtils.getAllWordDefinition(WordLinker.Dictionary.WEBSTER.name(), words);
4、从word分词提供的10大相似性算法中任选一个,同时指定使用word分词提供的针对纯英文的分词器:
1TextSimilarity textSimilarity = new CosineTextSimilarity(); 2textSimilarity.setSegmentationAlgorithm(SegmentationAlgorithm.PureEnglish);
5、计算相似性,返回最相似的100个单词:
1int count = 100; 2Hits result = textSimilarity.rank(wordDefinition, allWordDefinition, count);
6、输出计算结果:
1StringBuilder temp = new StringBuilder(); 2int i=1; 3temp.append("<table border=\"1\">\n"); 4for(Hit hit : result.getHits()){ 5 String[] attrs = hit.getText().split("_"); 6 String w = attrs[0]; 7 StringBuilder definition = new StringBuilder(attrs[1]); 8 for(int j=2; j<attrs.length; j++){ 9 definition.append(attrs[j]).append("_"); 10 } 11 temp.append("<tr>"); 12 temp.append("<td> ").append(i++) 13 .append(". </td><td> ") 14 .append(WordLinker.toLink(w)) 15 .append(" </td><td> ") 16 .append(definition) 17 .append(" </td><td> ") 18 .append(hit.getScore()) 19 .append("</td><td> ") 20 .append("<a target=\"_blank\" href=\"definition-similar-rule.jsp?word=" + hit.getText() + "&count=" + count + "&words_type=" + request.getAttribute("words_type") + "\">相似</a>") 21 .append(" </td>\n"); 22 temp.append("</tr>\n"); 23} 24temp.append("</table>\n"); 25htmlFragment = temp.toString();
计算效果如下图所示:
1、使用韦氏词典的定义

2、使用爱词霸的定义


3、使用有道词典的定义
