公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

本文介绍了如何解决在Web API中获得的值与通过ws4j中的源代码获得的值之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ws4j库开发了以下用于句子语义匹配的API。但我没能得到语义上的相似之处。
输出以图像的形式附加,其中显示冗余或0的值。
是否有遗漏调用的库?

package ws4jv01;

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.HirstStOnge;
import edu.cmu.lti.ws4j.impl.JiangConrath;
import edu.cmu.lti.ws4j.impl.LeacockChodorow;
import edu.cmu.lti.ws4j.impl.Lesk;
import edu.cmu.lti.ws4j.impl.Lin;
import edu.cmu.lti.ws4j.impl.Path;
import edu.cmu.lti.ws4j.impl.Resnik;
import edu.cmu.lti.ws4j.impl.WuPalmer;

public class SentenceMatcherSimilarityMatrix
{
 private static ILexicalDatabase db = new NictWordNet();
 public double[][] getSimilarityMatrix( String[] words1, String[] words2, RelatednessCalculator rc )
{
    double[][] result = new double[words1.length][words2.length];
    for ( int i=0; i<words1.length; i++ ){
        for ( int j=0; j<words2.length; j++ ) {
            double score = rc.calcRelatednessOfWords(words1[i], words2[j]);
            result[i][j] = score;
          }
        }
    return result;
  }
  private void compute (String[] words1, String[] words2)
  {
    System.out.println("WuPalmer");
    RelatednessCalculator rc1 = new WuPalmer(db);
       {
        double[][] s1 = getSimilarityMatrix(words1, words2,rc1);
        for(int i=0; i<words1.length; i++){
            for(int j=0; j< words2.length; j++){
                System.out.print(s1[i][j] +"	");
            } 
            System.out.println();
        }}
    System.out.println();
    System.out.println();

    System.out.println("Resnik");
    RelatednessCalculator rc2 = new Resnik(db);
    {
        double[][] s2 = getSimilarityMatrix(words1, words2,rc2);
        for(int i=0; i<words1.length; i++){
            for(int j=0; j< words2.length; j++){
                System.out.print(s2[i][j] +"	");
            } 
            System.out.println();
        }}
    System.out.println();
    System.out.println();

    System.out.println("JiangConrath");
    RelatednessCalculator rc3 = new JiangConrath(db);
    {
        double[][] s2 = getSimilarityMatrix(words1, words2,rc3);
        for(int i=0; i<words1.length; i++){
            for(int j=0; j< words2.length; j++){
                System.out.print(s2[i][j] +"	");
            } 
            System.out.println();
        }}
    System.out.println();
    System.out.println();

    System.out.println("Lin");
    RelatednessCalculator rc4 = new Lin(db);
    {
        double[][] s2 = getSimilarityMatrix(words1, words2,rc4);
        for(int i=0; i<words1.length; i++){
            for(int j=0; j< words2.length; j++){
                System.out.print(s2[i][j] +"	");
            } 
            System.out.println();
        }}
    System.out.println();
    System.out.println();

    System.out.println("LeacockChodrow");
    RelatednessCalculator rc5 = new LeacockChodorow(db);
    {
        double[][] s2 = getSimilarityMatrix(words1, words2,rc5);
        for(int i=0; i<words1.length; i++){
            for(int j=0; j< words2.length; j++){
                System.out.print(s2[i][j] +"	");
            } 
            System.out.println();
        }}
    System.out.println();
    System.out.println();

    System.out.println("Path");
    RelatednessCalculator rc6 = new Path(db);
    {
        double[][] s2 = getSimilarityMatrix(words1, words2,rc6);
        for(int i=0; i<words1.length; i++){
            for(int j=0; j< words2.length; j++){
                System.out.print(s2[i][j] +"	");
            } 
            System.out.println();
        }}
    System.out.println();
    System.out.println();

    System.out.println("Lesk");
    RelatednessCalculator rc7 = new Lesk(db);
    {
        double[][] s2 = getSimilarityMatrix(words1, words2,rc7);
        for(int i=0; i<words1.length; i++){
            for(int j=0; j< words2.length; j++){
                System.out.print(s2[i][j] +"	");
            } 
            System.out.println();
        }}
    System.out.println();
    System.out.println();

    System.out.println("HirstStOnge");
    RelatednessCalculator rc8 = new HirstStOnge(db);
    {
        double[][] s2 = getSimilarityMatrix(words1, words2,rc8);

        for(int i=0; i<words1.length; i++){
            for(int j=0; j< words2.length; j++){
                System.out.print(s2[i][j] +"	");
            } 
            System.out.println();
           }}
          }

public static void main(String[] args) 
{
    String sent1 = "The boy is playing with a dog.";
    String sent2 = "The kid is playing with his pet.";

    String[] words1 = sent1.split(" ");
    String[] words2 = sent2.split(" ");
    SentenceMatcherSimilarityMatrix sm1 = new SentenceMatcherSimilarityMatrix(); 
    sm1.compute(words1, words2);
}
}

Result set

推荐答案

相似性矩阵呈现中的细微优化:

for (int i = 0; i <= words1.length; i++) {
    for (int j = 0; j <= words2.length; j++) {
        if(i==0 && j==0) {
            System.out.print(" " + "	"); 
        } else if(i==0) {
            System.out.print(words2[j-1] + "	");
        } else if(j==0) {
            System.out.print(words1[i-1] + "	");
        } else {
            System.out.print(s1[i-1][j-1] + "	");
        }
    }
    System.out.println();
}

这将使矩阵显示为:

这篇关于如何解决在Web API中获得的值与通过ws4j中的源代码获得的值之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

分享到:
标签:API Web ws4j 如何解决 差异 源代码
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定