Similarity of two strings to distinguish

static void main public (String [] args) { 
// to compare two strings
; String str1 = "I let him play to the other party Guo Degang more"
cover String str2 = "My style of play to go local coverage of G ";
Long Start = System.currentTimeMillis ();
Levenshtein (str1.toLowerCase (), str2.toLowerCase ());
Long End = System.currentTimeMillis ();
System.out.println (End - Start );
}

/ **
*
* @param str1
* @param str2
* /
public static void Levenshtein (str1 string, string str2) {
// calculate the lengths of the two strings.
LEN1 str1.length = int ();
int LEN2 str2.length = ();
// build the above said array larger than the length of a space character
int [] [] dif = new int [len1 + 1] [len2 + 1 ];
// initial value, step B.
for (int A = 0; <= LEN1 A; A ++) {
DIF [A] [0] = A;
}
for (int A = 0; <= LEN2 A; A ++) {
DIF [0] [A] = A ;
}
// calculate the two characters are the same, value calculation upper left
int TEMP;
for (int I =. 1; I <= LEN1; I ++) {
for (int J =. 1; J <= LEN2; J ++) {
iF ( str1.charAt (I -. 1) == str2.charAt (J -. 1)) {
TEMP = 0;
} the else {
TEMP =. 1;
}
// take three smallest values
dif [i] [j] = min (DIF [I -. 1] [J -. 1] + TEMP, DIF [I] [J -. 1] +. 1,
DIF [I -. 1] [J] +. 1);
}
}
System.out.println ( "character string \ "" + str1 + "\ " \ and "" + str2 + "\" compare ");
// takes a value lower right corner of the array, the same comparison representative of different positions of different character strings
System.out.println ( "Step Difference:" + DIF [LEN1] [LEN2]);
// calculate the similarity
float similarity = 1 - (float) dif [len1] [len2] / Math.max (str1.length ( ), str2.length ());
System.out.println ( "similarity:" similarity +);
}

// get the minimum
Private static int min (int ... IS) {
int min = Integer.MAX_VALUE;
for (int I: IS) {
IF (min> I) {
min = I;
}
}
return min;
}

Guess you like

Origin www.cnblogs.com/liyiren/p/12467304.html