计算两段文本的相识度
来源:互联网 发布:热云数据融资 编辑:程序博客网 时间:2024/06/08 06:09
package com.wlkj.test;import java.text.NumberFormat;import java.util.Locale;public class SimilarTest { public static void main(String[] args) { String strA = "我喜欢你"; String strB = "你喜欢我"; System.out.println(similarityResult(SimilarDegree(strA,strB))); } /* * 计算相似度 */ public static double SimilarDegree(String strA, String strB) { String newStrA = removeSign(strA); String newStrB = removeSign(strB); // 用较大的字符串长度作为分母,相似子串作为分子计算出字串相似度 int temp = Math.max(newStrA.length(), newStrB.length()); int temp2 = longestCommonSubstring(newStrA, newStrB).length(); return temp2 * 1.0 / temp; } /* * 将字符串的所有数据依次写成一行 */ public static String removeSign(String str) { StringBuffer sb = new StringBuffer(); // 遍历字符串str,如果是汉字数字或字母,则追加到sb上面 for (char item : str.toCharArray()) if (charReg(item)) { sb.append(item); } return sb.toString(); } /* * 判断字符是否为汉字,数字和字母, 因为对符号进行相似度比较没有实际意义,故符号不加入考虑范围。 */ public static boolean charReg(char charValue) { return (charValue >= 0x4E00 && charValue <= 0X9FA5) || (charValue >= 'a' && charValue <= 'z') || (charValue >= 'A' && charValue <= 'Z') || (charValue >= '0' && charValue <= '9'); } /* * 求公共子串,采用动态规划算法。 其不要求所求得的字符在所给的字符串中是连续的。 */ public static String longestCommonSubstring(String strA, String strB) { char[] chars_strA = strA.toCharArray(); char[] chars_strB = strB.toCharArray(); int m = chars_strA.length; int n = chars_strB.length; /* * 初始化矩阵数据,matrix[0][0]的值为0, * 如果字符数组chars_strA和chars_strB的对应位相同,则matrix[i][j]的值为左上角的值加1, * 否则,matrix[i][j]的值等于左上方最近两个位置的较大值, 矩阵中其余各点的值为0. */ int[][] matrix = new int[m + 1][n + 1]; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (chars_strA[i - 1] == chars_strB[j - 1]) matrix[i][j] = matrix[i - 1][j - 1] + 1; else matrix[i][j] = Math.max(matrix[i][j - 1], matrix[i - 1][j]); } } /* * 矩阵中,如果matrix[m][n]的值不等于matrix[m-1][n]的值也不等于matrix[m][n-1]的值, * 则matrix[m][n]对应的字符为相似字符元,并将其存入result数组中。 */ char[] result = new char[matrix[m][n]]; int currentIndex = result.length - 1; while (matrix[m][n] != 0) { if (matrix[n] == matrix[n - 1]) n--; else if (matrix[m][n] == matrix[m - 1][n]) m--; else { result[currentIndex] = chars_strA[m - 1]; currentIndex--; n--; m--; } } return new String(result); } /* * 结果转换成百分比形式 */ public static String similarityResult(double resule) { return NumberFormat.getPercentInstance(new Locale("en ", "US ")).format(resule); }}
阅读全文
0 0
- 计算两段文本的相识度
- 计算字符串相识度
- spark 新闻相识度计算
- 计算两段文字的KL距离
- Java识别两张图片相识度
- C# 计算两个字符串相识度
- 查找两段文本中相同的词句
- 计算空间两直线公垂线段的两个垂足
- 文本段的整体复制
- 计算两段yuv格式视频流中每一帧的psnr值
- 空间两条直线段的最短距离及最近点计算
- 不曾相识的时候
- ibatis的初相识
- UML的初相识
- 收集的两段代码
- 两段人生的交集
- 两段有意思的函数
- 两段有用的函数
- Go mobile(〇)
- leetcode 354. Russian Doll Envelopes & leetcode 300 Longest Increasing Subsequence
- java执行jar出现编码问题的解决方案,Could not decode a text frame as UTF-8
- mysql索引
- Android数据库(SQLite)框架(7)——使用LitePal聚合函数
- 计算两段文本的相识度
- 25 web.xml配置详解及示例
- 用openssl进行rsa的加密与解密(linux,C++版)
- 多重背包问题
- 虚函数的作用
- NOIP2016 T5 蚯蚓
- 2、Hyperledger Fabric 交易流程
- struts.xml配置说明
- 机器学习常用算法总结