/**
  * Retreives the Soundex code for a given String object.
  *
  * @param str String to encode using the Soundex algorithm
  * @return A soundex code for the String supplied
  * @throws IllegalArgumentException if a character is not mapped
  */
 @DSGenerator(
     tool_name = "Doppelganger",
     tool_version = "2.0",
     generated_on = "2014-09-03 14:59:48.718 -0400",
     hash_original_method = "0EBBB65C00DFAFB7B6E0400F5755712C",
     hash_generated_method = "94B188BA634D9793A5790601BA8B7F45")
 public String soundex(String str) {
   if (str == null) {
     return null;
   }
   str = SoundexUtils.clean(str);
   if (str.length() == 0) {
     return str;
   }
   char out[] = {'0', '0', '0', '0'};
   char last, mapped;
   int incount = 1, count = 1;
   out[0] = str.charAt(0);
   last = getMappingCode(str, 0);
   while ((incount < str.length()) && (count < out.length)) {
     mapped = getMappingCode(str, incount++);
     if (mapped != 0) {
       if ((mapped != '0') && (mapped != last)) {
         out[count++] = mapped;
       }
       last = mapped;
     }
   }
   return new String(out);
 }
Exemple #2
0
 /**
  * Retreives the Soundex code for a given String object.
  *
  * @param str String to encode using the Soundex algorithm
  * @return A soundex code for the String supplied
  * @throws IllegalArgumentException if a character is not mapped
  */
 public String soundex(String str) {
   if (str == null) {
     return null;
   }
   str = SoundexUtils.clean(str);
   if (str.length() == 0) {
     return str;
   }
   char out[] = {'0', '0', '0', '0'};
   char last, mapped;
   int incount = 1, count = 1;
   out[0] = str.charAt(0);
   // getMappingCode() throws IllegalArgumentException
   last = getMappingCode(str, 0);
   while ((incount < str.length()) && (count < out.length)) {
     mapped = getMappingCode(str, incount++);
     if (mapped != 0) {
       if ((mapped != '0') && (mapped != last)) {
         out[count++] = mapped;
       }
       last = mapped;
     }
   }
   return new String(out);
 }
 /**
  * Encodes the Strings and returns the number of characters in the two encoded Strings that are
  * the same. This return value ranges from 0 through 4: 0 indicates little or no similarity, and 4
  * indicates strong similarity or identical values.
  *
  * @param s1 A String that will be encoded and compared.
  * @param s2 A String that will be encoded and compared.
  * @return The number of characters in the two encoded Strings that are the same from 0 to 4.
  * @see <a
  *     href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp">
  *     MS T-SQL DIFFERENCE </a>
  * @throws EncoderException if an error occurs encoding one of the strings
  * @since 1.3
  */
 @DSGenerator(
     tool_name = "Doppelganger",
     tool_version = "2.0",
     generated_on = "2014-09-03 14:59:48.711 -0400",
     hash_original_method = "9372A1D570FF0E071E2A73F0471280C8",
     hash_generated_method = "73E7D5E07B73B540EF5D01C579CF6548")
 public int difference(String s1, String s2) throws EncoderException {
   return SoundexUtils.difference(this, s1, s2);
 }
Exemple #4
0
  public String nysiis(String str) {
    if (str == null) {
      return null;
    }
    str = SoundexUtils.clean(str);
    if (str.length() == 0) {
      return str;
    }
    str = PAT_MAC.matcher(str).replaceFirst("MCC");
    str = PAT_KN.matcher(str).replaceFirst("NN");
    str = PAT_K.matcher(str).replaceFirst("C");
    str = PAT_PH_PF.matcher(str).replaceFirst("FF");
    str = PAT_SCH.matcher(str).replaceFirst("SSS");

    str = PAT_EE_IE.matcher(str).replaceFirst("Y");
    str = PAT_DT_ETC.matcher(str).replaceFirst("D");

    StringBuilder key = new StringBuilder(str.length());
    key.append(str.charAt(0));

    char[] chars = str.toCharArray();
    int len = chars.length;
    for (int i = 1; i < len; i++) {
      char next = i < len - 1 ? chars[(i + 1)] : ' ';
      char aNext = i < len - 2 ? chars[(i + 2)] : ' ';
      char[] transcoded = transcodeRemaining(chars[(i - 1)], chars[i], next, aNext);
      System.arraycopy(transcoded, 0, chars, i, transcoded.length);
      if (chars[i] != chars[(i - 1)]) {
        key.append(chars[i]);
      }
    }
    if (key.length() > 1) {
      char lastChar = key.charAt(key.length() - 1);
      if (lastChar == 'S') {
        key.deleteCharAt(key.length() - 1);
        lastChar = key.charAt(key.length() - 1);
      }
      if (key.length() > 2) {
        char last2Char = key.charAt(key.length() - 2);
        if ((last2Char == 'A') && (lastChar == 'Y')) {
          key.deleteCharAt(key.length() - 2);
        }
      }
      if (lastChar == 'A') {
        key.deleteCharAt(key.length() - 1);
      }
    }
    String string = key.toString();
    return isStrict() ? string.substring(0, Math.min(6, string.length())) : string;
  }
  /**
   * Retrieves the NYSIIS code for a given String object.
   *
   * @param str String to encode using the NYSIIS algorithm
   * @return A NYSIIS code for the String supplied
   */
  public String nysiis(String str) {
    if (str == null) {
      return null;
    }

    // Use the same clean rules as Soundex
    str = SoundexUtils.clean(str);

    if (str.length() == 0) {
      return str;
    }

    // Translate first characters of name:
    // MAC -> MCC, KN -> NN, K -> C, PH | PF -> FF, SCH -> SSS
    str = PAT_MAC.matcher(str).replaceFirst("MCC");
    str = PAT_KN.matcher(str).replaceFirst("NN");
    str = PAT_K.matcher(str).replaceFirst("C");
    str = PAT_PH_PF.matcher(str).replaceFirst("FF");
    str = PAT_SCH.matcher(str).replaceFirst("SSS");

    // Translate last characters of name:
    // EE -> Y, IE -> Y, DT | RT | RD | NT | ND -> D
    str = PAT_EE_IE.matcher(str).replaceFirst("Y");
    str = PAT_DT_ETC.matcher(str).replaceFirst("D");

    // First character of key = first character of name.
    StringBuilder key = new StringBuilder(str.length());
    key.append(str.charAt(0));

    // Transcode remaining characters, incrementing by one character each
    // time
    final char[] chars = str.toCharArray();
    final int len = chars.length;

    for (int i = 1; i < len; i++) {
      final char next = i < len - 1 ? chars[i + 1] : SPACE;
      final char aNext = i < len - 2 ? chars[i + 2] : SPACE;
      final char[] transcoded = transcodeRemaining(chars[i - 1], chars[i], next, aNext);
      System.arraycopy(transcoded, 0, chars, i, transcoded.length);

      // only append the current char to the key if it is different from
      // the last one
      if (chars[i] != chars[i - 1]) {
        key.append(chars[i]);
      }
    }

    if (key.length() > 1) {
      char lastChar = key.charAt(key.length() - 1);

      // If last character is S, remove it.
      if (lastChar == 'S') {
        key.deleteCharAt(key.length() - 1);
        lastChar = key.charAt(key.length() - 1);
      }

      if (key.length() > 2) {
        final char last2Char = key.charAt(key.length() - 2);
        // If last characters are AY, replace with Y.
        if (last2Char == 'A' && lastChar == 'Y') {
          key.deleteCharAt(key.length() - 2);
        }
      }

      // If last character is A, remove it.
      if (lastChar == 'A') {
        key.deleteCharAt(key.length() - 1);
      }
    }

    final String string = key.toString();
    return this.isStrict() ? string.substring(0, Math.min(TRUE_LENGTH, string.length())) : string;
  }
Exemple #6
0
 /**
  * Encodes the Strings and returns the number of characters in the two encoded Strings that are
  * the same. This return value ranges from 0 through 4: 0 indicates little or no similarity, and 4
  * indicates strong similarity or identical values.
  *
  * @param s1 A String that will be encoded and compared.
  * @param s2 A String that will be encoded and compared.
  * @return The number of characters in the two encoded Strings that are the same from 0 to 4.
  * @see SoundexUtils#difference(StringEncoder,String,String)
  * @see <a
  *     href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp">
  *     MS T-SQL DIFFERENCE </a>
  * @throws EncoderException if an error occurs encoding one of the strings
  * @since 1.3
  */
 public int difference(String s1, String s2) throws EncoderException {
   return SoundexUtils.difference(this, s1, s2);
 }