1 // Mozilla has modified this file - see https://hg.mozilla.org/ for details.
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements.  See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License.  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package org.mozilla.apache.commons.codec.language;
20 
21 /**
22  * Encodes a string into a Caverphone 2.0 value.
23  *
24  * This is an algorithm created by the Caversham Project at the University of Otago. It implements the Caverphone 2.0
25  * algorithm:
26  *
27  * @author Apache Software Foundation
28  * @version $Id: Caverphone.java 1075947 2011-03-01 17:56:14Z ggregory $
29  * @see <a href="http://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
30  * @see <a href="http://caversham.otago.ac.nz/files/working/ctp150804.pdf">Caverphone 2.0 specification</a>
31  * @since 1.5
32  */
33 public class Caverphone2 extends AbstractCaverphone {
34 
35     private static final String TEN_1 = "1111111111";
36 
37     /**
38      * Encodes the given String into a Caverphone 2.0 value.
39      *
40      * @param source
41      *            String the source string
42      * @return A caverphone code for the given String
43      */
encode(String source)44     public String encode(String source) {
45         String txt = source;
46         if (txt == null || txt.length() == 0) {
47             return TEN_1;
48         }
49 
50         // 1. Convert to lowercase
51         txt = txt.toLowerCase(java.util.Locale.ENGLISH);
52 
53         // 2. Remove anything not A-Z
54         txt = txt.replaceAll("[^a-z]", "");
55 
56         // 2.5. Remove final e
57         txt = txt.replaceAll("e$", ""); // 2.0 only
58 
59         // 3. Handle various start options
60         txt = txt.replaceAll("^cough", "cou2f");
61         txt = txt.replaceAll("^rough", "rou2f");
62         txt = txt.replaceAll("^tough", "tou2f");
63         txt = txt.replaceAll("^enough", "enou2f"); // 2.0 only
64         txt = txt.replaceAll("^trough", "trou2f"); // 2.0 only - note the spec says ^enough here again, c+p error I assume
65         txt = txt.replaceAll("^gn", "2n");
66 
67         // End
68         txt = txt.replaceAll("mb$", "m2");
69 
70         // 4. Handle replacements
71         txt = txt.replaceAll("cq", "2q");
72         txt = txt.replaceAll("ci", "si");
73         txt = txt.replaceAll("ce", "se");
74         txt = txt.replaceAll("cy", "sy");
75         txt = txt.replaceAll("tch", "2ch");
76         txt = txt.replaceAll("c", "k");
77         txt = txt.replaceAll("q", "k");
78         txt = txt.replaceAll("x", "k");
79         txt = txt.replaceAll("v", "f");
80         txt = txt.replaceAll("dg", "2g");
81         txt = txt.replaceAll("tio", "sio");
82         txt = txt.replaceAll("tia", "sia");
83         txt = txt.replaceAll("d", "t");
84         txt = txt.replaceAll("ph", "fh");
85         txt = txt.replaceAll("b", "p");
86         txt = txt.replaceAll("sh", "s2");
87         txt = txt.replaceAll("z", "s");
88         txt = txt.replaceAll("^[aeiou]", "A");
89         txt = txt.replaceAll("[aeiou]", "3");
90         txt = txt.replaceAll("j", "y"); // 2.0 only
91         txt = txt.replaceAll("^y3", "Y3"); // 2.0 only
92         txt = txt.replaceAll("^y", "A"); // 2.0 only
93         txt = txt.replaceAll("y", "3"); // 2.0 only
94         txt = txt.replaceAll("3gh3", "3kh3");
95         txt = txt.replaceAll("gh", "22");
96         txt = txt.replaceAll("g", "k");
97         txt = txt.replaceAll("s+", "S");
98         txt = txt.replaceAll("t+", "T");
99         txt = txt.replaceAll("p+", "P");
100         txt = txt.replaceAll("k+", "K");
101         txt = txt.replaceAll("f+", "F");
102         txt = txt.replaceAll("m+", "M");
103         txt = txt.replaceAll("n+", "N");
104         txt = txt.replaceAll("w3", "W3");
105         txt = txt.replaceAll("wh3", "Wh3");
106         txt = txt.replaceAll("w$", "3"); // 2.0 only
107         txt = txt.replaceAll("w", "2");
108         txt = txt.replaceAll("^h", "A");
109         txt = txt.replaceAll("h", "2");
110         txt = txt.replaceAll("r3", "R3");
111         txt = txt.replaceAll("r$", "3"); // 2.0 only
112         txt = txt.replaceAll("r", "2");
113         txt = txt.replaceAll("l3", "L3");
114         txt = txt.replaceAll("l$", "3"); // 2.0 only
115         txt = txt.replaceAll("l", "2");
116 
117         // 5. Handle removals
118         txt = txt.replaceAll("2", "");
119         txt = txt.replaceAll("3$", "A"); // 2.0 only
120         txt = txt.replaceAll("3", "");
121 
122         // 6. put ten 1s on the end
123         txt = txt + TEN_1;
124 
125         // 7. take the first ten characters as the code
126         return txt.substring(0, TEN_1.length());
127     }
128 
129 }
130