    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.codec.language;
    019
    020import java.util.regex.Pattern;
    021
    022import org.apache.commons.codec.EncoderException;
    023import org.apache.commons.codec.StringEncoder;
    024
    025/**
    026 * Encodes a string into a NYSIIS value. NYSIIS is an encoding used to relate similar names, but can also be used as a
    027 * general purpose scheme to find word with similar phonemes.
    028 * <p>
    029 * NYSIIS features an accuracy increase of 2.7% over the traditional Soundex algorithm.
    030 * <p>
    031 * Algorithm description:
    032 * <pre>
    033 * 1. Transcode first characters of name
    034 *   1a. MAC -&gt;   MCC
    035 *   1b. KN  -&gt;   NN
    036 *   1c. K   -&gt;   C
    037 *   1d. PH  -&gt;   FF
    038 *   1e. PF  -&gt;   FF
    039 *   1f. SCH -&gt;   SSS
    040 * 2. Transcode last characters of name
    041 *   2a. EE, IE          -&gt;   Y
    042 *   2b. DT,RT,RD,NT,ND  -&gt;   D
    043 * 3. First character of key = first character of name
    044 * 4. Transcode remaining characters by following these rules, incrementing by one character each time
    045 *   4a. EV  -&gt;   AF  else A,E,I,O,U -&gt; A
    046 *   4b. Q   -&gt;   G
    047 *   4c. Z   -&gt;   S
    048 *   4d. M   -&gt;   N
    049 *   4e. KN  -&gt;   N   else K -&gt; C
    050 *   4f. SCH -&gt;   SSS
    051 *   4g. PH  -&gt;   FF
    052 *   4h. H   -&gt;   If previous or next is nonvowel, previous
    053 *   4i. W   -&gt;   If previous is vowel, previous
    054 *   4j. Add current to key if current != last key character
    055 * 5. If last character is S, remove it
    056 * 6. If last characters are AY, replace with Y
    057 * 7. If last character is A, remove it
    058 * 8. Collapse all strings of repeated characters
    059 * 9. Add original first character of name as first character of key
    060 * </pre>
    061 * <p>
    062 * This class is immutable and thread-safe.
    063 *
    064 * @see <a href="http://en.wikipedia.org/wiki/NYSIIS">NYSIIS on Wikipedia</a>
    065 * @see <a href="http://www.dropby.com/NYSIIS.html">NYSIIS on dropby.com</a>
    066 * @see Soundex
    067 * @since 1.7
    068 */
    069public class Nysiis implements StringEncoder {
    070
    071    private static final char[] CHARS_A   = new char[] { 'A' };
    072    private static final char[] CHARS_AF  = new char[] { 'A', 'F' };
    073    private static final char[] CHARS_C   = new char[] { 'C' };
    074    private static final char[] CHARS_FF  = new char[] { 'F', 'F' };
    075    private static final char[] CHARS_G   = new char[] { 'G' };
    076    private static final char[] CHARS_N   = new char[] { 'N' };
    077    private static final char[] CHARS_NN  = new char[] { 'N', 'N' };
    078    private static final char[] CHARS_S   = new char[] { 'S' };
    079    private static final char[] CHARS_SSS = new char[] { 'S', 'S', 'S' };
    080
    081    private static final Pattern PAT_MAC    = Pattern.compile("^MAC");
    082    private static final Pattern PAT_KN     = Pattern.compile("^KN");
    083    private static final Pattern PAT_K      = Pattern.compile("^K");
    084    private static final Pattern PAT_PH_PF  = Pattern.compile("^(PH|PF)");
    085    private static final Pattern PAT_SCH    = Pattern.compile("^SCH");
    086    private static final Pattern PAT_EE_IE  = Pattern.compile("(EE|IE)$");
    087    private static final Pattern PAT_DT_ETC = Pattern.compile("(DT|RT|RD|NT|ND)$");
    088
    089    private static final char SPACE = ' ';
    090    private static final int TRUE_LENGTH = 6;
    091
    092    /**
    093     * Tests if the given character is a vowel.
    094     *
    095     * @param c
    096     *            the character to test
    097     * @return {@code true} if the character is a vowel, {@code false} otherwise
    098     */
    099    private static boolean isVowel(final char c) {
    100        return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
    101    }
    102
    103    /**
    104     * Transcodes the remaining parts of the String. The method operates on a sliding window, looking at 4 characters at
    105     * a time: [i-1, i, i+1, i+2].
    106     *
    107     * @param prev
    108     *            the previous character
    109     * @param curr
    110     *            the current character
    111     * @param next
    112     *            the next character
    113     * @param aNext
    114     *            the after next character
    115     * @return a transcoded array of characters, starting from the current position
    116     */
    117    private static char[] transcodeRemaining(final char prev, final char curr, final char next, final char aNext) {
    118        // 1. EV -> AF
    119        if (curr == 'E' && next == 'V') {
    120            return CHARS_AF;
    121        }
    122
    123        // A, E, I, O, U -> A
    124        if (isVowel(curr)) {
    125            return CHARS_A;
    126        }
    127
    128        // 2. Q -> G, Z -> S, M -> N
    129        if (curr == 'Q') {
    130            return CHARS_G;
    131        } else if (curr == 'Z') {
    132            return CHARS_S;
    133        } else if (curr == 'M') {
    134            return CHARS_N;
    135        }
    136
    137        // 3. KN -> NN else K -> C
    138        if (curr == 'K') {
    139            if (next == 'N') {
    140                return CHARS_NN;
    141            }
    142            return CHARS_C;
    143        }
    144
    145        // 4. SCH -> SSS
    146        if (curr == 'S' && next == 'C' && aNext == 'H') {
    147            return CHARS_SSS;
    148        }
    149
    150        // PH -> FF
    151        if (curr == 'P' && next == 'H') {
    152            return CHARS_FF;
    153        }
    154
    155        // 5. H -> If previous or next is a non vowel, previous.
    156        if (curr == 'H' && (!isVowel(prev) || !isVowel(next))) {
    157            return new char[] { prev };
    158        }
    159
    160        // 6. W -> If previous is vowel, previous.
    161        if (curr == 'W' && isVowel(prev)) {
    162            return new char[] { prev };
    163        }
    164
    165        return new char[] { curr };
    166    }
    167
    168    /** Indicates the strict mode. */
    169    private final boolean strict;
    170
    171    /**
    172     * Creates an instance of the {@link Nysiis} encoder with strict mode (original form),
    173     * i.e. encoded strings have a maximum length of 6.
    174     */
    175    public Nysiis() {
    176        this(true);
    177    }
    178
    179    /**
    180     * Create an instance of the {@link Nysiis} encoder with the specified strict mode:
    181     *
    182     * <ul>
    183     *  <li>{@code true}: encoded strings have a maximum length of 6</li>
    184     *  <li>{@code false}: encoded strings may have arbitrary length</li>
    185     * </ul>
    186     *
    187     * @param strict
    188     *            the strict mode
    189     */
    190    public Nysiis(final boolean strict) {
    191        this.strict = strict;
    192    }
    193
    194    /**
    195     * Encodes an Object using the NYSIIS algorithm. This method is provided in order to satisfy the requirements of the
    196     * Encoder interface, and will throw an {@link EncoderException} if the supplied object is not of type
    197     * {@link String}.
    198     *
    199     * @param obj
    200     *            Object to encode
    201     * @return An object (or a {@link String}) containing the NYSIIS code which corresponds to the given String.
    202     * @throws EncoderException
    203     *            if the parameter supplied is not of a {@link String}
    204     * @throws IllegalArgumentException
    205     *            if a character is not mapped
    206     */
    207    @Override
    208    public Object encode(final Object obj) throws EncoderException {
    209        if (!(obj instanceof String)) {
    210            throw new EncoderException("Parameter supplied to Nysiis encode is not of type java.lang.String");
    211        }
    212        return this.nysiis((String) obj);
    213    }
    214
    215    /**
    216     * Encodes a String using the NYSIIS algorithm.
    217     *
    218     * @param str
    219     *            A String object to encode
    220     * @return A Nysiis code corresponding to the String supplied
    221     * @throws IllegalArgumentException
    222     *            if a character is not mapped
    223     */
    224    @Override
    225    public String encode(final String str) {
    226        return this.nysiis(str);
    227    }
    228
    229    /**
    230     * Indicates the strict mode for this {@link Nysiis} encoder.
    231     *
    232     * @return {@code true} if the encoder is configured for strict mode, {@code false} otherwise
    233     */
    234    public boolean isStrict() {
    235        return this.strict;
    236    }
    237
    238    /**
    239     * Retrieves the NYSIIS code for a given String object.
    240     *
    241     * @param str
    242     *            String to encode using the NYSIIS algorithm
    243     * @return A NYSIIS code for the String supplied
    244     */
    245    public String nysiis(String str) {
    246        if (str == null) {
    247            return null;
    248        }
    249
    250        // Use the same clean rules as Soundex
    251        str = SoundexUtils.clean(str);
    252
    253        if (str.length() == 0) {
    254            return str;
    255        }
    256
    257        // Translate first characters of name:
    258        // MAC -> MCC, KN -> NN, K -> C, PH | PF -> FF, SCH -> SSS
    259        str = PAT_MAC.matcher(str).replaceFirst("MCC");
    260        str = PAT_KN.matcher(str).replaceFirst("NN");
    261        str = PAT_K.matcher(str).replaceFirst("C");
    262        str = PAT_PH_PF.matcher(str).replaceFirst("FF");
    263        str = PAT_SCH.matcher(str).replaceFirst("SSS");
    264
    265        // Translate last characters of name:
    266        // EE -> Y, IE -> Y, DT | RT | RD | NT | ND -> D
    267        str = PAT_EE_IE.matcher(str).replaceFirst("Y");
    268        str = PAT_DT_ETC.matcher(str).replaceFirst("D");
    269
    270        // First character of key = first character of name.
    271        final StringBuilder key = new StringBuilder(str.length());
    272        key.append(str.charAt(0));
    273
    274        // Transcode remaining characters, incrementing by one character each time
    275        final char[] chars = str.toCharArray();
    276        final int len = chars.length;
    277
    278        for (int i = 1; i < len; i++) {
    279            final char next = i < len - 1 ? chars[i + 1] : SPACE;
    280            final char aNext = i < len - 2 ? chars[i + 2] : SPACE;
    281            final char[] transcoded = transcodeRemaining(chars[i - 1], chars[i], next, aNext);
    282            System.arraycopy(transcoded, 0, chars, i, transcoded.length);
    283
    284            // only append the current char to the key if it is different from the last one
    285            if (chars[i] != chars[i - 1]) {
    286                key.append(chars[i]);
    287            }
    288        }
    289
    290        if (key.length() > 1) {
    291            char lastChar = key.charAt(key.length() - 1);
    292
    293            // If last character is S, remove it.
    294            if (lastChar == 'S') {
    295                key.deleteCharAt(key.length() - 1);
    296                lastChar = key.charAt(key.length() - 1);
    297            }
    298
    299            if (key.length() > 2) {
    300                final char last2Char = key.charAt(key.length() - 2);
    301                // If last characters are AY, replace with Y.
    302                if (last2Char == 'A' && lastChar == 'Y') {
    303                    key.deleteCharAt(key.length() - 2);
    304                }
    305            }
    306
    307            // If last character is A, remove it.
    308            if (lastChar == 'A') {
    309                key.deleteCharAt(key.length() - 1);
    310            }
    311        }
    312
    313        final String string = key.toString();
    314        return this.isStrict() ? string.substring(0, Math.min(TRUE_LENGTH, string.length())) : string;
    315    }
    316
    317}