Github user HuJiayin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7115#discussion_r34001741
  
    --- Diff: 
unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java ---
    @@ -211,4 +215,57 @@ public boolean equals(final Object other) {
       public int hashCode() {
         return Arrays.hashCode(bytes);
       }
    +
    +  private char[] getSoundexMapping() {
    +    return this.soundexMapping;
    +  }
    +
    +  private char map(final char ch) {
    +    final int index = ch - 'A';
    +    if (index < 0 || index >= this.getSoundexMapping().length) {
    +      throw new IllegalArgumentException("The character is not mapped: " + 
ch);
    +    }
    +    return this.getSoundexMapping()[index];
    +  }
    +
    +  private char getMappingCode(final String str, final int index) {
    +    // map() throws IllegalArgumentException
    +    final char mappedChar = this.map(str.charAt(index));
    +    // HW rule check
    +    if (index > 1 && mappedChar != '0') {
    +      final char hwChar = str.charAt(index - 1);
    +      if ('H' == hwChar || 'W' == hwChar) {
    +        final char preHWChar = str.charAt(index - 2);
    +        final char firstCode = this.map(preHWChar);
    +        if (firstCode == mappedChar || 'H' == preHWChar || 'W' == 
preHWChar) {
    +          return 0;
    +        }
    +      }
    +    }
    +    return mappedChar;
    +  }
    +
    +  public String soundex(String str) {
    +    if (str == null) {
    +      return null;
    +    }
    +    if (str.length() == 0) {
    +      return str;
    +    }
    +    final 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);
    --- End diff --
    
    You can call UTF8String.fromString() after call this function.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to