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