Coverage Report - org.apache.commons.codec.language.Caverphone1
 
Classes in this File Line Coverage Branch Coverage Complexity
Caverphone1
100%
61/61
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 1.0 value.
 22  
  *
 23  
  * This is an algorithm created by the Caversham Project at the University of Otago. It implements the Caverphone 1.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/ctp060902.pdf">Caverphone 1.0 specification</a>
 29  
  * @since 1.5
 30  
  *
 31  
  * <p>This class is immutable and thread-safe.</p>
 32  
  */
 33  10
 public class Caverphone1 extends AbstractCaverphone {
 34  
 
 35  
     private static final String SIX_1 = "111111";
 36  
 
 37  
     /**
 38  
      * Encodes the given String into a Caverphone 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  35
         String txt = source;
 47  35
         if (txt == null || txt.length() == 0) {
 48  2
             return SIX_1;
 49  
         }
 50  
 
 51  
         // 1. Convert to lowercase
 52  33
         txt = txt.toLowerCase(java.util.Locale.ENGLISH);
 53  
 
 54  
         // 2. Remove anything not A-Z
 55  33
         txt = txt.replaceAll("[^a-z]", "");
 56  
 
 57  
         // 3. Handle various start options
 58  
         // 2 is a temporary placeholder to indicate a consonant which we are no longer interested in.
 59  33
         txt = txt.replaceAll("^cough", "cou2f");
 60  33
         txt = txt.replaceAll("^rough", "rou2f");
 61  33
         txt = txt.replaceAll("^tough", "tou2f");
 62  33
         txt = txt.replaceAll("^enough", "enou2f");
 63  33
         txt = txt.replaceAll("^gn", "2n");
 64  
 
 65  
         // End
 66  33
         txt = txt.replaceAll("mb$", "m2");
 67  
 
 68  
         // 4. Handle replacements
 69  33
         txt = txt.replaceAll("cq", "2q");
 70  33
         txt = txt.replaceAll("ci", "si");
 71  33
         txt = txt.replaceAll("ce", "se");
 72  33
         txt = txt.replaceAll("cy", "sy");
 73  33
         txt = txt.replaceAll("tch", "2ch");
 74  33
         txt = txt.replaceAll("c", "k");
 75  33
         txt = txt.replaceAll("q", "k");
 76  33
         txt = txt.replaceAll("x", "k");
 77  33
         txt = txt.replaceAll("v", "f");
 78  33
         txt = txt.replaceAll("dg", "2g");
 79  33
         txt = txt.replaceAll("tio", "sio");
 80  33
         txt = txt.replaceAll("tia", "sia");
 81  33
         txt = txt.replaceAll("d", "t");
 82  33
         txt = txt.replaceAll("ph", "fh");
 83  33
         txt = txt.replaceAll("b", "p");
 84  33
         txt = txt.replaceAll("sh", "s2");
 85  33
         txt = txt.replaceAll("z", "s");
 86  33
         txt = txt.replaceAll("^[aeiou]", "A");
 87  
         // 3 is a temporary placeholder marking a vowel
 88  33
         txt = txt.replaceAll("[aeiou]", "3");
 89  33
         txt = txt.replaceAll("3gh3", "3kh3");
 90  33
         txt = txt.replaceAll("gh", "22");
 91  33
         txt = txt.replaceAll("g", "k");
 92  33
         txt = txt.replaceAll("s+", "S");
 93  33
         txt = txt.replaceAll("t+", "T");
 94  33
         txt = txt.replaceAll("p+", "P");
 95  33
         txt = txt.replaceAll("k+", "K");
 96  33
         txt = txt.replaceAll("f+", "F");
 97  33
         txt = txt.replaceAll("m+", "M");
 98  33
         txt = txt.replaceAll("n+", "N");
 99  33
         txt = txt.replaceAll("w3", "W3");
 100  33
         txt = txt.replaceAll("wy", "Wy"); // 1.0 only
 101  33
         txt = txt.replaceAll("wh3", "Wh3");
 102  33
         txt = txt.replaceAll("why", "Why"); // 1.0 only
 103  33
         txt = txt.replaceAll("w", "2");
 104  33
         txt = txt.replaceAll("^h", "A");
 105  33
         txt = txt.replaceAll("h", "2");
 106  33
         txt = txt.replaceAll("r3", "R3");
 107  33
         txt = txt.replaceAll("ry", "Ry"); // 1.0 only
 108  33
         txt = txt.replaceAll("r", "2");
 109  33
         txt = txt.replaceAll("l3", "L3");
 110  33
         txt = txt.replaceAll("ly", "Ly"); // 1.0 only
 111  33
         txt = txt.replaceAll("l", "2");
 112  33
         txt = txt.replaceAll("j", "y"); // 1.0 only
 113  33
         txt = txt.replaceAll("y3", "Y3"); // 1.0 only
 114  33
         txt = txt.replaceAll("y", "2"); // 1.0 only
 115  
 
 116  
         // 5. Handle removals
 117  33
         txt = txt.replaceAll("2", "");
 118  33
         txt = txt.replaceAll("3", "");
 119  
 
 120  
         // 6. put ten 1s on the end
 121  33
         txt = txt + SIX_1;
 122  
 
 123  
         // 7. take the first six characters as the code
 124  33
         return txt.substring(0, SIX_1.length());
 125  
     }
 126  
 
 127  
 }