ColognePhonetic.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.codec.language;

  18. import java.util.Arrays;
  19. import java.util.Locale;

  20. import org.apache.commons.codec.EncoderException;
  21. import org.apache.commons.codec.StringEncoder;

  22. /**
  23.  * Encodes a string into a Cologne Phonetic value.
  24.  * <p>
  25.  * Implements the <a href="https://de.wikipedia.org/wiki/K%C3%B6lner_Phonetik">K&ouml;lner Phonetik</a> (Cologne
  26.  * Phonetic) algorithm issued by Hans Joachim Postel in 1969.
  27.  * </p>
  28.  * <p>
  29.  * The <em>K&ouml;lner Phonetik</em> is a phonetic algorithm which is optimized for the German language. It is related to
  30.  * the well-known soundex algorithm.
  31.  * </p>
  32.  *
  33.  * <h2>Algorithm</h2>
  34.  *
  35.  * <ul>
  36.  *
  37.  * <li>
  38.  * <h3>Step 1:</h3>
  39.  * After preprocessing (conversion to upper case, transcription of <a
  40.  * href="https://en.wikipedia.org/wiki/Germanic_umlaut">germanic umlauts</a>, removal of non alphabetical characters) the
  41.  * letters of the supplied text are replaced by their phonetic code according to the following table.
  42.  * <table border="1">
  43.  * <caption style="caption-side: bottom"><small><i>(Source: <a
  44.  * href="https://de.wikipedia.org/wiki/K%C3%B6lner_Phonetik#Buchstabencodes">Wikipedia (de): K&ouml;lner Phonetik --
  45.  * Buchstabencodes</a>)</i></small></caption> <tbody>
  46.  * <tr>
  47.  * <th>Letter</th>
  48.  * <th>Context</th>
  49.  * <th>Code</th>
  50.  * </tr>
  51.  * <tr>
  52.  * <td>A, E, I, J, O, U, Y</td>
  53.  * <td></td>
  54.  * <td>0</td>
  55.  * </tr>
  56.  * <tr>
  57.  *
  58.  * <td>H</td>
  59.  * <td></td>
  60.  * <td>-</td>
  61.  * </tr>
  62.  * <tr>
  63.  * <td>B</td>
  64.  * <td></td>
  65.  * <td rowspan="2">1</td>
  66.  * </tr>
  67.  * <tr>
  68.  * <td>P</td>
  69.  * <td>not before H</td>
  70.  *
  71.  * </tr>
  72.  * <tr>
  73.  * <td>D, T</td>
  74.  * <td>not before C, S, Z</td>
  75.  * <td>2</td>
  76.  * </tr>
  77.  * <tr>
  78.  * <td>F, V, W</td>
  79.  * <td></td>
  80.  * <td rowspan="2">3</td>
  81.  * </tr>
  82.  * <tr>
  83.  *
  84.  * <td>P</td>
  85.  * <td>before H</td>
  86.  * </tr>
  87.  * <tr>
  88.  * <td>G, K, Q</td>
  89.  * <td></td>
  90.  * <td rowspan="3">4</td>
  91.  * </tr>
  92.  * <tr>
  93.  * <td rowspan="2">C</td>
  94.  * <td>at onset before A, H, K, L, O, Q, R, U, X</td>
  95.  *
  96.  * </tr>
  97.  * <tr>
  98.  * <td>before A, H, K, O, Q, U, X except after S, Z</td>
  99.  * </tr>
  100.  * <tr>
  101.  * <td>X</td>
  102.  * <td>not after C, K, Q</td>
  103.  * <td>48</td>
  104.  * </tr>
  105.  * <tr>
  106.  * <td>L</td>
  107.  * <td></td>
  108.  *
  109.  * <td>5</td>
  110.  * </tr>
  111.  * <tr>
  112.  * <td>M, N</td>
  113.  * <td></td>
  114.  * <td>6</td>
  115.  * </tr>
  116.  * <tr>
  117.  * <td>R</td>
  118.  * <td></td>
  119.  * <td>7</td>
  120.  * </tr>
  121.  *
  122.  * <tr>
  123.  * <td>S, Z</td>
  124.  * <td></td>
  125.  * <td rowspan="6">8</td>
  126.  * </tr>
  127.  * <tr>
  128.  * <td rowspan="3">C</td>
  129.  * <td>after S, Z</td>
  130.  * </tr>
  131.  * <tr>
  132.  * <td>at onset except before A, H, K, L, O, Q, R, U, X</td>
  133.  * </tr>
  134.  *
  135.  * <tr>
  136.  * <td>not before A, H, K, O, Q, U, X</td>
  137.  * </tr>
  138.  * <tr>
  139.  * <td>D, T</td>
  140.  * <td>before C, S, Z</td>
  141.  * </tr>
  142.  * <tr>
  143.  * <td>X</td>
  144.  * <td>after C, K, Q</td>
  145.  * </tr>
  146.  * </tbody>
  147.  * </table>
  148.  *
  149.  * <h4>Example:</h4>
  150.  *
  151.  * {@code "M}&uuml;{@code ller-L}&uuml;<code>denscheidt"
  152.  * =&gt; "MULLERLUDENSCHEIDT" =&gt; "6005507500206880022"</code>
  153.  *
  154.  * </li>
  155.  *
  156.  * <li>
  157.  * <h3>Step 2:</h3>
  158.  * Collapse of all multiple consecutive code digits.
  159.  * <h4>Example:</h4>
  160.  * {@code "6005507500206880022" =&gt; "6050750206802"}</li>
  161.  *
  162.  * <li>
  163.  * <h3>Step 3:</h3>
  164.  * Removal of all codes "0" except at the beginning. This means that two or more identical consecutive digits can occur
  165.  * if they occur after removing the "0" digits.
  166.  *
  167.  * <h4>Example:</h4>
  168.  * {@code "6050750206802" =&gt; "65752682"}</li>
  169.  *
  170.  * </ul>
  171.  *
  172.  * <p>
  173.  * This class is thread-safe.
  174.  * </p>
  175.  *
  176.  * @see <a href="https://de.wikipedia.org/wiki/K%C3%B6lner_Phonetik">Wikipedia (de): K&ouml;lner Phonetik (in German)</a>
  177.  * @since 1.5
  178.  */
  179. public class ColognePhonetic implements StringEncoder {

  180.     /**
  181.      * This class is not thread-safe; the field {@link #length} is mutable.
  182.      * However, it is not shared between threads, as it is constructed on demand
  183.      * by the method {@link ColognePhonetic#colognePhonetic(String)}
  184.      */
  185.     abstract static class CologneBuffer {

  186.         protected final char[] data;

  187.         protected int length;

  188.         CologneBuffer(final char[] data) {
  189.             this.data = data;
  190.             this.length = data.length;
  191.         }

  192.         CologneBuffer(final int buffSize) {
  193.             this.data = new char[buffSize];
  194.             this.length = 0;
  195.         }

  196.         protected abstract char[] copyData(int start, int length);

  197.         public boolean isEmpty() {
  198.             return length() == 0;
  199.         }

  200.         public int length() {
  201.             return length;
  202.         }

  203.         @Override
  204.         public String toString() {
  205.             return new String(copyData(0, length));
  206.         }
  207.     }

  208.     private final class CologneInputBuffer extends CologneBuffer {

  209.         CologneInputBuffer(final char[] data) {
  210.             super(data);
  211.         }

  212.         @Override
  213.         protected char[] copyData(final int start, final int length) {
  214.             final char[] newData = new char[length];
  215.             System.arraycopy(data, data.length - this.length + start, newData, 0, length);
  216.             return newData;
  217.         }

  218.         public char getNextChar() {
  219.             return data[getNextPos()];
  220.         }

  221.         protected int getNextPos() {
  222.             return data.length - length;
  223.         }

  224.         public char removeNext() {
  225.             final char ch = getNextChar();
  226.             length--;
  227.             return ch;
  228.         }
  229.     }

  230.     private final class CologneOutputBuffer extends CologneBuffer {

  231.         private char lastCode;

  232.         CologneOutputBuffer(final int buffSize) {
  233.             super(buffSize);
  234.             lastCode = '/'; // impossible value
  235.         }

  236.         @Override
  237.         protected char[] copyData(final int start, final int length) {
  238.             return Arrays.copyOfRange(data, start, length);
  239.         }

  240.         /**
  241.          * Stores the next code in the output buffer, keeping track of the previous code.
  242.          * '0' is only stored if it is the first entry.
  243.          * Ignored chars are never stored.
  244.          * If the code is the same as the last code (whether stored or not) it is not stored.
  245.          *
  246.          * @param code the code to store.
  247.          */
  248.         public void put(final char code) {
  249.             if (code != CHAR_IGNORE && lastCode != code && (code != '0' || length == 0)) {
  250.                 data[length] = code;
  251.                 length++;
  252.             }
  253.             lastCode = code;
  254.         }
  255.     }
  256.     // Predefined char arrays for better performance and less GC load
  257.     private static final char[] AEIJOUY = { 'A', 'E', 'I', 'J', 'O', 'U', 'Y' };
  258.     private static final char[] CSZ = { 'C', 'S', 'Z' };
  259.     private static final char[] FPVW = { 'F', 'P', 'V', 'W' };
  260.     private static final char[] GKQ = { 'G', 'K', 'Q' };
  261.     private static final char[] CKQ = { 'C', 'K', 'Q' };
  262.     private static final char[] AHKLOQRUX = { 'A', 'H', 'K', 'L', 'O', 'Q', 'R', 'U', 'X' };

  263.     private static final char[] SZ = { 'S', 'Z' };

  264.     private static final char[] AHKOQUX = { 'A', 'H', 'K', 'O', 'Q', 'U', 'X' };

  265.     private static final char[] DTX = { 'D', 'T', 'X' };

  266.     private static final char CHAR_IGNORE = '-';    // is this character to be ignored?

  267.     /*
  268.      * Returns whether the array contains the key, or not.
  269.      */
  270.     private static boolean arrayContains(final char[] arr, final char key) {
  271.         for (final char element : arr) {
  272.             if (element == key) {
  273.                 return true;
  274.             }
  275.         }
  276.         return false;
  277.     }

  278.     /**
  279.      * Constructs a new instance.
  280.      */
  281.     public ColognePhonetic() {
  282.         // empty
  283.     }

  284.     /**
  285.      * <p>
  286.      * Implements the <em>K&ouml;lner Phonetik</em> algorithm.
  287.      * </p>
  288.      * <p>
  289.      * In contrast to the initial description of the algorithm, this implementation does the encoding in one pass.
  290.      * </p>
  291.      *
  292.      * @param text The source text to encode
  293.      * @return the corresponding encoding according to the <em>K&ouml;lner Phonetik</em> algorithm
  294.      */
  295.     public String colognePhonetic(final String text) {
  296.         if (text == null) {
  297.             return null;
  298.         }

  299.         final CologneInputBuffer input = new CologneInputBuffer(preprocess(text));
  300.         final CologneOutputBuffer output = new CologneOutputBuffer(input.length() * 2);

  301.         char nextChar;

  302.         char lastChar = CHAR_IGNORE;
  303.         char chr;

  304.         while (!input.isEmpty()) {
  305.             chr = input.removeNext();

  306.             if (!input.isEmpty()) {
  307.                 nextChar = input.getNextChar();
  308.             } else {
  309.                 nextChar = CHAR_IGNORE;
  310.             }

  311.             if (chr < 'A' || chr > 'Z') {
  312.                     continue; // ignore unwanted characters
  313.             }

  314.             if (arrayContains(AEIJOUY, chr)) {
  315.                 output.put('0');
  316.             } else if (chr == 'B' || chr == 'P' && nextChar != 'H') {
  317.                 output.put('1');
  318.             } else if ((chr == 'D' || chr == 'T') && !arrayContains(CSZ, nextChar)) {
  319.                 output.put('2');
  320.             } else if (arrayContains(FPVW, chr)) {
  321.                 output.put('3');
  322.             } else if (arrayContains(GKQ, chr)) {
  323.                 output.put('4');
  324.             } else if (chr == 'X' && !arrayContains(CKQ, lastChar)) {
  325.                 output.put('4');
  326.                 output.put('8');
  327.             } else if (chr == 'S' || chr == 'Z') {
  328.                 output.put('8');
  329.             } else if (chr == 'C') {
  330.                 if (output.isEmpty()) {
  331.                     if (arrayContains(AHKLOQRUX, nextChar)) {
  332.                         output.put('4');
  333.                     } else {
  334.                         output.put('8');
  335.                     }
  336.                 } else if (arrayContains(SZ, lastChar) || !arrayContains(AHKOQUX, nextChar)) {
  337.                     output.put('8');
  338.                 } else {
  339.                     output.put('4');
  340.                 }
  341.             } else if (arrayContains(DTX, chr)) {
  342.                 output.put('8');
  343.             } else {
  344.                 switch (chr) {
  345.                 case 'R':
  346.                     output.put('7');
  347.                     break;
  348.                 case 'L':
  349.                     output.put('5');
  350.                     break;
  351.                 case 'M':
  352.                 case 'N':
  353.                     output.put('6');
  354.                     break;
  355.                 case 'H':
  356.                     output.put(CHAR_IGNORE); // needed by put
  357.                     break;
  358.                 default:
  359.                     break;
  360.                 }
  361.             }

  362.             lastChar = chr;
  363.         }
  364.         return output.toString();
  365.     }

  366.     @Override
  367.     public Object encode(final Object object) throws EncoderException {
  368.         if (!(object instanceof String)) {
  369.             throw new EncoderException("This method's parameter was expected to be of the type " +
  370.                 String.class.getName() +
  371.                 ". But actually it was of the type " +
  372.                 object.getClass().getName() +
  373.                 ".");
  374.         }
  375.         return encode((String) object);
  376.     }

  377.     @Override
  378.     public String encode(final String text) {
  379.         return colognePhonetic(text);
  380.     }

  381.     /**
  382.      * Compares the first encoded string to the second encoded string.
  383.      *
  384.      * @param text1 source text to encode before testing for equality.
  385.      * @param text2 source text to encode before testing for equality.
  386.      * @return {@code true} if the encoding the first string equals the encoding of the second string, {@code false}
  387.      *         otherwise
  388.      */
  389.     public boolean isEncodeEqual(final String text1, final String text2) {
  390.         return colognePhonetic(text1).equals(colognePhonetic(text2));
  391.     }

  392.     /**
  393.      * Converts the string to upper case and replaces Germanic umlaut characters
  394.      * The following characters are mapped:
  395.      * <ul>
  396.      * <li>capital A, umlaut mark</li>
  397.      * <li>capital U, umlaut mark</li>
  398.      * <li>capital O, umlaut mark</li>
  399.      * <li>small sharp s, German</li>
  400.      * </ul>
  401.      */
  402.     private char[] preprocess(final String text) {
  403.         // This converts German small sharp s (Eszett) to SS
  404.         final char[] chrs = text.toUpperCase(Locale.GERMAN).toCharArray();

  405.         for (int index = 0; index < chrs.length; index++) {
  406.             switch (chrs[index]) {
  407.                 case '\u00C4': // capital A, umlaut mark
  408.                     chrs[index] = 'A';
  409.                     break;
  410.                 case '\u00DC': // capital U, umlaut mark
  411.                     chrs[index] = 'U';
  412.                     break;
  413.                 case '\u00D6': // capital O, umlaut mark
  414.                     chrs[index] = 'O';
  415.                     break;
  416.                 default:
  417.                     break;
  418.             }
  419.         }
  420.         return chrs;
  421.     }
  422. }