Here you can find the source of getEquality(String str1, String str2)
Parameter | Description |
---|---|
str1 | First string to analyze |
str2 | Second string to analyze |
public static float getEquality(String str1, String str2)
//package com.java2s; import java.util.*; public class Main { /**/*from ww w .j a v a 2 s .co m*/ * Analyze two strings to determine how much they differ. * * @param str1 First string to analyze * @param str2 Second string to analyze * @return 1.0 if they are identical */ public static float getEquality(String str1, String str2) { String lineDelimiter = System.getProperty("line.separator"); StringTokenizer st1 = new StringTokenizer(str1, lineDelimiter); StringTokenizer st2 = new StringTokenizer(str2, lineDelimiter); // Keep track of the line statistics in order to calculate equality int linecount1 = 0; // Total number of lines in the first string int linecount2 = 0; // Total number of lines in the second string int samecount = 0; // Total number of lines that match int addedcount = 0; // Total number of lines added int deletedcount = 0; // Total number of lines deleted // Count the number of lines in each string linecount1 = st1.countTokens(); linecount2 = st2.countTokens(); // Define an array to contain the lines in each string String[] x = new String[linecount1]; // lines in first string String[] y = new String[linecount2]; // lines in second string // Keep track of the current line in each string int M = 0; // number of lines of first string int N = 0; // number of lines of second string // Break the strings up into an array of lines while (st1.hasMoreTokens()) { x[M++] = st1.nextToken(); } while (st2.hasMoreTokens()) { y[N++] = st2.nextToken(); } // opt[i][j] = length of LCS of x[i..M] and y[j..N] int[][] opt = new int[M + 1][N + 1]; // compute length of LCS and all subproblems via dynamic programming for (int i = M - 1; i >= 0; i--) { for (int j = N - 1; j >= 0; j--) { if (x[i].equals(y[j])) { opt[i][j] = opt[i + 1][j + 1] + 1; } else { opt[i][j] = Math.max(opt[i + 1][j], opt[i][j + 1]); } } } // recover LCS itself and print out non-matching lines to standard output int i = 0, j = 0; while (i < M && j < N) { // Determine if the current lines differ if (x[i].equals(y[j])) { // No change: x[i] and y[j] samecount++; i++; j++; } else if (opt[i + 1][j] >= opt[i][j + 1]) { deletedcount++; i++; // Line deleted: x[i] } else { addedcount++; j++; // Line added: y[j] } } // dump out one remainder of one string if the other is exhausted while (i < M || j < N) { if (i == M) { addedcount++; j++; // Line added: y[j] } else if (j == N) { deletedcount++; i++; // Line deleted: x[i] } } // Calculate a measure of equality using the diff metrics int total = addedcount + deletedcount + samecount; float equality = (float) samecount / (float) total; return equality; } }