Author: bayard
Date: Sat Mar  8 01:00:24 2008
New Revision: 634911

URL: http://svn.apache.org/viewvc?rev=634911&view=rev
Log:
Applying my second patch from CODEC-60; adding the Caverphone 2.0 algorithm

Added:
    
commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/Caverphone.java
   (with props)
    
commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/CaverphoneTest.java
   (with props)

Added: 
commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/Caverphone.java
URL: 
http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/Caverphone.java?rev=634911&view=auto
==============================================================================
--- 
commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/Caverphone.java
 (added)
+++ 
commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/Caverphone.java
 Sat Mar  8 01:00:24 2008
@@ -0,0 +1,183 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+
+package org.apache.commons.codec.language;
+
+import org.apache.commons.codec.EncoderException;
+import org.apache.commons.codec.StringEncoder;
+
+/**
+ * Encodes a string into a caverphone value. 
+ *
+ * This is an algorithm created the Caversham Project at the University of 
Otago. 
+ * It implements the Caverphone 2.0 algorithm:
+ *
+ *
+ * @author Apache Software Foundation
+ * @version $Id: Caverphone.java 582447 2007-10-06 04:14:43Z bayard $
+ * @see <a href="http://en.wikipedia.org/wiki/Caverphone";>Wikipedia - 
Caverphone</a>
+ * @see <a 
href="http://caversham.otago.ac.nz/files/working/ctp150804.pdf";>Caverphone 2.0 
specification</a>
+ */
+public class Caverphone implements StringEncoder {
+
+    /**
+     * Creates an instance of the Caverphone encoder
+     */
+    public Caverphone() {
+        super();
+    }
+
+    /**
+     * Find the caverphone value of a String. 
+     *
+     * @param txt String to find the caverphone code for
+     * @return A caverphone code corresponding to the String supplied
+     */
+    public String caverphone(String txt) {
+        // NOTE: Version 1.0 of Caverphone is easily derivable from this code 
+        // by commenting out the 2.0 lines and adding in the 1.0 lines
+
+        if( txt == null || txt.length() == 0 ) {
+            return "1111111111";
+        }
+
+        // 1. Convert to lowercase
+        txt = txt.toLowerCase();
+
+        // 2. Remove anything not A-Z
+        txt = txt.replaceAll("[^a-z]", "");
+
+        // 2.5. Remove final e
+        txt = txt.replace("e$", "");             // 2.0 only
+
+        // 3. Handle various start options
+        txt = txt.replace("^cough", "cou2f");
+        txt = txt.replace("^rough", "rou2f");
+        txt = txt.replace("^tough", "tou2f");
+        txt = txt.replace("^enough", "enou2f");  // 2.0 only
+        txt = txt.replace("^trough", "trou2f");  // 2.0 only - note the spec 
says ^enough here again, c+p error I assume
+        txt = txt.replace("^gn", "2n");
+        txt = txt.replace("^mb", "m2");
+
+        // 4. Handle replacements
+        txt = txt.replaceAll("cq", "2q");
+        txt = txt.replaceAll("ci", "si");
+        txt = txt.replaceAll("ce", "se");
+        txt = txt.replaceAll("cy", "sy");
+        txt = txt.replaceAll("tch", "2ch");
+        txt = txt.replaceAll("c", "k");
+        txt = txt.replaceAll("q", "k");
+        txt = txt.replaceAll("x", "k");
+        txt = txt.replaceAll("v", "f");
+        txt = txt.replaceAll("dg", "2g");
+        txt = txt.replaceAll("tio", "sio");
+        txt = txt.replaceAll("tia", "sia");
+        txt = txt.replaceAll("d", "t");
+        txt = txt.replaceAll("ph", "fh");
+        txt = txt.replaceAll("b", "p");
+        txt = txt.replaceAll("sh", "s2");
+        txt = txt.replaceAll("z", "s");
+        txt = txt.replace("^[aeiou]", "A");
+        txt = txt.replaceAll("[aeiou]", "3");
+        txt = txt.replaceAll("j", "y");        // 2.0 only
+        txt = txt.replaceAll("^y3", "Y3");     // 2.0 only
+        txt = txt.replaceAll("^y", "A");       // 2.0 only
+        txt = txt.replaceAll("y", "3");        // 2.0 only
+        txt = txt.replaceAll("3gh3", "3kh3");
+        txt = txt.replaceAll("gh", "22");
+        txt = txt.replaceAll("g", "k");
+        txt = txt.replaceAll("s+", "S");
+        txt = txt.replaceAll("t+", "T");
+        txt = txt.replaceAll("p+", "P");
+        txt = txt.replaceAll("k+", "K");
+        txt = txt.replaceAll("f+", "F");
+        txt = txt.replaceAll("m+", "M");
+        txt = txt.replaceAll("n+", "N");
+        txt = txt.replaceAll("w3", "W3");
+        //txt = txt.replaceAll("wy", "Wy");    // 1.0 only
+        txt = txt.replaceAll("wh3", "Wh3");
+        txt = txt.replaceAll("w$", "3");       // 2.0 only
+        //txt = txt.replaceAll("why", "Why");  // 1.0 only
+        txt = txt.replaceAll("w", "2");
+        txt = txt.replace("^h", "A");
+        txt = txt.replaceAll("h", "2");
+        txt = txt.replaceAll("r3", "R3");
+        txt = txt.replaceAll("r$", "3");       // 2.0 only
+        //txt = txt.replaceAll("ry", "Ry");    // 1.0 only
+        txt = txt.replaceAll("r", "2");
+        txt = txt.replaceAll("l3", "L3");
+        txt = txt.replaceAll("l$", "3");       // 2.0 only
+        //txt = txt.replaceAll("ly", "Ly");    // 1.0 only
+        txt = txt.replaceAll("l", "2");
+        //txt = txt.replaceAll("j", "y");      // 1.0 only
+        //txt = txt.replaceAll("y3", "Y3");    // 1.0 only
+        //txt = txt.replaceAll("y", "2");      // 1.0 only
+
+        // 5. Handle removals
+        txt = txt.replaceAll("2", "");
+        txt = txt.replaceAll("3$", "A");       // 2.0 only
+        txt = txt.replaceAll("3", "");
+
+        // 6. put ten 1s on the end
+        txt = txt + "111111" + "1111";        // 1.0 only has 6 1s
+
+        // 7. take the first six characters as the code
+        return txt.substring(0, 10);          // 1.0 truncates to 6
+    }
+
+    /**
+     * Encodes an Object using the caverphone algorithm.  This method
+     * is provided in order to satisfy the requirements of the
+     * Encoder interface, and will throw an EncoderException if the
+     * supplied object is not of type java.lang.String.
+     *
+     * @param pObject Object to encode
+     * @return An object (or type java.lang.String) containing the 
+     *         caverphone code which corresponds to the String supplied.
+     * @throws EncoderException if the parameter supplied is not
+     *                          of type java.lang.String
+     */
+    public Object encode(Object pObject) throws EncoderException {
+        if (!(pObject instanceof java.lang.String)) {
+            throw new EncoderException("Parameter supplied to Caverphone 
encode is not of type java.lang.String"); 
+        }
+        return caverphone((String) pObject);
+    }
+
+    /**
+     * Encodes a String using the Caverphone algorithm. 
+     *
+     * @param pString String object to encode
+     * @return The caverphone code corresponding to the String supplied
+     */
+    public String encode(String pString) {
+        return caverphone(pString);   
+    }
+
+    /**
+     * Tests if the caverphones of two strings are identical.
+     *
+     * @param str1 First of two strings to compare
+     * @param str2 Second of two strings to compare
+     * @return <code>true</code> if the caverphones of these strings are 
identical, 
+     *        <code>false</code> otherwise.
+     */
+    public boolean isCaverphoneEqual(String str1, String str2) {
+        return caverphone(str1).equals(caverphone(str2));
+    }
+
+}

Propchange: 
commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/Caverphone.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/CaverphoneTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/CaverphoneTest.java?rev=634911&view=auto
==============================================================================
--- 
commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/CaverphoneTest.java
 (added)
+++ 
commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/CaverphoneTest.java
 Sat Mar  8 01:00:24 2008
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+
+package org.apache.commons.codec.language;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.commons.codec.StringEncoder;
+import org.apache.commons.codec.StringEncoderAbstractTest;
+
+/**
+ * @author Apache Software Foundation
+ * @version $Id: CaverphoneTest.java 588074 2007-10-24 23:04:56Z ggregory $
+ */
+public class CaverphoneTest extends StringEncoderAbstractTest {
+
+    public static Test suite() {
+        return new TestSuite(CaverphoneTest.class);
+    }
+
+    public CaverphoneTest(String name) {
+        super(name);
+    }
+
+    protected StringEncoder makeEncoder() {
+        return new Caverphone();
+    }
+
+    public void testWikipediaExamples() {
+        Caverphone caverphone = new Caverphone();
+        String[][] data = {
+            {"Stevenson", "STFNSN1111"},
+            {"Peter",     "PTA1111111"},
+            {"ready",     "RTA1111111"},
+            {"social",    "SSA1111111"},
+            {"able",      "APA1111111"},
+            {"Tedder",    "TTA1111111"},
+            {"Karleen",   "KLN1111111"},
+            {"Dyun",      "TN11111111"},
+        };
+
+        for(int i=0; i<data.length; i++) {
+            assertEquals( data[i][1], caverphone.caverphone(data[i][0]) );
+        }
+    }
+
+}

Propchange: 
commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/CaverphoneTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to