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 1.0 value.
022     * 
023     * This is an algorithm created by the Caversham Project at the University of Otago. It implements the Caverphone 1.0
024     * algorithm:
025     * 
026     * @author Apache Software Foundation
027     * @version $Id: Caverphone.java 1075947 2011-03-01 17:56:14Z ggregory $
028     * @see <a href="http://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
029     * @see <a href="http://caversham.otago.ac.nz/files/working/ctp060902.pdf">Caverphone 1.0 specification</a>
030     * @since 1.5
031     */
032    public class Caverphone1 extends AbstractCaverphone {
033    
034        private static final String SIX_1 = "111111";
035    
036        /**
037         * Encodes the given String into a Caverphone value.
038         * 
039         * @param source
040         *            String the source string
041         * @return A caverphone code for the given String
042         */
043        public String encode(String source) {
044            String txt = source;
045            if (txt == null || txt.length() == 0) {
046                return SIX_1;
047            }
048    
049            // 1. Convert to lowercase
050            txt = txt.toLowerCase(java.util.Locale.ENGLISH);
051    
052            // 2. Remove anything not A-Z
053            txt = txt.replaceAll("[^a-z]", "");
054    
055            // 3. Handle various start options
056            // 2 is a temporary placeholder to indicate a consonant which we are no longer interested in.
057            txt = txt.replaceAll("^cough", "cou2f");
058            txt = txt.replaceAll("^rough", "rou2f");
059            txt = txt.replaceAll("^tough", "tou2f");
060            txt = txt.replaceAll("^enough", "enou2f");
061            txt = txt.replaceAll("^gn", "2n");
062    
063            // End
064            txt = txt.replaceAll("mb$", "m2");
065    
066            // 4. Handle replacements
067            txt = txt.replaceAll("cq", "2q");
068            txt = txt.replaceAll("ci", "si");
069            txt = txt.replaceAll("ce", "se");
070            txt = txt.replaceAll("cy", "sy");
071            txt = txt.replaceAll("tch", "2ch");
072            txt = txt.replaceAll("c", "k");
073            txt = txt.replaceAll("q", "k");
074            txt = txt.replaceAll("x", "k");
075            txt = txt.replaceAll("v", "f");
076            txt = txt.replaceAll("dg", "2g");
077            txt = txt.replaceAll("tio", "sio");
078            txt = txt.replaceAll("tia", "sia");
079            txt = txt.replaceAll("d", "t");
080            txt = txt.replaceAll("ph", "fh");
081            txt = txt.replaceAll("b", "p");
082            txt = txt.replaceAll("sh", "s2");
083            txt = txt.replaceAll("z", "s");
084            txt = txt.replaceAll("^[aeiou]", "A");
085            // 3 is a temporary placeholder marking a vowel
086            txt = txt.replaceAll("[aeiou]", "3");
087            txt = txt.replaceAll("3gh3", "3kh3");
088            txt = txt.replaceAll("gh", "22");
089            txt = txt.replaceAll("g", "k");
090            txt = txt.replaceAll("s+", "S");
091            txt = txt.replaceAll("t+", "T");
092            txt = txt.replaceAll("p+", "P");
093            txt = txt.replaceAll("k+", "K");
094            txt = txt.replaceAll("f+", "F");
095            txt = txt.replaceAll("m+", "M");
096            txt = txt.replaceAll("n+", "N");
097            txt = txt.replaceAll("w3", "W3");
098            txt = txt.replaceAll("wy", "Wy"); // 1.0 only
099            txt = txt.replaceAll("wh3", "Wh3");
100            txt = txt.replaceAll("why", "Why"); // 1.0 only
101            txt = txt.replaceAll("w", "2");
102            txt = txt.replaceAll("^h", "A");
103            txt = txt.replaceAll("h", "2");
104            txt = txt.replaceAll("r3", "R3");
105            txt = txt.replaceAll("ry", "Ry"); // 1.0 only
106            txt = txt.replaceAll("r", "2");
107            txt = txt.replaceAll("l3", "L3");
108            txt = txt.replaceAll("ly", "Ly"); // 1.0 only
109            txt = txt.replaceAll("l", "2");
110            txt = txt.replaceAll("j", "y"); // 1.0 only
111            txt = txt.replaceAll("y3", "Y3"); // 1.0 only
112            txt = txt.replaceAll("y", "2"); // 1.0 only
113    
114            // 5. Handle removals
115            txt = txt.replaceAll("2", "");
116            txt = txt.replaceAll("3", "");
117    
118            // 6. put ten 1s on the end
119            txt = txt + SIX_1;
120    
121            // 7. take the first six characters as the code
122            return txt.substring(0, SIX_1.length());
123        }
124    
125    }