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 */
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     * Cleans up a name: 1. Upper-cases everything 2. Removes some common punctuation 3. Removes accents 4. Removes any
067     * spaces.
068     *
069     * <h2>API Usage</h2>
070     * <p>
071     * Consider this method private, it is package protected for unit testing only.
072     * </p>
073     *
074     * @param name
075     *            The name to be cleaned
076     * @return The cleaned name
077     */
078    String cleanName(final String name) {
079        String upperName = name.toUpperCase(Locale.ENGLISH);
080
081        final String[] charsToTrim = { "\\-", "[&]", "\\'", "\\.", "[\\,]" };
082        for (final String str : charsToTrim) {
083            upperName = upperName.replaceAll(str, EMPTY);
084        }
085
086        upperName = removeAccents(upperName);
087        return upperName.replaceAll("\\s+", EMPTY);
088    }
089
090    /**
091     * Encodes an Object using the Match Rating Approach algorithm. Method is here to satisfy the requirements of the
092     * Encoder interface Throws an EncoderException if input object is not of type {@link String}.
093     *
094     * @param pObject
095     *            Object to encode
096     * @return An object (or type {@link String}) containing the Match Rating Approach code which corresponds to the
097     *         String supplied.
098     * @throws EncoderException
099     *             if the parameter supplied is not of type {@link String}
100     */
101    @Override
102    public final Object encode(final Object pObject) throws EncoderException {
103        if (!(pObject instanceof String)) {
104            throw new EncoderException(
105                    "Parameter supplied to Match Rating Approach encoder is not of type java.lang.String");
106        }
107        return encode((String) pObject);
108    }
109
110    /**
111     * Encodes a String using the Match Rating Approach (MRA) algorithm.
112     *
113     * @param name
114     *            String object to encode
115     * @return The MRA code corresponding to the String supplied
116     */
117    @Override
118    public final String encode(String name) {
119        // Bulletproof for trivial input - NINO
120        if (name == null || EMPTY.equalsIgnoreCase(name) || SPACE.equalsIgnoreCase(name) || name.length() == 1) {
121            return EMPTY;
122        }
123
124        // Preprocessing
125        name = cleanName(name);
126
127        // Bulletproof if name becomes empty after cleanName(name)
128        if (SPACE.equals(name) || name.isEmpty()) {
129            return EMPTY;
130        }
131
132        // BEGIN: Actual encoding part of the algorithm...
133        // 1. Delete all vowels unless the vowel begins the word
134        name = removeVowels(name);
135
136        // Bulletproof if name becomes empty after removeVowels(name)
137        if (SPACE.equals(name) || name.isEmpty()) {
138            return EMPTY;
139        }
140
141        // 2. Remove second consonant from any double consonant
142        name = removeDoubleConsonants(name);
143
144        return getFirst3Last3(name);
145    }
146
147    /**
148     * Gets the first and last 3 letters of a name (if &gt; 6 characters) Else just returns the name.
149     *
150     * <h2>API Usage</h2>
151     * <p>
152     * Consider this method private, it is package protected for unit testing only.
153     * </p>
154     *
155     * @param name
156     *            The string to get the substrings from
157     * @return Annexed first and last 3 letters of input word.
158     */
159    String getFirst3Last3(final String name) {
160        final int nameLength = name.length();
161
162        if (nameLength > 6) {
163            final String firstThree = name.substring(0, 3);
164            final String lastThree = name.substring(nameLength - 3, nameLength);
165            return firstThree + lastThree;
166        }
167        return name;
168    }
169
170    /**
171     * Obtains the min rating of the length sum of the 2 names. In essence the larger the sum length the smaller the
172     * min rating. Values strictly from documentation.
173     *
174     * <h2>API Usage</h2>
175     * <p>
176     * Consider this method private, it is package protected for unit testing only.
177     * </p>
178     *
179     * @param sumLength
180     *            The length of 2 strings sent down
181     * @return The min rating value
182     */
183    int getMinRating(final int sumLength) {
184        int minRating = 0;
185
186        if (sumLength <= 4) {
187            minRating = 5;
188        } else if (sumLength <= 7) { // already know it is at least 5
189            minRating = 4;
190        } else if (sumLength <= 11) { // already know it is at least 8
191            minRating = 3;
192        } else if (sumLength == 12) {
193            minRating = 2;
194        } else {
195            minRating = 1; // docs said little here.
196        }
197
198        return minRating;
199    }
200
201    /**
202     * Determines if two names are homophonous via Match Rating Approach (MRA) algorithm. It should be noted that the
203     * strings are cleaned in the same way as {@link #encode(String)}.
204     *
205     * @param name1
206     *            First of the 2 strings (names) to compare
207     * @param name2
208     *            Second of the 2 names to compare
209     * @return {@code true} if the encodings are identical {@code false} otherwise.
210     */
211    public boolean isEncodeEquals(String name1, String name2) {
212        // Bulletproof for trivial input - NINO
213        if (name1 == null || EMPTY.equalsIgnoreCase(name1) || SPACE.equalsIgnoreCase(name1)) {
214            return false;
215        }
216        if (name2 == null || EMPTY.equalsIgnoreCase(name2) || SPACE.equalsIgnoreCase(name2)) {
217            return false;
218        }
219        if (name1.length() == 1 || name2.length() == 1) {
220            return false;
221        }
222        if (name1.equalsIgnoreCase(name2)) {
223            return true;
224        }
225
226        // Preprocessing
227        name1 = cleanName(name1);
228        name2 = cleanName(name2);
229
230        // Actual MRA Algorithm
231
232        // 1. Remove vowels
233        name1 = removeVowels(name1);
234        name2 = removeVowels(name2);
235
236        // 2. Remove double consonants
237        name1 = removeDoubleConsonants(name1);
238        name2 = removeDoubleConsonants(name2);
239
240        // 3. Reduce down to 3 letters
241        name1 = getFirst3Last3(name1);
242        name2 = getFirst3Last3(name2);
243
244        // 4. Check for length difference - if 3 or greater, then no similarity
245        // comparison is done
246        if (Math.abs(name1.length() - name2.length()) >= 3) {
247            return false;
248        }
249
250        // 5. Obtain the minimum rating value by calculating the length sum of the
251        // encoded Strings and sending it down.
252        final int sumLength = Math.abs(name1.length() + name2.length());
253        final int minRating = getMinRating(sumLength);
254
255        // 6. Process the encoded Strings from left to right and remove any
256        // identical characters found from both Strings respectively.
257        final int count = leftToRightThenRightToLeftProcessing(name1, name2);
258
259        // 7. Each PNI item that has a similarity rating equal to or greater than
260        // the min is considered to be a good candidate match
261        return count >= minRating;
262
263    }
264
265    /**
266     * Determines if a letter is a vowel.
267     *
268     * <h2>API Usage</h2>
269     * <p>
270     * Consider this method private, it is package protected for unit testing only.
271     * </p>
272     *
273     * @param letter
274     *            The letter under investigation
275     * @return True if a vowel, else false
276     */
277    boolean isVowel(final String letter) {
278        return letter.equalsIgnoreCase("E") || letter.equalsIgnoreCase("A") || letter.equalsIgnoreCase("O") ||
279               letter.equalsIgnoreCase("I") || letter.equalsIgnoreCase("U");
280    }
281
282    /**
283     * Processes the names from left to right (first) then right to left removing identical letters in same positions.
284     * Then subtracts the longer string that remains from 6 and returns this.
285     *
286     * <h2>API Usage</h2>
287     * <p>
288     * Consider this method private, it is package protected for unit testing only.
289     * </p>
290     *
291     * @param name1
292     *            name2
293     * @return the length as above
294     */
295    int leftToRightThenRightToLeftProcessing(final String name1, final String name2) {
296        final char[] name1Char = name1.toCharArray();
297        final char[] name2Char = name2.toCharArray();
298
299        final int name1Size = name1.length() - 1;
300        final int name2Size = name2.length() - 1;
301
302        String name1LtRStart = EMPTY;
303        String name1LtREnd = EMPTY;
304
305        String name2RtLStart = EMPTY;
306        String name2RtLEnd = EMPTY;
307
308        for (int i = 0; i < name1Char.length; i++) {
309            if (i > name2Size) {
310                break;
311            }
312
313            name1LtRStart = name1.substring(i, i + 1);
314            name1LtREnd = name1.substring(name1Size - i, name1Size - i + 1);
315
316            name2RtLStart = name2.substring(i, i + 1);
317            name2RtLEnd = name2.substring(name2Size - i, name2Size - i + 1);
318
319            // Left to right...
320            if (name1LtRStart.equals(name2RtLStart)) {
321                name1Char[i] = ' ';
322                name2Char[i] = ' ';
323            }
324
325            // Right to left...
326            if (name1LtREnd.equals(name2RtLEnd)) {
327                name1Char[name1Size - i] = ' ';
328                name2Char[name2Size - i] = ' ';
329            }
330        }
331
332        // Char arrays -> string & remove extraneous space
333        final String strA = new String(name1Char).replaceAll("\\s+", EMPTY);
334        final String strB = new String(name2Char).replaceAll("\\s+", EMPTY);
335
336        // Final bit - subtract the longest string from 6 and return this int value
337        if (strA.length() > strB.length()) {
338            return Math.abs(6 - strA.length());
339        }
340        return Math.abs(6 - strB.length());
341    }
342
343    /**
344     * Removes accented letters and replaces with non-accented ASCII equivalent Case is preserved.
345     * http://www.codecodex.com/wiki/Remove_accent_from_letters_%28ex_.%C3%A9_to_e%29
346     *
347     * @param accentedWord
348     *            The word that may have accents in it.
349     * @return De-accented word
350     */
351    String removeAccents(final String accentedWord) {
352        if (accentedWord == null) {
353            return null;
354        }
355
356        final StringBuilder sb = new StringBuilder();
357        final int n = accentedWord.length();
358
359        for (int i = 0; i < n; i++) {
360            final char c = accentedWord.charAt(i);
361            final int pos = UNICODE.indexOf(c);
362            if (pos > -1) {
363                sb.append(PLAIN_ASCII.charAt(pos));
364            } else {
365                sb.append(c);
366            }
367        }
368
369        return sb.toString();
370    }
371
372    /**
373     * Replaces any double consonant pair with the single letter equivalent.
374     *
375     * <h2>API Usage</h2>
376     * <p>
377     * Consider this method private, it is package protected for unit testing only.
378     * </p>
379     *
380     * @param name
381     *            String to have double consonants removed
382     * @return Single consonant word
383     */
384    String removeDoubleConsonants(final String name) {
385        String replacedName = name.toUpperCase(Locale.ENGLISH);
386        for (final String dc : DOUBLE_CONSONANT) {
387            if (replacedName.contains(dc)) {
388                final String singleLetter = dc.substring(0, 1);
389                replacedName = replacedName.replace(dc, singleLetter);
390            }
391        }
392        return replacedName;
393    }
394
395    /**
396     * Deletes all vowels unless the vowel begins the word.
397     *
398     * <h2>API Usage</h2>
399     * <p>
400     * Consider this method private, it is package protected for unit testing only.
401     * </p>
402     *
403     * @param name
404     *            The name to have vowels removed
405     * @return De-voweled word
406     */
407    String removeVowels(String name) {
408        // Extract first letter
409        final String firstLetter = name.substring(0, 1);
410
411        name = name.replace("A", EMPTY);
412        name = name.replace("E", EMPTY);
413        name = name.replace("I", EMPTY);
414        name = name.replace("O", EMPTY);
415        name = name.replace("U", EMPTY);
416
417        name = name.replaceAll("\\s{2,}\\b", SPACE);
418
419        // return isVowel(firstLetter) ? (firstLetter + name) : name;
420        if (isVowel(firstLetter)) {
421            return firstLetter + name;
422        }
423        return name;
424    }
425}