JAVA中怎么获取两个字符串的相似度

发布时间:2021-06-21 17:43:04 作者:Leah
来源:亿速云 阅读:627

本篇文章为大家展示了JAVA中怎么获取两个字符串的相似度,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

代码如下:

public static float getSimilarityRatio(String str, String target) {

	int d[][]; // 矩阵
	int n = str.length();
	int m = target.length();
	int i; // 遍历str的
	int j; // 遍历target的
	char ch2; // str的
	char ch3; // target的
	int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
	if (n == 0 || m == 0) {
		return 0;
	}
	d = new int[n + 1][m + 1];
	for (i = 0; i <= n; i++) { // 初始化第一列
		d[i][0] = i;
	}

	for (j = 0; j <= m; j++) { // 初始化第一行
		d[0][j] = j;
	}

	for (i = 1; i <= n; i++) { // 遍历str
		ch2 = str.charAt(i - 1);
		// 去匹配target
		for (j = 1; j <= m; j++) {
			ch3 = target.charAt(j - 1);
			if (ch2 == ch3 || ch2 == ch3 + 32 || ch2 + 32 == ch3) {
				temp = 0;
			} else {
				temp = 1;
			}
			// 左边+1,上边+1, 左上角+temp取最小
			d[i][j] = Math.min(Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1), d[i - 1][j - 1] + temp);
		}
	}

	return (1 - (float) d[n][m] / Math.max(str.length(), target.length())) * 100F;
}~~~~

上述内容就是JAVA中怎么获取两个字符串的相似度,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 比较两个RGB颜色的相似度
  2. 两个字符串文本相似度计算

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:nginx中怎么配置正向代理

下一篇:php中怎么利用协种读取大文件csv

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》