01C语言的概述
2015-08-17 15:58
507 查看
package com.keertech.mwb.util;
import java.text.NumberFormat;
import java.util.Locale;
/**
* 比较两个字符串的相似度
*/
public class Similarity {
public static void main(String[] args) {
String strA = "我是湖南人dfgh";
String strB = "我是湖南人";
double result=SimilarDegree(strA, strB);
if(result>=0.7){
System.out.println("相似度很高!" +similarityResult(result)+result);
}else{
System.out.println("相似度不高"+similarityResult(result)+result);
}
System.out.println();
}
/**
* 相似度转百分比
*/
public static String similarityResult(double resule){
return NumberFormat.getPercentInstance(new Locale( "en ", "US ")).format(resule);
}
/**
* 相似度比较
* @param strA
* @param strB
* @return
*/
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;
}
private static String removeSign(String str) {
StringBuffer sb = new StringBuffer();
for (char item : str.toCharArray())
if (charReg(item)){
//System.out.println("--"+item);
sb.append(item);
}
return sb.toString();
}
private static boolean charReg(char charValue) {
return (charValue >= 0x4E00 && charValue <= 0X9FA5)
|| (charValue >= 'a' && charValue <= 'z')
|| (charValue >= 'A' && charValue <= 'Z')
|| (charValue >= '0' && charValue <= '9');
}
private 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;
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]);
}
}
char[] result = new char[matrix
];
int currentIndex = result.length - 1;
while (matrix
!= 0) {
if (matrix
== matrix
[n - 1])
n--;
else if (matrix
== matrix[m - 1]
)
m--;
else {
result[currentIndex] = chars_strA[m - 1];
currentIndex--;
n--;
m--;
}
}
return new String(result);
}
}
import java.text.NumberFormat;
import java.util.Locale;
/**
* 比较两个字符串的相似度
*/
public class Similarity {
public static void main(String[] args) {
String strA = "我是湖南人dfgh";
String strB = "我是湖南人";
double result=SimilarDegree(strA, strB);
if(result>=0.7){
System.out.println("相似度很高!" +similarityResult(result)+result);
}else{
System.out.println("相似度不高"+similarityResult(result)+result);
}
System.out.println();
}
/**
* 相似度转百分比
*/
public static String similarityResult(double resule){
return NumberFormat.getPercentInstance(new Locale( "en ", "US ")).format(resule);
}
/**
* 相似度比较
* @param strA
* @param strB
* @return
*/
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;
}
private static String removeSign(String str) {
StringBuffer sb = new StringBuffer();
for (char item : str.toCharArray())
if (charReg(item)){
//System.out.println("--"+item);
sb.append(item);
}
return sb.toString();
}
private static boolean charReg(char charValue) {
return (charValue >= 0x4E00 && charValue <= 0X9FA5)
|| (charValue >= 'a' && charValue <= 'z')
|| (charValue >= 'A' && charValue <= 'Z')
|| (charValue >= '0' && charValue <= '9');
}
private 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;
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]);
}
}
char[] result = new char[matrix
];
int currentIndex = result.length - 1;
while (matrix
!= 0) {
if (matrix
== matrix
[n - 1])
n--;
else if (matrix
== matrix[m - 1]
)
m--;
else {
result[currentIndex] = chars_strA[m - 1];
currentIndex--;
n--;
m--;
}
}
return new String(result);
}
}
相关文章推荐
- C++基本数据类型
- JAVA Socket编程和C++ Socket编程有什么不同
- [LeetCode] Sort List
- [C++]Single Number III单个数字3
- 漫话C++0x
- java 和 C++ Socket通信(java作为服务端server,C++作为客户端client,解决中文乱码问题GBK和UTF8)
- C语言字符串面试题目总结
- C++中数字与字符串之间的转换
- Java与C++Socket通讯注意事项
- C语言的冒泡排序和快速排序算法使用实例
- VC++弹出U盘
- java与C++之间进行SOCKET通讯要点简要解析
- 黑马程序员-[C语言] 第一篇:运算符、程序结构总结
- C语言求最小公倍数和最大公约数三种算法
- C++ STL学习
- [C++]Single Number 单个数字
- C语言中的左移与右移
- poj1979广搜c语言
- c++设计模式
- C语言 枚举 enum