View Javadoc
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  
18  package org.apache.commons.codec.language;
19  
20  import java.util.Locale;
21  
22  /**
23   * Encodes a string into a Caverphone 2.0 value.
24   *
25   * This is an algorithm created by the Caversham Project at the University of Otago. It implements the Caverphone 2.0
26   * algorithm:
27   *
28   * @see <a href="https://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
29   * @see <a href="https://caversham.otago.ac.nz/files/working/ctp150804.pdf">Caverphone 2.0 specification</a>
30   * @since 1.5
31   *
32   * <p>
33   * This class is immutable and thread-safe.
34   * </p>
35   */
36  public class Caverphone2 extends AbstractCaverphone {
37  
38      private static final String TEN_1 = "1111111111";
39  
40      /**
41       * Constructs a new instance.
42       */
43      public Caverphone2() {
44          // empty
45      }
46  
47      /**
48       * Encodes the given String into a Caverphone 2.0 value.
49       *
50       * @param source
51       *            String the source string.
52       * @return A Caverphone code for the given String.
53       */
54      @Override
55      public String encode(final String source) {
56          String txt = source;
57          if (SoundexUtils.isEmpty(txt)) {
58              return TEN_1;
59          }
60  
61          // 1. Convert to lowercase
62          txt = txt.toLowerCase(Locale.ENGLISH);
63  
64          // 2. Remove anything not A-Z
65          txt = txt.replaceAll("[^a-z]", "");
66  
67          // 2.5. Remove final e
68          txt = txt.replaceAll("e$", ""); // 2.0 only
69  
70          // 3. Handle various start options
71          txt = txt.replaceAll("^cough", "cou2f");
72          txt = txt.replaceAll("^rough", "rou2f");
73          txt = txt.replaceAll("^tough", "tou2f");
74          txt = txt.replaceAll("^enough", "enou2f"); // 2.0 only
75          txt = txt.replaceAll("^trough", "trou2f"); // 2.0 only
76                                                     // note the spec says ^enough here again, c+p error I assume
77          txt = txt.replaceAll("^gn", "2n");
78  
79          // End
80          txt = txt.replaceAll("mb$", "m2");
81  
82          // 4. Handle replacements
83          txt = txt.replace("cq", "2q");
84          txt = txt.replace("ci", "si");
85          txt = txt.replace("ce", "se");
86          txt = txt.replace("cy", "sy");
87          txt = txt.replace("tch", "2ch");
88          txt = txt.replace("c", "k");
89          txt = txt.replace("q", "k");
90          txt = txt.replace("x", "k");
91          txt = txt.replace("v", "f");
92          txt = txt.replace("dg", "2g");
93          txt = txt.replace("tio", "sio");
94          txt = txt.replace("tia", "sia");
95          txt = txt.replace("d", "t");
96          txt = txt.replace("ph", "fh");
97          txt = txt.replace("b", "p");
98          txt = txt.replace("sh", "s2");
99          txt = txt.replace("z", "s");
100         txt = txt.replaceAll("^[aeiou]", "A");
101         txt = txt.replaceAll("[aeiou]", "3");
102         txt = txt.replace("j", "y"); // 2.0 only
103         txt = txt.replaceAll("^y3", "Y3"); // 2.0 only
104         txt = txt.replaceAll("^y", "A"); // 2.0 only
105         txt = txt.replace("y", "3"); // 2.0 only
106         txt = txt.replace("3gh3", "3kh3");
107         txt = txt.replace("gh", "22");
108         txt = txt.replace("g", "k");
109         txt = txt.replaceAll("s+", "S");
110         txt = txt.replaceAll("t+", "T");
111         txt = txt.replaceAll("p+", "P");
112         txt = txt.replaceAll("k+", "K");
113         txt = txt.replaceAll("f+", "F");
114         txt = txt.replaceAll("m+", "M");
115         txt = txt.replaceAll("n+", "N");
116         txt = txt.replace("w3", "W3");
117         txt = txt.replace("wh3", "Wh3");
118         txt = txt.replaceAll("w$", "3"); // 2.0 only
119         txt = txt.replace("w", "2");
120         txt = txt.replaceAll("^h", "A");
121         txt = txt.replace("h", "2");
122         txt = txt.replace("r3", "R3");
123         txt = txt.replaceAll("r$", "3"); // 2.0 only
124         txt = txt.replace("r", "2");
125         txt = txt.replace("l3", "L3");
126         txt = txt.replaceAll("l$", "3"); // 2.0 only
127         txt = txt.replace("l", "2");
128 
129         // 5. Handle removals
130         txt = txt.replace("2", "");
131         txt = txt.replaceAll("3$", "A"); // 2.0 only
132         txt = txt.replace("3", "");
133 
134         // 6. put ten 1s on the end
135         txt += TEN_1;
136 
137         // 7. take the first ten characters as the code
138         return txt.substring(0, TEN_1.length());
139     }
140 
141 }