java - 在 Java 中实现 Soundex

标签 java search soundex

请帮我用java实现字符串相似度比较! 使用 org.apache.commons.codec.language.Soundex 库

Soundex soundex = new Soundex();
String phoneticValue = soundex.encode("YourString");
String phoneticValue2 = soundex.encode("YourStrink");

if(phoneticValue.equals(phoneticValue2)){

}

好像不行。如果字符串相似,编码函数会给出不同的结果。 如何将两个相似的字符串与该库进行比较?

期待您的来信! ;)

最佳答案

class Soundex{
private static int getConsonantCode( char ch ){
    String codeList[] = { "BFPV", "CGJKQSXZ","DT","L","MN","R" };
    int code = 0;
    for( int i = 0 ; i < codeList.length ; i++ ){
         if( codeList[i].indexOf(ch) >= 0 ) {
            code = i+1;
        }
    }
    return code;
}
private static boolean isVowel( char ch ){
    return (new String("AEIOUaeiou")).indexOf(ch) >= 0 ;
}
public static String getSoundexCode( String str ){
    str=str.toUpperCase();
    String soundexCode = "" + str.charAt(0), temp="";
    int length = str.length();
    char curr, prev, next;{ }
    String dropList = "AEIOUYHW";
    for( int i=1 ; i< length ; i++ ){
        curr = str.charAt(i);
        prev = str.charAt( i-1 );
        if( ( curr=='H' || curr == 'W') && i != length-1 ){
            if( temp.length() >= 2) temp=temp.substring(1);
            next=str.charAt( i+1 );
            if( isVowel(curr) && getConsonantCode( prev ) == getConsonantCode( next ) ){
                temp += prev+prev;
                i=i+1;
            }else if( getConsonantCode( prev ) == getConsonantCode(next) ){
                temp += prev;
                i=i+1;
            }
        }else if( getConsonantCode( curr ) != getConsonantCode(prev) ){
            if( dropList.indexOf( curr ) == -1 ){
                temp += curr;
            }
        }
    }
    temp = ( temp + "0000" ).substring( 0, 3 );
    for( int i = 0; i<=2 ; i++ ){
        soundexCode += getConsonantCode( temp.charAt(i) );
    }
    return soundexCode;
}
}

关于java - 在 Java 中实现 Soundex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43275987/

相关文章:

java - 获取网络适配器的最大带宽

java - 使用流合并两个集合,但仅合并唯一值,并使用谓词而不是等于?

vim - 是否有比 :cn to go to the next item in the quickfix list? 更短的组合键

sql - 优化多列 LIKE SQL 查询的一些最佳实践是什么?

sql - Mysql 关于更新和默认

java - 为什么 Hybris 中的促销引擎无法正常工作?

java - 可以访问 ArrayList 的排序父类(super class)

javascript - 搜索框结果点击并呈现 html 页面

sql-server - "sounds-like", "did you mean THAT"在 SQL Server 2005 中使用全文搜索的功能

search - 定制分析仪Elasticsearch Soundex Plus Snowball