java – ws4j为应该返回1的相似性度量返回无穷大
我有一个非常简单的代码,取自
this example,我使用Lin,Path和Wu-Palmer相似性度量来计算两个单词之间的相似性.我的代码如下:
import edu.cmu.lti.lexical_db.ILexicalDatabase; import edu.cmu.lti.lexical_db.NictWordNet; import edu.cmu.lti.ws4j.RelatednessCalculator; import edu.cmu.lti.ws4j.impl.Lin; import edu.cmu.lti.ws4j.impl.Path; import edu.cmu.lti.ws4j.impl.WuPalmer; public class Test { private static ILexicalDatabase db = new NictWordNet(); private static RelatednessCalculator lin = new Lin(db); private static RelatednessCalculator wup = new WuPalmer(db); private static RelatednessCalculator path = new Path(db); public static void main(String[] args) { String w1 = "walk"; String w2 = "trot"; System.out.println(lin.calcRelatednessOfWords(w1,w2)); System.out.println(wup.calcRelatednessOfWords(w1,w2)); System.out.println(path.calcRelatednessOfWords(w1,w2)); } } 当两个单词相同时,分数都是预期的除外.如果两个单词相同(例如w1 =“walk”; w2 =“walk”;),我所拥有的三个度量应该都返回1.0.但相反,他们正在返回1.7976931348623157E308. 我之前使用过ws4j(实际上是相同版本),但我从未见过这种行为.在线搜索没有产生任何线索.这里可能出现什么问题? 附: Lin,Wu-Palmer和Path措施应返回1的事实也可以通过the online demo provided by ws4j进行验证 解决方法
我有类似的问题,这就是这里发生的事情.我希望遇到此问题的其他人会通过回复找到帮助.
如果您注意到,在线演示允许您通过以下格式指定单词来选择单词意义:word#pos_tag #word_sense.例如,具有第一个单词意义的名词性别将是性别#n#1. 您的代码段默认使用第一个单词sense.当我计算“性别”和“性别”之间的WuPalmer相似性时,它将返回0.26.如果我使用在线演示,它将返回1.0.但如果我们使用“性别#n#1”和“性别#n#1”,在线演示将返回0.26,因此没有差异.在线演示计算所有pos标签/字检测对的最大值.这是一个相应的代码片段,可以解决这个问题: ILexicalDatabase db = new NictWordNet(); WS4JConfiguration.getInstance().setMFS(true); RelatednessCalculator rc = new Lin(db); String word1 = "gender"; String word2 = "sex"; List<POS[]> posPairs = rc.getPOSPairs(); double maxScore = -1D; for(POS[] posPair: posPairs) { List<Concept> synsets1 = (List<Concept>)db.getAllConcepts(word1,posPair[0].toString()); List<Concept> synsets2 = (List<Concept>)db.getAllConcepts(word2,posPair[1].toString()); for(Concept synset1: synsets1) { for (Concept synset2: synsets2) { Relatedness relatedness = rc.calcRelatednessOfSynset(synset1,synset2); double score = relatedness.getScore(); if (score > maxScore) { maxScore = score; } } } } if (maxScore == -1D) { maxScore = 0.0; } System.out.println("sim('" + word1 + "','" + word2 + "') = " + maxScore); 此外,这将为您提供非词干形式的0.0相似性,例如: ‘性别’和’性’.您可以使用ws4j中包含的porter stemmer来确保在需要时事先干掉单词. 希望这可以帮助! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |