毕设之路:指标名称间的匹配度计算

一、计算方法

  1、获取匹配度的计算方法:

package com.gb.Util;
/**
 *
 * @author 我命倾尘
 * @date 2019年10月24日 下午8:47:39 
 * @version 1.0 
 *
 */
public class getSimilarityRatio 
{
    //指标匹配度计算方法
    public static float getSimilarityRatio(String string, String target)
    {
        //定义一个用来存储指标名称和字符间匹配中间值得二维数组
        int similarity[][]; 
        //相同字符增量
        int temp;
        //得到来源指标名称和标准指标名称的长度
        int strLength = string.length();
        int targetLength = target.length();
        //用来遍历两个字符串的值
        int i,j;
        //用来存储两个字符串中字符的值
        char char1,char2;
        
        //其中一个为空时,无匹配度
        if (strLength == 0 || targetLength == 0) 
        {
            return 0;
        }
        
        similarity = new int[strLength + 1][targetLength + 1];
        //初始化
        for (i = 0; i <= strLength; i++) 
        {
            similarity[i][0] = i;
        }
        for (j = 0; j <= targetLength; j++) 
        {
            similarity[0][j] = j;
        }
        
        //遍历来源指标名称字符串
        for (i = 1; i <= strLength; i++) 
        {
            char1 = string.charAt(i - 1);
            // 匹配标准指标字符串
            for (j = 1; j <= targetLength; j++) 
            {
                char2 = target.charAt(j - 1);
                //矩阵中出现相同字符记为0
                //不相同字符记为1
                if (char1 == char2 || char1 == char2 + 32 || char1 + 32 == char2)//ASCII码表值相差32,即不区分大小写
                {
                    temp = 0;
                } 
                else 
                {
                    temp = 1;
                }
                //递推,分别取二维数组中的左值加一和上值加一,以及左上值加匹配增量,这三个值取最小,即为有差异的字符数
                similarity[i][j] = Math.min(Math.min(similarity[i - 1][j] + 1, similarity[i][j - 1] + 1), similarity[i - 1][j - 1] + temp);
            }
        }
        //差异字符数在较长字符串的字符个数中的占比为差异率
        //返回的值为被一减去之后的匹配率
        return (1 - (float) similarity[strLength][targetLength] / Math.max(string.length(), target.length())) * 100F;
    }
}

  2、测试方法:

public static void main(String[] args)
{
    String str = "科技项目总经费";
    String target = "科技项目经费";
    System.out.println("该指标匹配率:"+getSimilarityRatio(str, target)+"%");
}

  结果如下:

  

 二、计算方法解析

  1、方法:

  采用动态规划递推的方式,先建立二维数组并给定初始值,再根据初始值向后一一递推。

  先得到最小差异数,再得到差异数占比,用1减去之后则是两个指标名称字符串之间的匹配度

  2、重点(求数组中的值):

  数组中的第i行第j列的值,为它左侧的值+1和上侧的值+1以及左上角的值加上增量temp求最小值得到的。递推后所得的二维数组中,最后一行的最后一列即为最小差异数

猜你喜欢

转载自www.cnblogs.com/guobin-/p/11738529.html
今日推荐