`

获取输入汉字的拼音简码

阅读更多
public class GetPinYin {


private char[] alphatable = {
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',

        'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    };
private static int[] table = new int[27];

private static Map<String,String> rareWords;
    private static Map<String,String> bookFamilyNames;
    private static Map<String,String> bookFamilySurname;
   
static {

table[0]=0;
        table[1]=45253;
        table[2]=45761;
        table[3]=46318;
        table[4]=46826;
        table[5]=47010;
        table[6]=47297;
        table[7]=47614;
        table[8]=47614;
        table[9]=48119;
        table[10]=49062;
        table[11]=49324;
        table[12]=49896;
        table[13]=50371;
        table[14]=50614;
        table[15]=50622;
        table[16]=50906;
        table[17]=51387;
        table[18]=51446;
        table[19]=52218;
        table[20]=52218;
        table[21]=52218;
        table[22]=52698;
        table[23]=52980;
        table[24]=53689;
        table[25]=54481;
        table[26]=55289;


String file_name = new Constant().getClass().getClassLoader()
.getResource("rarewords.properties").getFile();

Properties prop = new Properties();
try {
prop.load(new FileInputStream(file_name));
} catch (Exception ex) {
Logit.errorLog(ex.getMessage(), new Throwable(ex));
}

rareWords = new HashMap<String, String>();
Iterator it=prop.entrySet().iterator();
while(it.hasNext()){
    Map.Entry entry=(Map.Entry)it.next();
    Object key = entry.getKey();
    Object value = entry.getValue();
    rareWords.put(key.toString(), value.toString());
}


String file_name_1 = new Constant().getClass().getClassLoader()
.getResource("rarewords111.properties").getFile();

Properties prop1 = new Properties();
try {
prop1.load(new FileInputStream(file_name_1));
} catch (Exception ex) {
Logit.errorLog(ex.getMessage(), new Throwable(ex));
}

bookFamilyNames = new HashMap<String, String>();
Iterator it1=prop1.entrySet().iterator();
while(it1.hasNext()){
Map.Entry entry=(Map.Entry)it1.next();
Object key = entry.getKey();
Object value = entry.getValue();
bookFamilyNames.put(key.toString(), value.toString());
}

String file_name_2 = new Constant().getClass().getClassLoader()
.getResource("rarewords222.properties").getFile();

Properties prop2 = new Properties();
try {
prop2.load(new FileInputStream(file_name_2));
} catch (Exception ex) {
Logit.errorLog(ex.getMessage(), new Throwable(ex));
}

bookFamilySurname = new HashMap<String, String>();
Iterator it2 = prop2.entrySet().iterator();
while(it2.hasNext()){
Map.Entry entry=(Map.Entry)it2.next();
Object key = entry.getKey();
Object value = entry.getValue();
bookFamilySurname.put(key.toString(), value.toString());
}


}

public String getRareWord(char ch) {
   Set<String> key = rareWords.keySet();
        for (Iterator it = key.iterator(); it.hasNext();) {
            String s = (String) it.next();
           if(rareWords.get(s).contains(String.valueOf(ch))) {
           return s.substring(0, 1).toUpperCase();
           }
        }
        return "0";
}
//根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串
public String getPinYin(String SourceStr) {
     String Result = "";
     int StrLength = SourceStr.length();
     int i;
     try {
         for (i = 0; i < StrLength; i++) {
        
         if (i == 0) {
         //姓
         if (SourceStr.length() >= 2) {
         String tempBjx = getXin(SourceStr.substring(0, 2));
         if (tempBjx != "0") {
         //在百家姓中,复姓
         i++;
         Result += tempBjx;
         continue;
         } else {
         tempBjx = getXin(SourceStr.substring(0, 1));
         if (tempBjx != "0") {
             //在百家姓里
             Result += tempBjx;
             } else {
             char temp = Char2Alpha(SourceStr.charAt(0));
                 if (temp  == '0') {
                 Result += getRareWord(SourceStr.charAt(0));
                       } else {
                       Result +=  temp;
                       }
             }
         }
         } else {
         String tempBjx = getXin(SourceStr.substring(0, 1));
         if (tempBjx != "0") {
             //在百家姓里
             Result += tempBjx;
             } else {
             char temp = Char2Alpha(SourceStr.charAt(0));
                 if (temp  == '0') {
                 Result += getRareWord(SourceStr.charAt(0));
                       } else {
                       Result +=  temp;
                       }
             }
         }
         } else {
         //名
         char temp = Char2Alpha(SourceStr.charAt(i));
         if (temp  == '0') {
         Result += getRareWord(SourceStr.charAt(i));
               } else {
               Result +=  temp;
               }
         }
        
           
         }
     } catch (Exception e) {
         Result = "";
     }
     return Result;
}

@SuppressWarnings("static-access")
public String getXin(String Xin) {

if (Xin == null || Xin.equals("")) {
return "0";
}
if (Xin.length() > 1) {
Iterator it = this.bookFamilySurname.entrySet().iterator();
while(it.hasNext()){
    Map.Entry entry=(Map.Entry)it.next();
    String  key = entry.getKey().toString();
    String value = entry.getValue().toString();
    if (value.contains(Xin)) {
    return key.toUpperCase();
    }
}

} else {
Iterator it = this.bookFamilyNames.entrySet().iterator();
while(it.hasNext()){
    Map.Entry entry=(Map.Entry)it.next();
    String  key = entry.getKey().toString();
    String value = entry.getValue().toString();
    if (value.contains(Xin)) {
    return key.substring(0, 1).toUpperCase();
    }
}
}

return "0";
}

//主函数,输入字符,得到他的声母,
//英文字母返回对应的大写字母
//其他非简体汉字返回 '0'
public char Char2Alpha(char ch) {

     if (ch >= 'a' && ch <= 'z')
         return (char) (ch - 'a' + 'A');
     if (ch >= 'A' && ch <= 'Z')
         return ch;
     int gb = gbValue(ch);
     if (gb < table[0])
         return '0';
     int i;
     for (i = 0; i < 26; ++i) {
         if (match(i, gb))
             break;
     }

     if (i >= 26)
         return '0';
     else
         return alphatable[i];
}


private boolean match(int i, int gb) {
     if (gb < table[i])
         return false;

     int j = i + 1;

     //字母Z使用了两个标签
     while (j < 26 && (table[j] == table[i]))
         ++j;

     if (j == 26)
         return gb <= table[j];
     else
         return gb < table[j];

}

//取出汉字的编码
private int gbValue(char ch) {
     String str = new String();
     str += ch;
     try {
         byte[] bytes = str.getBytes("GB2312");
         if (bytes.length < 2)
             return 0;
         return (bytes[0] << 8 & 0xff00) + (bytes[1] &
                 0xff);
     } catch (Exception e) {
         return 0;
     }

}
分享到:
评论

相关推荐

    自动获取汉字的拼音简码

    在IT行业中,自动获取汉字的拼音简码是一项实用的技术,特别是在处理中文文本的搜索、排序或数据处理时。这项技术的实现通常涉及到自然语言处理(NLP)和字符编码转换。下面将详细介绍这个主题,以及如何使用...

    输入汉字自动产生拼音简码

    在代码中,`word(hzchar[1]) shl 8 + word(hzchar[2])` 这一行代码的作用是获取输入汉字的Unicode编码。这里使用了Delphi/Pascal语言中的位运算符`shl`来将两个字节的值合并成一个整数,从而得到该汉字的Unicode编码...

    Oracle 汉字拼音简码获取

    5. **返回结果**:函数最后返回`v_Spell`变量的值,即为输入汉字字符串的拼音简码。 #### 应用场景 1. **数据检索**:在多语言环境下,通过将汉字转换为拼音简码,可以更高效地进行数据检索,特别是在没有汉字索引...

    asp.net(vb) 汉字拼音简码 自动完成

    VB代码可以调用API或自定义函数来获取输入汉字的拼音简码。 3. **自动完成逻辑**:根据用户输入的拼音简码部分,通过模糊匹配算法(如Levenshtein距离、Jaccard相似度等)在拼音库中查找匹配的汉字。这一步可能需要...

    Delphi获得汉字拼音简码..rar

    在中文环境下,有时我们需要将汉字转化为拼音,比如在搜索引擎优化、文本排序或语音合成等场景,这时掌握如何在Delphi中获取汉字的拼音简码就显得尤为重要。 首先,让我们理解一下汉字拼音简码的概念。拼音简码是...

    汉字转拼音简码

    拼音简码是基于汉字拼音首字母的一种简化表示方式,通常用于快速输入汉字,比如在早期的中文输入法中,用户可以通过输入汉字拼音的首字母来选择并输入汉字。例如,“打”字的拼音是"dǎ",其简码就是"D";“我”字的...

    vb下获取拼音码(含拼音简码数据库)

    - 在函数内部,遍历汉字字符串,对于每个汉字,查询拼音简码数据库获取其对应的拼音简码。 - 如果数据库是以文本文件形式存在,可以使用VB的`OpenTextFile`和`ReadLine`等函数进行读取;如果是数据库文件,如...

    SQL Server 汉字拼音简码获取

    在给定的文件中,我们看到一个名为`sf_ShortCode`的用户自定义函数被定义,其功能是根据输入的汉字获取相应的拼音简码。 #### 知识点二:SQL Server 函数参数与返回类型 此函数接受两个参数: 1. `@Input ...

    delphi查找汉字拼音简码

    总结,通过以上步骤,我们可以利用Delphi开发一个程序,输入汉字并获取其拼音简码。这个过程涉及到编码转换、字符串处理以及可能的数据查找操作。对于没有现成库支持的Delphi开发者,这是一个挑战,但也提供了实践...

    自动获得汉字的拼音简码

    总之,自动获取汉字的拼音简码在Java Web开发中是一个实用的技术点,涉及对汉字编码的理解、第三方库的使用以及字符串处理技巧。通过学习和掌握这些知识,开发者可以更好地处理中文字符,提升应用的功能性和用户体验...

    VB6.0 获取汉字拼音简码(首字母).rar

    标题提到的"VB6.0 获取汉字拼音简码(首字母).rar"是一个压缩包,其中包含VB6源码,用于实现这一功能。在描述中,作者指出此代码可以用于获取汉字的拼音简码,即首字母,并且在实际应用中,如编号或账号的生成,...

    拼音简码 javascript

    拼音简码通常是指通过输入部分汉字的首字母或缩写来快速输入汉字,比如“中国”可以用“zhg”来代替,这样可以大大提高打字速度。在JavaScript中实现这一功能,我们需要创建一个数据结构(如字典或对象)来存储汉字...

    vb拼音简码及查询,拼音首字母

    这包括输入框让用户输入汉字或拼音简码,以及显示结果的文本框或列表视图。VB提供了丰富的控件供开发者使用,如`TextBox`用于输入,`Label`用于显示信息,以及`ListBox`或`ListView`用于展示查询结果。 5. **事件...

    将汉字转换为拼音简码或五笔简码

    五笔输入法的优势在于,熟练用户可以通过记忆这些编码快速输入汉字,尤其对于大量文字输入的工作非常有效。Form1.frm和工程2.vbp可能包含了项目的主界面设计和程序逻辑,用于用户交互和转换过程的控制。 在实现汉字...

    .NET C# 中文拼音,中文简码转换示例

    中文简码通常用于快速输入汉字,尤其是在早期的计算机系统中。每个中文简码通常由一到四个英文字符组成,代表一个汉字。虽然现代操作系统和输入法已经很少使用中文简码,但在某些特定场景下,如编程或数据处理,我们...

    获取拼音简码[vb.net源码]

    拼音简码是汉语拼音的一种简化形式,通常用于快速输入汉字,特别是在早期的计算机系统中。每个汉字对应一个或多个拼音简码,这些简码通常是该汉字拼音的首字母,有时会忽略声调。例如,“中国”对应的拼音简码是...

    利用SQL SERVER实现拼音简码自动转换汉字.pdf

    为了简化汉字数据的输入,提高用户体验,开发者们往往会在数据库设计时增加拼音简码字段。这样用户在操作时只需输入相应的汉字拼音简码,即可实现数据的快速定位和处理。然而,这种做法虽然为用户带来了便利,却也...

    获得汉字的拼音简码程序(VB6.0代码编写)

    4. **界面设计**:在VB6.0中,你可以使用Visual Basic的Form设计器来构建用户界面,包括文本框(TextBox)用于输入汉字,以及标签(Label)或列表框(ListBox)用于显示拼音简码。需要设置适当的事件绑定,如`...

    实现输入拼音或者简码模糊查询数据库某个字段的值

    在中文环境下,由于汉字与拼音的对应关系,我们可以通过转换汉字为拼音来实现模糊匹配。C#中可以借助第三方库,如`Pinyin4net`,来完成这个任务。`Pinyin4net`库提供了一个方便的方法,将汉字转换为拼音,包括全拼和...

Global site tag (gtag.js) - Google Analytics