Source code
Description: source: package: org.apache.commons.codec.language, class: Caverphone2

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