Caverphone1.java

  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. package org.apache.commons.codec.language;

  18. import java.util.Locale;

  19. /**
  20.  * Encodes a string into a Caverphone 1.0 value.
  21.  *
  22.  * This is an algorithm created by the Caversham Project at the University of Otago. It implements the Caverphone 1.0
  23.  * algorithm:
  24.  *
  25.  * @see <a href="https://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
  26.  * @see <a href="https://caversham.otago.ac.nz/files/working/ctp060902.pdf">Caverphone 1.0 specification</a>
  27.  * @since 1.5
  28.  *
  29.  * <p>This class is immutable and thread-safe.</p>
  30.  */
  31. public class Caverphone1 extends AbstractCaverphone {

  32.     private static final String SIX_1 = "111111";

  33.     /**
  34.      * Constructs a new instance.
  35.      */
  36.     public Caverphone1() {
  37.         // empty
  38.     }

  39.     /**
  40.      * Encodes the given String into a Caverphone value.
  41.      *
  42.      * @param source
  43.      *            String the source string.
  44.      * @return A Caverphone code for the given String.
  45.      */
  46.     @Override
  47.     public String encode(final String source) {
  48.         String txt = source;
  49.         if (txt == null || txt.isEmpty()) {
  50.             return SIX_1;
  51.         }

  52.         // 1. Convert to lowercase
  53.         txt = txt.toLowerCase(Locale.ENGLISH);

  54.         // 2. Remove anything not A-Z
  55.         txt = txt.replaceAll("[^a-z]", "");

  56.         // 3. Handle various start options
  57.         // 2 is a temporary placeholder to indicate a consonant which we are no longer interested in.
  58.         txt = txt.replaceAll("^cough", "cou2f");
  59.         txt = txt.replaceAll("^rough", "rou2f");
  60.         txt = txt.replaceAll("^tough", "tou2f");
  61.         txt = txt.replaceAll("^enough", "enou2f");
  62.         txt = txt.replaceAll("^gn", "2n");

  63.         // End
  64.         txt = txt.replaceAll("mb$", "m2");

  65.         // 4. Handle replacements
  66.         txt = txt.replace("cq", "2q");
  67.         txt = txt.replace("ci", "si");
  68.         txt = txt.replace("ce", "se");
  69.         txt = txt.replace("cy", "sy");
  70.         txt = txt.replace("tch", "2ch");
  71.         txt = txt.replace("c", "k");
  72.         txt = txt.replace("q", "k");
  73.         txt = txt.replace("x", "k");
  74.         txt = txt.replace("v", "f");
  75.         txt = txt.replace("dg", "2g");
  76.         txt = txt.replace("tio", "sio");
  77.         txt = txt.replace("tia", "sia");
  78.         txt = txt.replace("d", "t");
  79.         txt = txt.replace("ph", "fh");
  80.         txt = txt.replace("b", "p");
  81.         txt = txt.replace("sh", "s2");
  82.         txt = txt.replace("z", "s");
  83.         txt = txt.replaceAll("^[aeiou]", "A");
  84.         // 3 is a temporary placeholder marking a vowel
  85.         txt = txt.replaceAll("[aeiou]", "3");
  86.         txt = txt.replace("3gh3", "3kh3");
  87.         txt = txt.replace("gh", "22");
  88.         txt = txt.replace("g", "k");
  89.         txt = txt.replaceAll("s+", "S");
  90.         txt = txt.replaceAll("t+", "T");
  91.         txt = txt.replaceAll("p+", "P");
  92.         txt = txt.replaceAll("k+", "K");
  93.         txt = txt.replaceAll("f+", "F");
  94.         txt = txt.replaceAll("m+", "M");
  95.         txt = txt.replaceAll("n+", "N");
  96.         txt = txt.replace("w3", "W3");
  97.         txt = txt.replace("wy", "Wy"); // 1.0 only
  98.         txt = txt.replace("wh3", "Wh3");
  99.         txt = txt.replace("why", "Why"); // 1.0 only
  100.         txt = txt.replace("w", "2");
  101.         txt = txt.replaceAll("^h", "A");
  102.         txt = txt.replace("h", "2");
  103.         txt = txt.replace("r3", "R3");
  104.         txt = txt.replace("ry", "Ry"); // 1.0 only
  105.         txt = txt.replace("r", "2");
  106.         txt = txt.replace("l3", "L3");
  107.         txt = txt.replace("ly", "Ly"); // 1.0 only
  108.         txt = txt.replace("l", "2");
  109.         txt = txt.replace("j", "y"); // 1.0 only
  110.         txt = txt.replace("y3", "Y3"); // 1.0 only
  111.         txt = txt.replace("y", "2"); // 1.0 only

  112.         // 5. Handle removals
  113.         txt = txt.replace("2", "");
  114.         txt = txt.replace("3", "");

  115.         // 6. put six 1s on the end
  116.         txt += SIX_1;

  117.         // 7. take the first six characters as the code
  118.         return txt.substring(0, SIX_1.length());
  119.     }

  120. }