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
018package org.apache.commons.codec.language;
019
020import org.apache.commons.codec.EncoderException;
021import org.apache.commons.codec.StringEncoder;
022
023/**
024 * Encodes a string into a Caverphone value.
025 * 
026 * This is an algorithm created the Caversham Project at the University of Otago. It implements the Caverphone 2.0
027 * algorithm:
028 * 
029 * @author Apache Software Foundation
030 * @version $Id: Caverphone.java 797690 2009-07-24 23:28:35Z ggregory $
031 * @see <a href="http://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
032 * @see <a href="http://caversham.otago.ac.nz/files/working/ctp150804.pdf">Caverphone 2.0 specification</a>
033 * @since 1.4
034 */
035public class Caverphone implements StringEncoder {
036
037    /**
038     * Creates an instance of the Caverphone encoder
039     */
040    public Caverphone() {
041        super();
042    }
043
044    /**
045     * Find the caverphone value of a String. 
046     *
047     * @param txt String to find the caverphone code for
048     * @return A caverphone code corresponding to the String supplied
049     */
050    public String caverphone(String txt) {
051        // NOTE: Version 1.0 of Caverphone is easily derivable from this code 
052        // by commenting out the 2.0 lines and adding in the 1.0 lines
053
054        if( txt == null || txt.length() == 0 ) {
055            return "1111111111";
056        }
057
058        // 1. Convert to lowercase
059        txt = txt.toLowerCase(java.util.Locale.ENGLISH);
060
061        // 2. Remove anything not A-Z
062        txt = txt.replaceAll("[^a-z]", "");
063
064        // 2.5. Remove final e
065        txt = txt.replaceAll("e$", "");             // 2.0 only
066
067        // 3. Handle various start options
068        txt = txt.replaceAll("^cough", "cou2f");
069        txt = txt.replaceAll("^rough", "rou2f");
070        txt = txt.replaceAll("^tough", "tou2f");
071        txt = txt.replaceAll("^enough", "enou2f");  // 2.0 only
072        txt = txt.replaceAll("^trough", "trou2f");  // 2.0 only - note the spec says ^enough here again, c+p error I assume
073        txt = txt.replaceAll("^gn", "2n");
074        txt = txt.replaceAll("^mb", "m2");
075
076        // 4. Handle replacements
077        txt = txt.replaceAll("cq", "2q");
078        txt = txt.replaceAll("ci", "si");
079        txt = txt.replaceAll("ce", "se");
080        txt = txt.replaceAll("cy", "sy");
081        txt = txt.replaceAll("tch", "2ch");
082        txt = txt.replaceAll("c", "k");
083        txt = txt.replaceAll("q", "k");
084        txt = txt.replaceAll("x", "k");
085        txt = txt.replaceAll("v", "f");
086        txt = txt.replaceAll("dg", "2g");
087        txt = txt.replaceAll("tio", "sio");
088        txt = txt.replaceAll("tia", "sia");
089        txt = txt.replaceAll("d", "t");
090        txt = txt.replaceAll("ph", "fh");
091        txt = txt.replaceAll("b", "p");
092        txt = txt.replaceAll("sh", "s2");
093        txt = txt.replaceAll("z", "s");
094        txt = txt.replaceAll("^[aeiou]", "A");
095        txt = txt.replaceAll("[aeiou]", "3");
096        txt = txt.replaceAll("j", "y");        // 2.0 only
097        txt = txt.replaceAll("^y3", "Y3");     // 2.0 only
098        txt = txt.replaceAll("^y", "A");       // 2.0 only
099        txt = txt.replaceAll("y", "3");        // 2.0 only
100        txt = txt.replaceAll("3gh3", "3kh3");
101        txt = txt.replaceAll("gh", "22");
102        txt = txt.replaceAll("g", "k");
103        txt = txt.replaceAll("s+", "S");
104        txt = txt.replaceAll("t+", "T");
105        txt = txt.replaceAll("p+", "P");
106        txt = txt.replaceAll("k+", "K");
107        txt = txt.replaceAll("f+", "F");
108        txt = txt.replaceAll("m+", "M");
109        txt = txt.replaceAll("n+", "N");
110        txt = txt.replaceAll("w3", "W3");
111        //txt = txt.replaceAll("wy", "Wy");    // 1.0 only
112        txt = txt.replaceAll("wh3", "Wh3");
113        txt = txt.replaceAll("w$", "3");       // 2.0 only
114        //txt = txt.replaceAll("why", "Why");  // 1.0 only
115        txt = txt.replaceAll("w", "2");
116        txt = txt.replaceAll("^h", "A");
117        txt = txt.replaceAll("h", "2");
118        txt = txt.replaceAll("r3", "R3");
119        txt = txt.replaceAll("r$", "3");       // 2.0 only
120        //txt = txt.replaceAll("ry", "Ry");    // 1.0 only
121        txt = txt.replaceAll("r", "2");
122        txt = txt.replaceAll("l3", "L3");
123        txt = txt.replaceAll("l$", "3");       // 2.0 only
124        //txt = txt.replaceAll("ly", "Ly");    // 1.0 only
125        txt = txt.replaceAll("l", "2");
126        //txt = txt.replaceAll("j", "y");      // 1.0 only
127        //txt = txt.replaceAll("y3", "Y3");    // 1.0 only
128        //txt = txt.replaceAll("y", "2");      // 1.0 only
129
130        // 5. Handle removals
131        txt = txt.replaceAll("2", "");
132        txt = txt.replaceAll("3$", "A");       // 2.0 only
133        txt = txt.replaceAll("3", "");
134
135        // 6. put ten 1s on the end
136        txt = txt + "111111" + "1111";        // 1.0 only has 6 1s
137
138        // 7. take the first six characters as the code
139        return txt.substring(0, 10);          // 1.0 truncates to 6
140    }
141
142    /**
143     * Encodes an Object using the caverphone algorithm.  This method
144     * is provided in order to satisfy the requirements of the
145     * Encoder interface, and will throw an EncoderException if the
146     * supplied object is not of type java.lang.String.
147     *
148     * @param pObject Object to encode
149     * @return An object (or type java.lang.String) containing the 
150     *         caverphone code which corresponds to the String supplied.
151     * @throws EncoderException if the parameter supplied is not
152     *                          of type java.lang.String
153     */
154    public Object encode(Object pObject) throws EncoderException {
155        if (!(pObject instanceof String)) {
156            throw new EncoderException("Parameter supplied to Caverphone encode is not of type java.lang.String"); 
157        }
158        return caverphone((String) pObject);
159    }
160
161    /**
162     * Encodes a String using the Caverphone algorithm. 
163     *
164     * @param pString String object to encode
165     * @return The caverphone code corresponding to the String supplied
166     */
167    public String encode(String pString) {
168        return caverphone(pString);   
169    }
170
171    /**
172     * Tests if the caverphones of two strings are identical.
173     *
174     * @param str1 First of two strings to compare
175     * @param str2 Second of two strings to compare
176     * @return <code>true</code> if the caverphones of these strings are identical, 
177     *        <code>false</code> otherwise.
178     */
179    public boolean isCaverphoneEqual(String str1, String str2) {
180        return caverphone(str1).equals(caverphone(str2));
181    }
182
183}