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    
018    package org.apache.commons.codec.language;
019    
020    /**
021     * Encodes a string into a Caverphone 2.0 value.
022     *
023     * This is an algorithm created by the Caversham Project at the University of Otago. It implements the Caverphone 2.0
024     * algorithm:
025     *
026     * @version $Id: Caverphone2.html 889935 2013-12-11 05:05:13Z ggregory $
027     * @see <a href="http://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
028     * @see <a href="http://caversham.otago.ac.nz/files/working/ctp150804.pdf">Caverphone 2.0 specification</a>
029     * @since 1.5
030     *
031     * <p>This class is immutable and thread-safe.</p>
032     */
033    public class Caverphone2 extends AbstractCaverphone {
034    
035        private static final String TEN_1 = "1111111111";
036    
037        /**
038         * Encodes the given String into a Caverphone 2.0 value.
039         *
040         * @param source
041         *            String the source string
042         * @return A caverphone code for the given String
043         */
044        @Override
045        public String encode(String source) {
046            String txt = source;
047            if (txt == null || txt.length() == 0) {
048                return TEN_1;
049            }
050    
051            // 1. Convert to lowercase
052            txt = txt.toLowerCase(java.util.Locale.ENGLISH);
053    
054            // 2. Remove anything not A-Z
055            txt = txt.replaceAll("[^a-z]", "");
056    
057            // 2.5. Remove final e
058            txt = txt.replaceAll("e$", ""); // 2.0 only
059    
060            // 3. Handle various start options
061            txt = txt.replaceAll("^cough", "cou2f");
062            txt = txt.replaceAll("^rough", "rou2f");
063            txt = txt.replaceAll("^tough", "tou2f");
064            txt = txt.replaceAll("^enough", "enou2f"); // 2.0 only
065            txt = txt.replaceAll("^trough", "trou2f"); // 2.0 only
066                                                       // note the spec says ^enough here again, c+p error I assume
067            txt = txt.replaceAll("^gn", "2n");
068    
069            // End
070            txt = txt.replaceAll("mb$", "m2");
071    
072            // 4. Handle replacements
073            txt = txt.replaceAll("cq", "2q");
074            txt = txt.replaceAll("ci", "si");
075            txt = txt.replaceAll("ce", "se");
076            txt = txt.replaceAll("cy", "sy");
077            txt = txt.replaceAll("tch", "2ch");
078            txt = txt.replaceAll("c", "k");
079            txt = txt.replaceAll("q", "k");
080            txt = txt.replaceAll("x", "k");
081            txt = txt.replaceAll("v", "f");
082            txt = txt.replaceAll("dg", "2g");
083            txt = txt.replaceAll("tio", "sio");
084            txt = txt.replaceAll("tia", "sia");
085            txt = txt.replaceAll("d", "t");
086            txt = txt.replaceAll("ph", "fh");
087            txt = txt.replaceAll("b", "p");
088            txt = txt.replaceAll("sh", "s2");
089            txt = txt.replaceAll("z", "s");
090            txt = txt.replaceAll("^[aeiou]", "A");
091            txt = txt.replaceAll("[aeiou]", "3");
092            txt = txt.replaceAll("j", "y"); // 2.0 only
093            txt = txt.replaceAll("^y3", "Y3"); // 2.0 only
094            txt = txt.replaceAll("^y", "A"); // 2.0 only
095            txt = txt.replaceAll("y", "3"); // 2.0 only
096            txt = txt.replaceAll("3gh3", "3kh3");
097            txt = txt.replaceAll("gh", "22");
098            txt = txt.replaceAll("g", "k");
099            txt = txt.replaceAll("s+", "S");
100            txt = txt.replaceAll("t+", "T");
101            txt = txt.replaceAll("p+", "P");
102            txt = txt.replaceAll("k+", "K");
103            txt = txt.replaceAll("f+", "F");
104            txt = txt.replaceAll("m+", "M");
105            txt = txt.replaceAll("n+", "N");
106            txt = txt.replaceAll("w3", "W3");
107            txt = txt.replaceAll("wh3", "Wh3");
108            txt = txt.replaceAll("w$", "3"); // 2.0 only
109            txt = txt.replaceAll("w", "2");
110            txt = txt.replaceAll("^h", "A");
111            txt = txt.replaceAll("h", "2");
112            txt = txt.replaceAll("r3", "R3");
113            txt = txt.replaceAll("r$", "3"); // 2.0 only
114            txt = txt.replaceAll("r", "2");
115            txt = txt.replaceAll("l3", "L3");
116            txt = txt.replaceAll("l$", "3"); // 2.0 only
117            txt = txt.replaceAll("l", "2");
118    
119            // 5. Handle removals
120            txt = txt.replaceAll("2", "");
121            txt = txt.replaceAll("3$", "A"); // 2.0 only
122            txt = txt.replaceAll("3", "");
123    
124            // 6. put ten 1s on the end
125            txt = txt + TEN_1;
126    
127            // 7. take the first ten characters as the code
128            return txt.substring(0, TEN_1.length());
129        }
130    
131    }