Beispiel #1
0
  private static double correlateStrings(String s1, String s2, boolean truncate) {
    int minLength = Math.min(s1.length(), s2.length());
    if (truncate && (minLength == 1)) {
      return s1.charAt(0) == s2.charAt(0) ? 1.0 : 0.0;
    } else if ((s1.length() == 1) && (s2.length() == 1)) {
      return s1.equals(s2) ? 1.0 : 0.0;
    } else if (minLength == 0) {
      return s1.isEmpty() && s2.isEmpty() ? 1.0 : 0;
    }

    // Convert strings to numbers and harmonize length in a method dependent on truncate:
    if (truncate) {
      // Harmonize length by truncation:
      if (s1.length() > minLength) {
        s1 = s1.substring(0, minLength);
      }
      if (s2.length() > minLength) {
        s2 = s2.substring(0, minLength);
      }
    }
    double[] n1 = DuplicateCheck.numberizeString(s1);
    double[] n2 = DuplicateCheck.numberizeString(s2);
    // If truncation is disabled, harmonize length by interpolation:
    if (!truncate) {
      if (n1.length < n2.length) {
        n1 = DuplicateCheck.stretchArray(n1, n2.length);
      } else if (n2.length < n1.length) {
        n2 = DuplicateCheck.stretchArray(n2, n1.length);
      }
    }
    return DuplicateCheck.corrCoef(n1, n2);
  }