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