    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 *      https://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 */
    017package org.apache.commons.codec.language;
    018
    019import java.util.Locale;
    020
    021import org.apache.commons.codec.EncoderException;
    022import org.apache.commons.codec.StringEncoder;
    023
    024/**
    025 * Match Rating Approach Phonetic Algorithm Developed by <CITE>Western Airlines</CITE> in 1977.
    026 * <p>
    027 * This class is immutable and thread-safe.
    028 * </p>
    029 *
    030 * @see <a href="https://en.wikipedia.org/wiki/Match_rating_approach">Wikipedia - Match Rating Approach</a>
    031 * @since 1.8
    032 */
    033public class MatchRatingApproachEncoder implements StringEncoder {
    034
    035    private static final String SPACE = " ";
    036
    037    private static final String EMPTY = "";
    038
    039    /**
    040     * The plain letter equivalent of the accented letters.
    041     */
    042    private static final String PLAIN_ASCII = "AaEeIiOoUu" + // grave
    043            "AaEeIiOoUuYy" + // acute
    044            "AaEeIiOoUuYy" + // circumflex
    045            "AaOoNn" + // tilde
    046            "AaEeIiOoUuYy" + // umlaut
    047            "Aa" + // ring
    048            "Cc" + // cedilla
    049            "OoUu"; // double acute
    050
    051    /**
    052     * Unicode characters corresponding to various accented letters. For example: \u00DA is U acute etc...
    053     */
    054    private static final String UNICODE = "\u00C0\u00E0\u00C8\u00E8\u00CC\u00EC\u00D2\u00F2\u00D9\u00F9" +
    055            "\u00C1\u00E1\u00C9\u00E9\u00CD\u00ED\u00D3\u00F3\u00DA\u00FA\u00DD\u00FD" +
    056            "\u00C2\u00E2\u00CA\u00EA\u00CE\u00EE\u00D4\u00F4\u00DB\u00FB\u0176\u0177" +
    057            "\u00C3\u00E3\u00D5\u00F5\u00D1\u00F1" +
    058            "\u00C4\u00E4\u00CB\u00EB\u00CF\u00EF\u00D6\u00F6\u00DC\u00FC\u0178\u00FF" +
    059            "\u00C5\u00E5" + "\u00C7\u00E7" + "\u0150\u0151\u0170\u0171";
    060
    061    private static final String[] DOUBLE_CONSONANT =
    062            { "BB", "CC", "DD", "FF", "GG", "HH", "JJ", "KK", "LL", "MM", "NN", "PP", "QQ", "RR", "SS",
    063                   "TT", "VV", "WW", "XX", "YY", "ZZ" };
    064
    065    /**
    066     * Constructs a new instance.
    067     */
    068    public MatchRatingApproachEncoder() {
    069        // empty
    070    }
    071
    072    /**
    073     * Cleans up a name: 1. Upper-cases everything 2. Removes some common punctuation 3. Removes accents 4. Removes any
    074     * spaces.
    075     *
    076     * <h2>API Usage</h2>
    077     * <p>
    078     * Consider this method private, it is package protected for unit testing only.
    079     * </p>
    080     *
    081     * @param name
    082     *            The name to be cleaned
    083     * @return The cleaned name
    084     */
    085    String cleanName(final String name) {
    086        String upperName = name.toUpperCase(Locale.ENGLISH);
    087
    088        final String[] charsToTrim = { "\\-", "[&]", "\\'", "\\.", "[\\,]" };
    089        for (final String str : charsToTrim) {
    090            upperName = upperName.replaceAll(str, EMPTY);
    091        }
    092
    093        upperName = removeAccents(upperName);
    094        return upperName.replaceAll("\\s+", EMPTY);
    095    }
    096
    097    /**
    098     * Encodes an Object using the Match Rating Approach algorithm. Method is here to satisfy the requirements of the
    099     * Encoder interface Throws an EncoderException if input object is not of type {@link String}.
    100     *
    101     * @param pObject
    102     *            Object to encode
    103     * @return An object (or type {@link String}) containing the Match Rating Approach code which corresponds to the
    104     *         String supplied.
    105     * @throws EncoderException
    106     *             if the parameter supplied is not of type {@link String}
    107     */
    108    @Override
    109    public final Object encode(final Object pObject) throws EncoderException {
    110        if (!(pObject instanceof String)) {
    111            throw new EncoderException(
    112                    "Parameter supplied to Match Rating Approach encoder is not of type java.lang.String");
    113        }
    114        return encode((String) pObject);
    115    }
    116
    117    /**
    118     * Encodes a String using the Match Rating Approach (MRA) algorithm.
    119     *
    120     * @param name
    121     *            String object to encode
    122     * @return The MRA code corresponding to the String supplied
    123     */
    124    @Override
    125    public final String encode(String name) {
    126        // Bulletproof for trivial input - NINO
    127        if (name == null || EMPTY.equalsIgnoreCase(name) || SPACE.equalsIgnoreCase(name) || name.length() == 1) {
    128            return EMPTY;
    129        }
    130
    131        // Preprocessing
    132        name = cleanName(name);
    133
    134        // Bulletproof if name becomes empty after cleanName(name)
    135        if (SPACE.equals(name) || name.isEmpty()) {
    136            return EMPTY;
    137        }
    138
    139        // BEGIN: Actual encoding part of the algorithm...
    140        // 1. Delete all vowels unless the vowel begins the word
    141        name = removeVowels(name);
    142
    143        // Bulletproof if name becomes empty after removeVowels(name)
    144        if (SPACE.equals(name) || name.isEmpty()) {
    145            return EMPTY;
    146        }
    147
    148        // 2. Remove second consonant from any double consonant
    149        name = removeDoubleConsonants(name);
    150
    151        return getFirst3Last3(name);
    152    }
    153
    154    /**
    155     * Gets the first and last 3 letters of a name (if &gt; 6 characters) Else just returns the name.
    156     *
    157     * <h2>API Usage</h2>
    158     * <p>
    159     * Consider this method private, it is package protected for unit testing only.
    160     * </p>
    161     *
    162     * @param name
    163     *            The string to get the substrings from
    164     * @return Annexed first and last 3 letters of input word.
    165     */
    166    String getFirst3Last3(final String name) {
    167        final int nameLength = name.length();
    168
    169        if (nameLength > 6) {
    170            final String firstThree = name.substring(0, 3);
    171            final String lastThree = name.substring(nameLength - 3, nameLength);
    172            return firstThree + lastThree;
    173        }
    174        return name;
    175    }
    176
    177    /**
    178     * Obtains the min rating of the length sum of the 2 names. In essence the larger the sum length the smaller the
    179     * min rating. Values strictly from documentation.
    180     *
    181     * <h2>API Usage</h2>
    182     * <p>
    183     * Consider this method private, it is package protected for unit testing only.
    184     * </p>
    185     *
    186     * @param sumLength
    187     *            The length of 2 strings sent down
    188     * @return The min rating value
    189     */
    190    int getMinRating(final int sumLength) {
    191        int minRating = 0;
    192
    193        if (sumLength <= 4) {
    194            minRating = 5;
    195        } else if (sumLength <= 7) { // already know it is at least 5
    196            minRating = 4;
    197        } else if (sumLength <= 11) { // already know it is at least 8
    198            minRating = 3;
    199        } else if (sumLength == 12) {
    200            minRating = 2;
    201        } else {
    202            minRating = 1; // docs said little here.
    203        }
    204
    205        return minRating;
    206    }
    207
    208    /**
    209     * Determines if two names are homophonous via Match Rating Approach (MRA) algorithm. It should be noted that the
    210     * strings are cleaned in the same way as {@link #encode(String)}.
    211     *
    212     * @param name1
    213     *            First of the 2 strings (names) to compare
    214     * @param name2
    215     *            Second of the 2 names to compare
    216     * @return {@code true} if the encodings are identical {@code false} otherwise.
    217     */
    218    public boolean isEncodeEquals(String name1, String name2) {
    219        // Bulletproof for trivial input - NINO
    220        if (name1 == null || EMPTY.equalsIgnoreCase(name1) || SPACE.equalsIgnoreCase(name1)) {
    221            return false;
    222        }
    223        if (name2 == null || EMPTY.equalsIgnoreCase(name2) || SPACE.equalsIgnoreCase(name2)) {
    224            return false;
    225        }
    226        if (name1.length() == 1 || name2.length() == 1) {
    227            return false;
    228        }
    229        if (name1.equalsIgnoreCase(name2)) {
    230            return true;
    231        }
    232
    233        // Preprocessing
    234        name1 = cleanName(name1);
    235        name2 = cleanName(name2);
    236
    237        // Actual MRA Algorithm
    238
    239        // 1. Remove vowels
    240        name1 = removeVowels(name1);
    241        name2 = removeVowels(name2);
    242
    243        // 2. Remove double consonants
    244        name1 = removeDoubleConsonants(name1);
    245        name2 = removeDoubleConsonants(name2);
    246
    247        // 3. Reduce down to 3 letters
    248        name1 = getFirst3Last3(name1);
    249        name2 = getFirst3Last3(name2);
    250
    251        // 4. Check for length difference - if 3 or greater, then no similarity
    252        // comparison is done
    253        if (Math.abs(name1.length() - name2.length()) >= 3) {
    254            return false;
    255        }
    256
    257        // 5. Obtain the minimum rating value by calculating the length sum of the
    258        // encoded Strings and sending it down.
    259        final int sumLength = Math.abs(name1.length() + name2.length());
    260        final int minRating = getMinRating(sumLength);
    261
    262        // 6. Process the encoded Strings from left to right and remove any
    263        // identical characters found from both Strings respectively.
    264        final int count = leftToRightThenRightToLeftProcessing(name1, name2);
    265
    266        // 7. Each PNI item that has a similarity rating equal to or greater than
    267        // the min is considered to be a good candidate match
    268        return count >= minRating;
    269
    270    }
    271
    272    /**
    273     * Determines if a letter is a vowel.
    274     *
    275     * <h2>API Usage</h2>
    276     * <p>
    277     * Consider this method private, it is package protected for unit testing only.
    278     * </p>
    279     *
    280     * @param letter
    281     *            The letter under investigation
    282     * @return True if a vowel, else false
    283     */
    284    boolean isVowel(final String letter) {
    285        return letter.equalsIgnoreCase("E") || letter.equalsIgnoreCase("A") || letter.equalsIgnoreCase("O") ||
    286               letter.equalsIgnoreCase("I") || letter.equalsIgnoreCase("U");
    287    }
    288
    289    /**
    290     * Processes the names from left to right (first) then right to left removing identical letters in same positions.
    291     * Then subtracts the longer string that remains from 6 and returns this.
    292     *
    293     * <h2>API Usage</h2>
    294     * <p>
    295     * Consider this method private, it is package protected for unit testing only.
    296     * </p>
    297     *
    298     * @param name1
    299     *            name2
    300     * @return the length as above
    301     */
    302    int leftToRightThenRightToLeftProcessing(final String name1, final String name2) {
    303        final char[] name1Char = name1.toCharArray();
    304        final char[] name2Char = name2.toCharArray();
    305
    306        final int name1Size = name1.length() - 1;
    307        final int name2Size = name2.length() - 1;
    308
    309        String name1LtRStart = EMPTY;
    310        String name1LtREnd = EMPTY;
    311
    312        String name2RtLStart = EMPTY;
    313        String name2RtLEnd = EMPTY;
    314
    315        for (int i = 0; i < name1Char.length; i++) {
    316            if (i > name2Size) {
    317                break;
    318            }
    319
    320            name1LtRStart = name1.substring(i, i + 1);
    321            name1LtREnd = name1.substring(name1Size - i, name1Size - i + 1);
    322
    323            name2RtLStart = name2.substring(i, i + 1);
    324            name2RtLEnd = name2.substring(name2Size - i, name2Size - i + 1);
    325
    326            // Left to right...
    327            if (name1LtRStart.equals(name2RtLStart)) {
    328                name1Char[i] = ' ';
    329                name2Char[i] = ' ';
    330            }
    331
    332            // Right to left...
    333            if (name1LtREnd.equals(name2RtLEnd)) {
    334                name1Char[name1Size - i] = ' ';
    335                name2Char[name2Size - i] = ' ';
    336            }
    337        }
    338
    339        // Char arrays -> string & remove extraneous space
    340        final String strA = new String(name1Char).replaceAll("\\s+", EMPTY);
    341        final String strB = new String(name2Char).replaceAll("\\s+", EMPTY);
    342
    343        // Final bit - subtract the longest string from 6 and return this int value
    344        if (strA.length() > strB.length()) {
    345            return Math.abs(6 - strA.length());
    346        }
    347        return Math.abs(6 - strB.length());
    348    }
    349
    350    /**
    351     * Removes accented letters and replaces with non-accented ASCII equivalent Case is preserved.
    352     * http://www.codecodex.com/wiki/Remove_accent_from_letters_%28ex_.%C3%A9_to_e%29
    353     *
    354     * @param accentedWord
    355     *            The word that may have accents in it.
    356     * @return De-accented word
    357     */
    358    String removeAccents(final String accentedWord) {
    359        if (accentedWord == null) {
    360            return null;
    361        }
    362
    363        final StringBuilder sb = new StringBuilder();
    364        final int n = accentedWord.length();
    365
    366        for (int i = 0; i < n; i++) {
    367            final char c = accentedWord.charAt(i);
    368            final int pos = UNICODE.indexOf(c);
    369            if (pos > -1) {
    370                sb.append(PLAIN_ASCII.charAt(pos));
    371            } else {
    372                sb.append(c);
    373            }
    374        }
    375
    376        return sb.toString();
    377    }
    378
    379    /**
    380     * Replaces any double consonant pair with the single letter equivalent.
    381     *
    382     * <h2>API Usage</h2>
    383     * <p>
    384     * Consider this method private, it is package protected for unit testing only.
    385     * </p>
    386     *
    387     * @param name
    388     *            String to have double consonants removed
    389     * @return Single consonant word
    390     */
    391    String removeDoubleConsonants(final String name) {
    392        String replacedName = name.toUpperCase(Locale.ENGLISH);
    393        for (final String dc : DOUBLE_CONSONANT) {
    394            if (replacedName.contains(dc)) {
    395                final String singleLetter = dc.substring(0, 1);
    396                replacedName = replacedName.replace(dc, singleLetter);
    397            }
    398        }
    399        return replacedName;
    400    }
    401
    402    /**
    403     * Deletes all vowels unless the vowel begins the word.
    404     *
    405     * <h2>API Usage</h2>
    406     * <p>
    407     * Consider this method private, it is package protected for unit testing only.
    408     * </p>
    409     *
    410     * @param name
    411     *            The name to have vowels removed
    412     * @return De-voweled word
    413     */
    414    String removeVowels(String name) {
    415        // Extract first letter
    416        final String firstLetter = name.substring(0, 1);
    417
    418        name = name.replace("A", EMPTY);
    419        name = name.replace("E", EMPTY);
    420        name = name.replace("I", EMPTY);
    421        name = name.replace("O", EMPTY);
    422        name = name.replace("U", EMPTY);
    423
    424        name = name.replaceAll("\\s{2,}\\b", SPACE);
    425
    426        // return isVowel(firstLetter) ? (firstLetter + name) : name;
    427        if (isVowel(firstLetter)) {
    428            return firstLetter + name;
    429        }
    430        return name;
    431    }
    432}