added phonetic classes

This commit is contained in:
Michael Peter Christen 2011-12-14 17:33:18 +01:00
parent 8e301cd917
commit 4243ace863
6 changed files with 2378 additions and 0 deletions

View File

@ -0,0 +1,429 @@
/*
* 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 net.yacy.cora.language.phonetic;
import java.util.Locale;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringEncoder;
/**
* <p>
* Encodes a string into a Cologne Phonetic value.
* </p>
* <p>
* Implements the <a href="http://de.wikipedia.org/wiki/K%C3%B6lner_Phonetik">K&ouml;lner Phonetik</a> (Cologne Phonetic)
* algorithm issued by Hans Joachim Postel in 1969.
* </p>
*
* <p>
* The <i>K&ouml;lner Phonetik</i> is a phonetic algorithm which is optimized for the German language. It is related to the
* well-known soundex algorithm.
* </p>
*
* <h2>Algorithm</h2>
*
* <ul>
*
* <li>
* <h3>Step 1:</h3>
* After preprocessing (conversion to upper case, transcription of <a
* href="http://en.wikipedia.org/wiki/Germanic_umlaut">germanic umlauts</a>, removal of non alphabetical characters) the
* letters of the supplied text are replaced by their phonetic code according to the following table.
* <table border="1">
* <tbody>
* <tr>
* <th>Letter</th>
* <th>Context</th>
* <th align="center">Code</th>
* </tr>
* <tr>
* <td>A, E, I, J, O, U, Y</td>
* <td></td>
* <td align="center">0</td>
* </tr>
* <tr>
*
* <td>H</td>
* <td></td>
* <td align="center">-</td>
* </tr>
* <tr>
* <td>B</td>
* <td></td>
* <td rowspan="2" align="center">1</td>
* </tr>
* <tr>
* <td>P</td>
* <td>not before H</td>
*
* </tr>
* <tr>
* <td>D, T</td>
* <td>not before C, S, Z</td>
* <td align="center">2</td>
* </tr>
* <tr>
* <td>F, V, W</td>
* <td></td>
* <td rowspan="2" align="center">3</td>
* </tr>
* <tr>
*
* <td>P</td>
* <td>before H</td>
* </tr>
* <tr>
* <td>G, K, Q</td>
* <td></td>
* <td rowspan="3" align="center">4</td>
* </tr>
* <tr>
* <td rowspan="2">C</td>
* <td>at onset before A, H, K, L, O, Q, R, U, X</td>
*
* </tr>
* <tr>
* <td>before A, H, K, O, Q, U, X except after S, Z</td>
* </tr>
* <tr>
* <td>X</td>
* <td>not after C, K, Q</td>
* <td align="center">48</td>
* </tr>
* <tr>
* <td>L</td>
* <td></td>
*
* <td align="center">5</td>
* </tr>
* <tr>
* <td>M, N</td>
* <td></td>
* <td align="center">6</td>
* </tr>
* <tr>
* <td>R</td>
* <td></td>
* <td align="center">7</td>
* </tr>
*
* <tr>
* <td>S, Z</td>
* <td></td>
* <td rowspan="6" align="center">8</td>
* </tr>
* <tr>
* <td rowspan="3">C</td>
* <td>after S, Z</td>
* </tr>
* <tr>
* <td>at onset except before A, H, K, L, O, Q, R, U, X</td>
* </tr>
*
* <tr>
* <td>not before A, H, K, O, Q, U, X</td>
* </tr>
* <tr>
* <td>D, T</td>
* <td>before C, S, Z</td>
* </tr>
* <tr>
* <td>X</td>
* <td>after C, K, Q</td>
* </tr>
* </tbody>
* </table>
* <p>
* <small><i>(Source: <a href= "http://de.wikipedia.org/wiki/K%C3%B6lner_Phonetik#Buchstabencodes" >Wikipedia (de):
* K&ouml;lner Phonetik -- Buchstabencodes</a>)</i></small>
* </p>
*
* <h4>Example:</h4>
*
* {@code "M}&uuml;{@code ller-L}&uuml;{@code denscheidt" => "MULLERLUDENSCHEIDT" => "6005507500206880022"}
*
* </li>
*
* <li>
* <h3>Step 2:</h3>
* Collapse of all multiple consecutive code digits.
* <h4>Example:</h4>
* {@code "6005507500206880022" => "6050750206802"}</li>
*
* <li>
* <h3>Step 3:</h3>
* Removal of all codes "0" except at the beginning. This means that two or more identical consecutive digits can occur
* if they occur after removing the "0" digits.
*
* <h4>Example:</h4>
* {@code "6050750206802" => "65752682"}</li>
*
* </ul>
*
* @see <a href="http://de.wikipedia.org/wiki/K%C3%B6lner_Phonetik">Wikipedia (de): K&ouml;lner Phonetik (in German)</a>
* @author Apache Software Foundation
* @since 1.5
*/
public class ColognePhonetic implements StringEncoder {
private abstract class CologneBuffer {
protected final char[] data;
protected int length = 0;
public CologneBuffer(char[] data) {
this.data = data;
this.length = data.length;
}
public CologneBuffer(int buffSize) {
this.data = new char[buffSize];
this.length = 0;
}
protected abstract char[] copyData(int start, final int length);
public int length() {
return length;
}
@Override
public String toString() {
return new String(copyData(0, length));
}
}
private class CologneOutputBuffer extends CologneBuffer {
public CologneOutputBuffer(int buffSize) {
super(buffSize);
}
public void addRight(char chr) {
data[length] = chr;
length++;
}
@Override
protected char[] copyData(int start, final int length) {
char[] newData = new char[length];
System.arraycopy(data, start, newData, 0, length);
return newData;
}
}
private class CologneInputBuffer extends CologneBuffer {
public CologneInputBuffer(char[] data) {
super(data);
}
public void addLeft(char ch) {
length++;
data[getNextPos()] = ch;
}
@Override
protected char[] copyData(int start, final int length) {
char[] newData = new char[length];
System.arraycopy(data, data.length - this.length + start, newData, 0, length);
return newData;
}
public char getNextChar() {
return data[getNextPos()];
}
protected int getNextPos() {
return data.length - length;
}
public char removeNext() {
char ch = getNextChar();
length--;
return ch;
}
}
/**
* Maps some Germanic characters to plain for internal processing. The following characters are mapped:
* <ul>
* <li>capital a, umlaut mark</li>
* <li>capital u, umlaut mark</li>
* <li>capital o, umlaut mark</li>
* <li>small sharp s, German</li>
* </ul>
*/
private static final char[][] PREPROCESS_MAP = new char[][]{
{'\u00C4', 'A'}, // capital a, umlaut mark
{'\u00DC', 'U'}, // capital u, umlaut mark
{'\u00D6', 'O'}, // capital o, umlaut mark
{'\u00DF', 'S'} // small sharp s, German
};
/*
* Returns whether the array contains the key, or not.
*/
private static boolean arrayContains(char[] arr, char key) {
for (char element : arr) {
if (element == key) {
return true;
}
}
return false;
}
/**
* <p>
* Implements the <i>K&ouml;lner Phonetik</i> algorithm.
* </p>
* <p>
* In contrast to the initial description of the algorithm, this implementation does the encoding in one pass.
* </p>
*
* @param text
* @return the corresponding encoding according to the <i>K&ouml;lner Phonetik</i> algorithm
*/
public String colognePhonetic(String text) {
if (text == null) {
return null;
}
text = preprocess(text);
CologneOutputBuffer output = new CologneOutputBuffer(text.length() * 2);
CologneInputBuffer input = new CologneInputBuffer(text.toCharArray());
char nextChar;
char lastChar = '-';
char lastCode = '/';
char code;
char chr;
int rightLength = input.length();
while (rightLength > 0) {
chr = input.removeNext();
if ((rightLength = input.length()) > 0) {
nextChar = input.getNextChar();
} else {
nextChar = '-';
}
if (arrayContains(new char[]{'A', 'E', 'I', 'J', 'O', 'U', 'Y'}, chr)) {
code = '0';
} else if (chr == 'H' || chr < 'A' || chr > 'Z') {
if (lastCode == '/') {
continue;
}
code = '-';
} else if (chr == 'B' || (chr == 'P' && nextChar != 'H')) {
code = '1';
} else if ((chr == 'D' || chr == 'T') && !arrayContains(new char[]{'S', 'C', 'Z'}, nextChar)) {
code = '2';
} else if (arrayContains(new char[]{'W', 'F', 'P', 'V'}, chr)) {
code = '3';
} else if (arrayContains(new char[]{'G', 'K', 'Q'}, chr)) {
code = '4';
} else if (chr == 'X' && !arrayContains(new char[]{'C', 'K', 'Q'}, lastChar)) {
code = '4';
input.addLeft('S');
rightLength++;
} else if (chr == 'S' || chr == 'Z') {
code = '8';
} else if (chr == 'C') {
if (lastCode == '/') {
if (arrayContains(new char[]{'A', 'H', 'K', 'L', 'O', 'Q', 'R', 'U', 'X'}, nextChar)) {
code = '4';
} else {
code = '8';
}
} else {
if (arrayContains(new char[]{'S', 'Z'}, lastChar) ||
!arrayContains(new char[]{'A', 'H', 'O', 'U', 'K', 'Q', 'X'}, nextChar)) {
code = '8';
} else {
code = '4';
}
}
} else if (arrayContains(new char[]{'T', 'D', 'X'}, chr)) {
code = '8';
} else if (chr == 'R') {
code = '7';
} else if (chr == 'L') {
code = '5';
} else if (chr == 'M' || chr == 'N') {
code = '6';
} else {
code = chr;
}
if (code != '-' && (lastCode != code && (code != '0' || lastCode == '/') || code < '0' || code > '8')) {
output.addRight(code);
}
lastChar = chr;
lastCode = code;
}
return output.toString();
}
public Object encode(Object object) throws EncoderException {
if (!(object instanceof String)) {
throw new EncoderException("This method's parameter was expected to be of the type " +
String.class.getName() +
". But actually it was of the type " +
object.getClass().getName() +
".");
}
return encode((String) object);
}
public String encode(String text) {
return colognePhonetic(text);
}
public boolean isEncodeEqual(String text1, String text2) {
return colognePhonetic(text1).equals(colognePhonetic(text2));
}
/**
* Converts the string to upper case and replaces germanic characters as defined in {@link #PREPROCESS_MAP}.
*/
private String preprocess(String text) {
text = text.toUpperCase(Locale.GERMAN);
char[] chrs = text.toCharArray();
for (int index = 0; index < chrs.length; index++) {
if (chrs[index] > 'Z') {
for (char[] element : PREPROCESS_MAP) {
if (chrs[index] == element[0]) {
chrs[index] = element[1];
break;
}
}
}
}
return new String(chrs);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,407 @@
/*
* 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 net.yacy.cora.language.phonetic;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringEncoder;
/**
* Encodes a string into a Metaphone value.
* <p>
* Initial Java implementation by <CITE>William B. Brogden. December, 1997</CITE>.
* Permission given by <CITE>wbrogden</CITE> for code to be used anywhere.
* </p>
* <p>
* <CITE>Hanging on the Metaphone</CITE> by <CITE>Lawrence Philips</CITE> in <CITE>Computer Language of Dec. 1990, p
* 39.</CITE>
* </p>
* <p>
* Note, that this does not match the algorithm that ships with PHP, or the algorithm
* found in the Perl <a href="http://search.cpan.org/~mschwern/Text-Metaphone-1.96/Metaphone.pm">Text:Metaphone-1.96</a>.
* They have had undocumented changes from the originally published algorithm.
* For more information, see <a href="https://issues.apache.org/jira/browse/CODEC-57">CODEC-57</a>.
* </p>
*
* @author Apache Software Foundation
* @version $Id: Metaphone.java 1157192 2011-08-12 17:27:38Z ggregory $
*/
public class Metaphone implements StringEncoder {
/**
* Five values in the English language
*/
private static final String VOWELS = "AEIOU" ;
/**
* Variable used in Metaphone algorithm
*/
private static final String FRONTV = "EIY" ;
/**
* Variable used in Metaphone algorithm
*/
private static final String VARSON = "CSPTG" ;
/**
* The max code length for metaphone is 4
*/
private int maxCodeLen = 4 ;
/**
* Creates an instance of the Metaphone encoder
*/
public Metaphone() {
super();
}
/**
* Find the metaphone value of a String. This is similar to the
* soundex algorithm, but better at finding similar sounding words.
* All input is converted to upper case.
* Limitations: Input format is expected to be a single ASCII word
* with only characters in the A - Z range, no punctuation or numbers.
*
* @param txt String to find the metaphone code for
* @return A metaphone code corresponding to the String supplied
*/
public String metaphone(String txt) {
boolean hard = false ;
if ((txt == null) || (txt.length() == 0)) {
return "" ;
}
// single character is itself
if (txt.length() == 1) {
return txt.toUpperCase(java.util.Locale.ENGLISH) ;
}
char[] inwd = txt.toUpperCase(java.util.Locale.ENGLISH).toCharArray() ;
StringBuffer local = new StringBuffer(40); // manipulate
StringBuffer code = new StringBuffer(10) ; // output
// handle initial 2 characters exceptions
switch(inwd[0]) {
case 'K' :
case 'G' :
case 'P' : /* looking for KN, etc*/
if (inwd[1] == 'N') {
local.append(inwd, 1, inwd.length - 1);
} else {
local.append(inwd);
}
break;
case 'A': /* looking for AE */
if (inwd[1] == 'E') {
local.append(inwd, 1, inwd.length - 1);
} else {
local.append(inwd);
}
break;
case 'W' : /* looking for WR or WH */
if (inwd[1] == 'R') { // WR -> R
local.append(inwd, 1, inwd.length - 1);
break ;
}
if (inwd[1] == 'H') {
local.append(inwd, 1, inwd.length - 1);
local.setCharAt(0, 'W'); // WH -> W
} else {
local.append(inwd);
}
break;
case 'X' : /* initial X becomes S */
inwd[0] = 'S';
local.append(inwd);
break ;
default :
local.append(inwd);
} // now local has working string with initials fixed
int wdsz = local.length();
int n = 0 ;
while ((code.length() < this.getMaxCodeLen()) &&
(n < wdsz) ) { // max code size of 4 works well
char symb = local.charAt(n) ;
// remove duplicate letters except C
if ((symb != 'C') && (isPreviousChar( local, n, symb )) ) {
n++ ;
} else { // not dup
switch(symb) {
case 'A' : case 'E' : case 'I' : case 'O' : case 'U' :
if (n == 0) {
code.append(symb);
}
break ; // only use vowel if leading char
case 'B' :
if ( isPreviousChar(local, n, 'M') &&
isLastChar(wdsz, n) ) { // B is silent if word ends in MB
break;
}
code.append(symb);
break;
case 'C' : // lots of C special cases
/* discard if SCI, SCE or SCY */
if ( isPreviousChar(local, n, 'S') &&
!isLastChar(wdsz, n) &&
(FRONTV.indexOf(local.charAt(n + 1)) >= 0) ) {
break;
}
if (regionMatch(local, n, "CIA")) { // "CIA" -> X
code.append('X');
break;
}
if (!isLastChar(wdsz, n) &&
(FRONTV.indexOf(local.charAt(n + 1)) >= 0)) {
code.append('S');
break; // CI,CE,CY -> S
}
if (isPreviousChar(local, n, 'S') &&
isNextChar(local, n, 'H') ) { // SCH->sk
code.append('K') ;
break ;
}
if (isNextChar(local, n, 'H')) { // detect CH
if ((n == 0) &&
(wdsz >= 3) &&
isVowel(local,2) ) { // CH consonant -> K consonant
code.append('K');
} else {
code.append('X'); // CHvowel -> X
}
} else {
code.append('K');
}
break ;
case 'D' :
if (!isLastChar(wdsz, n + 1) &&
isNextChar(local, n, 'G') &&
(FRONTV.indexOf(local.charAt(n + 2)) >= 0)) { // DGE DGI DGY -> J
code.append('J'); n += 2 ;
} else {
code.append('T');
}
break ;
case 'G' : // GH silent at end or before consonant
if (isLastChar(wdsz, n + 1) &&
isNextChar(local, n, 'H')) {
break;
}
if (!isLastChar(wdsz, n + 1) &&
isNextChar(local,n,'H') &&
!isVowel(local,n+2)) {
break;
}
if ((n > 0) &&
( regionMatch(local, n, "GN") ||
regionMatch(local, n, "GNED") ) ) {
break; // silent G
}
if (isPreviousChar(local, n, 'G')) {
// NOTE: Given that duplicated chars are removed, I don't see how this can ever be true
hard = true ;
} else {
hard = false ;
}
if (!isLastChar(wdsz, n) &&
(FRONTV.indexOf(local.charAt(n + 1)) >= 0) &&
(!hard)) {
code.append('J');
} else {
code.append('K');
}
break ;
case 'H':
if (isLastChar(wdsz, n)) {
break ; // terminal H
}
if ((n > 0) &&
(VARSON.indexOf(local.charAt(n - 1)) >= 0)) {
break;
}
if (isVowel(local,n+1)) {
code.append('H'); // Hvowel
}
break;
case 'F':
case 'J' :
case 'L' :
case 'M':
case 'N' :
case 'R' :
code.append(symb);
break;
case 'K' :
if (n > 0) { // not initial
if (!isPreviousChar(local, n, 'C')) {
code.append(symb);
}
} else {
code.append(symb); // initial K
}
break ;
case 'P' :
if (isNextChar(local,n,'H')) {
// PH -> F
code.append('F');
} else {
code.append(symb);
}
break ;
case 'Q' :
code.append('K');
break;
case 'S' :
if (regionMatch(local,n,"SH") ||
regionMatch(local,n,"SIO") ||
regionMatch(local,n,"SIA")) {
code.append('X');
} else {
code.append('S');
}
break;
case 'T' :
if (regionMatch(local,n,"TIA") ||
regionMatch(local,n,"TIO")) {
code.append('X');
break;
}
if (regionMatch(local,n,"TCH")) {
// Silent if in "TCH"
break;
}
// substitute numeral 0 for TH (resembles theta after all)
if (regionMatch(local,n,"TH")) {
code.append('0');
} else {
code.append('T');
}
break ;
case 'V' :
code.append('F'); break ;
case 'W' : case 'Y' : // silent if not followed by vowel
if (!isLastChar(wdsz,n) &&
isVowel(local,n+1)) {
code.append(symb);
}
break ;
case 'X' :
code.append('K'); code.append('S');
break ;
case 'Z' :
code.append('S'); break ;
} // end switch
n++ ;
} // end else from symb != 'C'
if (code.length() > this.getMaxCodeLen()) {
code.setLength(this.getMaxCodeLen());
}
}
return code.toString();
}
private boolean isVowel(StringBuffer string, int index) {
return VOWELS.indexOf(string.charAt(index)) >= 0;
}
private boolean isPreviousChar(StringBuffer string, int index, char c) {
boolean matches = false;
if( index > 0 &&
index < string.length() ) {
matches = string.charAt(index - 1) == c;
}
return matches;
}
private boolean isNextChar(StringBuffer string, int index, char c) {
boolean matches = false;
if( index >= 0 &&
index < string.length() - 1 ) {
matches = string.charAt(index + 1) == c;
}
return matches;
}
private boolean regionMatch(StringBuffer string, int index, String test) {
boolean matches = false;
if( index >= 0 &&
(index + test.length() - 1) < string.length() ) {
String substring = string.substring( index, index + test.length());
matches = substring.equals( test );
}
return matches;
}
private boolean isLastChar(int wdsz, int n) {
return n + 1 == wdsz;
}
/**
* Encodes an Object using the metaphone 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
* metaphone 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 String)) {
throw new EncoderException("Parameter supplied to Metaphone encode is not of type java.lang.String");
}
return metaphone((String) pObject);
}
/**
* Encodes a String using the Metaphone algorithm.
*
* @param pString String object to encode
* @return The metaphone code corresponding to the String supplied
*/
public String encode(String pString) {
return metaphone(pString);
}
/**
* Tests is the metaphones 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 metaphones of these strings are identical,
* <code>false</code> otherwise.
*/
public boolean isMetaphoneEqual(String str1, String str2) {
return metaphone(str1).equals(metaphone(str2));
}
/**
* Returns the maxCodeLen.
* @return int
*/
public int getMaxCodeLen() { return this.maxCodeLen; }
/**
* Sets the maxCodeLen.
* @param maxCodeLen The maxCodeLen to set
*/
public void setMaxCodeLen(int maxCodeLen) { this.maxCodeLen = maxCodeLen; }
}

View File

@ -0,0 +1,73 @@
/**
* Phonetic
* Copyright 201 by Michael Peter Christen, mc@yacy.net, Frankfurt a. M., Germany
* First released 13.12.2011 at http://yacy.net
*
* $LastChangedDate$
* $LastChangedRevision$
* $LastChangedBy$
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program in the file lgpl21.txt
* If not, see <http://www.gnu.org/licenses/>.
*/
package net.yacy.cora.language.phonetic;
public class Phonetic {
public enum Encoder {
SOUNDEX("Soundex", ""),
COLONE_PHONETIC("Koelner Phonetik", "http://de.wikipedia.org/wiki/K%C3%B6lner_Phonetik"),
METAPHONE("Metaphone", ""),
DOUBLE_METAPHONE("Double Metaphone", ""),
NONE("", "");
final String printName;
final String infoUrl;
private Encoder(final String printName, final String infoUrl) {
this.printName = printName;
this.infoUrl = infoUrl;
}
}
private static final Soundex soundexEncoder = new Soundex();
private static final Metaphone metaphoneEncoder = new Metaphone();
private static final DoubleMetaphone doubleMetaphoneEncoder = new DoubleMetaphone();
private static final ColognePhonetic colognePhonetic = new ColognePhonetic();
public static String encode(final Encoder encoder, final String s) {
try {
if (encoder == Encoder.SOUNDEX) return soundexEncoder.encode(s);
if (encoder == Encoder.COLONE_PHONETIC) return colognePhonetic.encode(s);
if (encoder == Encoder.METAPHONE) return metaphoneEncoder.encode(s);
if (encoder == Encoder.DOUBLE_METAPHONE) return doubleMetaphoneEncoder.encode(s);
return s;
} catch (Throwable e) {
// some encoders do not work with non-ASCII charachters and throw an exception
return s;
}
}
public static void main(String[] args) {
for (Encoder encoder: Encoder.values()) {
for (String s: args) {
System.out.print(Phonetic.encode(encoder, s));
System.out.print(" ");
}
System.out.println();
}
}
}

View File

@ -0,0 +1,340 @@
/*
* 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 net.yacy.cora.language.phonetic;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringEncoder;
/**
* Encodes a string into a Soundex value. Soundex is an encoding used to relate similar names, but can also be used as a
* general purpose scheme to find word with similar phonemes.
*
* @author Apache Software Foundation
* @version $Id: Soundex.java 1201529 2011-11-13 21:57:16Z ggregory $
*/
public class Soundex implements StringEncoder {
/**
* This is a default mapping of the 26 letters used in US English. A value of <code>0</code> for a letter position
* means do not encode.
* <p>
* (This constant is provided as both an implementation convenience and to allow Javadoc to pick
* up the value for the constant values page.)
* </p>
*
* @see #US_ENGLISH_MAPPING
*/
public static final String US_ENGLISH_MAPPING_STRING = "01230120022455012623010202";
/**
* This is a default mapping of the 26 letters used in US English. A value of <code>0</code> for a letter position
* means do not encode.
*
* @see Soundex#Soundex(char[])
*/
private static final char[] US_ENGLISH_MAPPING = US_ENGLISH_MAPPING_STRING.toCharArray();
/**
* An instance of Soundex using the US_ENGLISH_MAPPING mapping.
*
* @see #US_ENGLISH_MAPPING
*/
public static final Soundex US_ENGLISH = new Soundex();
/**
* Every letter of the alphabet is "mapped" to a numerical value. This char array holds the values to which each
* letter is mapped. This implementation contains a default map for US_ENGLISH
*/
private final char[] soundexMapping;
/**
* Creates an instance using US_ENGLISH_MAPPING
*
* @see Soundex#Soundex(char[])
* @see Soundex#US_ENGLISH_MAPPING
*/
public Soundex() {
this.soundexMapping = US_ENGLISH_MAPPING;
}
/**
* Creates a soundex instance using the given mapping. This constructor can be used to provide an internationalized
* mapping for a non-Western character set.
*
* Every letter of the alphabet is "mapped" to a numerical value. This char array holds the values to which each
* letter is mapped. This implementation contains a default map for US_ENGLISH
*
* @param mapping
* Mapping array to use when finding the corresponding code for a given character
*/
public Soundex(char[] mapping) {
this.soundexMapping = new char[mapping.length];
System.arraycopy(mapping, 0, this.soundexMapping, 0, mapping.length);
}
/**
* Creates a refined soundex instance using a custom mapping. This constructor can be used to customize the mapping,
* and/or possibly provide an internationalized mapping for a non-Western character set.
*
* @param mapping
* Mapping string to use when finding the corresponding code for a given character
* @since 1.4
*/
public Soundex(String mapping) {
this.soundexMapping = mapping.toCharArray();
}
/**
* Encodes the Strings and returns the number of characters in the two encoded Strings that are the same. This
* return value ranges from 0 through 4: 0 indicates little or no similarity, and 4 indicates strong similarity or
* identical values.
*
* @param s1
* A String that will be encoded and compared.
* @param s2
* A String that will be encoded and compared.
* @return The number of characters in the two encoded Strings that are the same from 0 to 4.
*
* @see SoundexUtils#difference(StringEncoder,String,String)
* @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp"> MS
* T-SQL DIFFERENCE </a>
*
* @throws EncoderException
* if an error occurs encoding one of the strings
* @since 1.3
*/
public int difference(String s1, String s2) throws EncoderException {
return difference(this, s1, s2);
}
/**
* Encodes an Object using the soundex 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 soundex code which corresponds to the String
* supplied.
* @throws EncoderException
* if the parameter supplied is not of type java.lang.String
* @throws IllegalArgumentException
* if a character is not mapped
*/
public Object encode(Object pObject) throws EncoderException {
if (!(pObject instanceof String)) {
throw new EncoderException("Parameter supplied to Soundex encode is not of type java.lang.String");
}
return soundex((String) pObject);
}
/**
* Encodes a String using the soundex algorithm.
*
* @param pString
* A String object to encode
* @return A Soundex code corresponding to the String supplied
* @throws IllegalArgumentException
* if a character is not mapped
*/
public String encode(String pString) {
return soundex(pString);
}
/**
* Used internally by the SoundEx algorithm.
*
* Consonants from the same code group separated by W or H are treated as one.
*
* @param str
* the cleaned working string to encode (in upper case).
* @param index
* the character position to encode
* @return Mapping code for a particular character
* @throws IllegalArgumentException
* if the character is not mapped
*/
private char getMappingCode(String str, int index) {
// map() throws IllegalArgumentException
char mappedChar = this.map(str.charAt(index));
// HW rule check
if (index > 1 && mappedChar != '0') {
char hwChar = str.charAt(index - 1);
if ('H' == hwChar || 'W' == hwChar) {
char preHWChar = str.charAt(index - 2);
char firstCode = this.map(preHWChar);
if (firstCode == mappedChar || 'H' == preHWChar || 'W' == preHWChar) {
return 0;
}
}
}
return mappedChar;
}
/**
* Returns the soundex mapping.
*
* @return soundexMapping.
*/
private char[] getSoundexMapping() {
return this.soundexMapping;
}
/**
* Maps the given upper-case character to its Soundex code.
*
* @param ch
* An upper-case character.
* @return A Soundex code.
* @throws IllegalArgumentException
* Thrown if <code>ch</code> is not mapped.
*/
private char map(char ch) {
int index = ch - 'A';
if (index < 0 || index >= this.getSoundexMapping().length) {
throw new IllegalArgumentException("The character is not mapped: " + ch);
}
return this.getSoundexMapping()[index];
}
/**
* Retrieves the Soundex code for a given String object.
*
* @param str
* String to encode using the Soundex algorithm
* @return A soundex code for the String supplied
* @throws IllegalArgumentException
* if a character is not mapped
*/
public String soundex(String str) {
if (str == null) {
return null;
}
str = clean(str);
if (str.length() == 0) {
return str;
}
char out[] = {'0', '0', '0', '0'};
char last, mapped;
int incount = 1, count = 1;
out[0] = str.charAt(0);
// getMappingCode() throws IllegalArgumentException
last = getMappingCode(str, 0);
while ((incount < str.length()) && (count < out.length)) {
mapped = getMappingCode(str, incount++);
if (mapped != 0) {
if ((mapped != '0') && (mapped != last)) {
out[count++] = mapped;
}
last = mapped;
}
}
return new String(out);
}
/**
* Cleans up the input string before Soundex processing by only returning
* upper case letters.
*
* @param str
* The String to clean.
* @return A clean String.
*/
static String clean(String str) {
if (str == null || str.length() == 0) {
return str;
}
int len = str.length();
char[] chars = new char[len];
int count = 0;
for (int i = 0; i < len; i++) {
if (Character.isLetter(str.charAt(i))) {
chars[count++] = str.charAt(i);
}
}
if (count == len) {
return str.toUpperCase(java.util.Locale.ENGLISH);
}
return new String(chars, 0, count).toUpperCase(java.util.Locale.ENGLISH);
}
/**
* Encodes the Strings and returns the number of characters in the two
* encoded Strings that are the same.
* <ul>
* <li>For Soundex, this return value ranges from 0 through 4: 0 indicates
* little or no similarity, and 4 indicates strong similarity or identical
* values.</li>
* <li>For refined Soundex, the return value can be greater than 4.</li>
* </ul>
*
* @param encoder
* The encoder to use to encode the Strings.
* @param s1
* A String that will be encoded and compared.
* @param s2
* A String that will be encoded and compared.
* @return The number of characters in the two Soundex encoded Strings that
* are the same.
*
* @see #differenceEncoded(String,String)
* @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp">
* MS T-SQL DIFFERENCE</a>
*
* @throws EncoderException
* if an error occurs encoding one of the strings
*/
static int difference(StringEncoder encoder, String s1, String s2) throws EncoderException {
return differenceEncoded(encoder.encode(s1), encoder.encode(s2));
}
/**
* Returns the number of characters in the two Soundex encoded Strings that
* are the same.
* <ul>
* <li>For Soundex, this return value ranges from 0 through 4: 0 indicates
* little or no similarity, and 4 indicates strong similarity or identical
* values.</li>
* <li>For refined Soundex, the return value can be greater than 4.</li>
* </ul>
*
* @param es1
* An encoded String.
* @param es2
* An encoded String.
* @return The number of characters in the two Soundex encoded Strings that
* are the same.
*
* @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp">
* MS T-SQL DIFFERENCE</a>
*/
static int differenceEncoded(String es1, String es2) {
if (es1 == null || es2 == null) {
return 0;
}
int lengthToMatch = Math.min(es1.length(), es2.length());
int diff = 0;
for (int i = 0; i < lengthToMatch; i++) {
if (es1.charAt(i) == es2.charAt(i)) {
diff++;
}
}
return diff;
}
}

View File

@ -1,3 +1,27 @@
/**
* ClassProvider
* Copyright 201 by Michael Peter Christen, mc@yacy.net, Frankfurt a. M., Germany
* First released 13.12.2011 at http://yacy.net
*
* $LastChangedDate$
* $LastChangedRevision$
* $LastChangedBy$
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program in the file lgpl21.txt
* If not, see <http://www.gnu.org/licenses/>.
*/
package net.yacy.cora.plugin;
import java.io.File;