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; } }
二、測試方法:spa
public static void main(String[] args) { String str = "科技項目總經費"; String target = "科技項目經費"; System.out.println("該指標匹配率:"+getSimilarityRatio(str, target)+"%"); }
結果以下:code
2、計算方法解析blog
一、方法:字符串
採用動態規劃遞推的方式,先創建二維數組並給定初始值,再根據初始值向後一一遞推。get
先獲得最小差別數,再獲得差別數佔比,用1減去以後則是兩個指標名稱字符串之間的匹配度。string
二、重點(求數組中的值):it
數組中的第i行第j列的值,爲它左側的值+1和上側的值+1以及左上角的值加上增量temp求最小值獲得的。遞推後所得的二維數組中,最後一行的最後一列即爲最小差別數。io