博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Android实例】输入汉字,转化为拼音
阅读量:5903 次
发布时间:2019-06-19

本文共 5966 字,大约阅读时间需要 19 分钟。

  hot3.png

先把我demo贴出来,
http://pan.baidu.com/s/1gd2jsAn

添加的pinyin4j包,这边有他的下载地址http://pan.baidu.com/s/1mgnMdsc

public class Change {

private String[] pinyin = null;
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
public String getCharacterPinyin(char c) {
try {
pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format);
} catch (BadHanyuPinyinOutputFormatCombination e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (pinyin == null) {
return null;
}
return pinyin[0];
}
public String getStringPinYin(String str) {
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
StringBuilder sb = new StringBuilder();
String tempPinyin = null;
for (int i = 0; i < str.length(); i++) {
tempPinyin = getCharacterPinyin(str.charAt(i));
if (tempPinyin == null) {
sb.append(str.charAt(i));
} else {
sb.append(tempPinyin);
}
}
return sb.toString();
}
}

下面是基础知识,我转载的

使用pinyin4j - 小米米儿小的个人空间 - 开源中国社区

Pinyin4j就是为了解决类似这样的问题的。它是sourceforge.net上的一个开源项目,功能非常强大:

+ 支持同一汉字有多个发音
+ 还支持拼音的格式化输出,比如第几声之类的,

基本用法
通常情况下,只需要用到其中的PinyinHelper类中的静态方法toHanyuPinyinStringArray就可以了
1
2
3
4
String[] pinyinArray =PinyinHelper.toHanyuPinyinStringArray('单');
for(inti = 0; i < pinyinArray.length; ++i){
         System.out.println(pinyinArray[i]);
}
Pinyin4j支持拼音输出的格式化,比如,“黄”可以输出为“huang”、“huang2”、“huáng”等等
1
2
3
4
5
6
7
8
9
10
11
12
HanyuPinyinOutputFormat format= newHanyuPinyinOutputFormat();
format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);             
String[] pinyinArray = null;
try{
         pinyinArray = PinyinHelper.toHanyuPinyinStringArray('黄', format);
}catch(BadHanyuPinyinOutputFormatCombination e){
         e.printStackTrace();
}
for(inti = 0; i < pinyinArray.length; ++i){
         System.out.println(pinyinArray[i]);
}
一个工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
packagecn.heimar.common.util;
 
importjava.util.HashSet;
importjava.util.Set;
 
importnet.sourceforge.pinyin4j.PinyinHelper;
importnet.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
importnet.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
importnet.sourceforge.pinyin4j.format.HanyuPinyinToneType;
importnet.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
importnet.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
 
publicclassPinyin4jUtil {
 
    /**
     * 返回拼音类型:全拼
     */
    publicstaticfinalString RET_PINYIN_TYPE_FULL = "full";
     
    /**
     * 返回拼音类型:首字母
     */
    publicstaticfinalString RET_PINYIN_TYPE_HEADCHAR = "headChar";
     
    /**
     * 字符串集合转换字符串(逗号分隔)
     *
     * @param stringSet
     * @return
     */
    publicstaticString makeStringByStringSet(Set<String> stringSet) {
        StringBuilder str = newStringBuilder();
        inti = 0;
        for(String s : stringSet) {
            if(i == stringSet.size() - 1) {
                str.append(s);
            } else{
                str.append(s + ",");
            }
            i++;
        }
        returnstr.toString().toLowerCase();
    }
 
    publicstaticSet<String> str2Pinyin(String src) {
        returnstr2Pinyin(src, null);
    }
     
    /**
     * 字符串转换为拼音
     * @param src   需要转换的字符串
     * @param retType   返回拼音结果类型
     * @return  如果retType为RET_PINYIN_TYPE_FULL,则返回全拼;如果retType为RET_PINYIN_TYPE_HEADCHAR;如果传入其他值,返回全拼
     * @throws BadHanyuPinyinOutputFormatCombination
     */
    publicstaticSet<String> str2Pinyin(String src, String retType) {
        if(src != null&& !src.trim().equalsIgnoreCase("")) {
            char[] srcChar;
            srcChar = src.toCharArray();
            // 汉语拼音格式输出类
            HanyuPinyinOutputFormat hanYuPinOutputFormat = newHanyuPinyinOutputFormat();
 
            // 输出设置,大小写,音标方式等
            hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
 
            String[][] temp = newString[src.length()][];
            for(inti = 0; i < srcChar.length; i++) {
                charc = srcChar[i];
                     
                try{
                    temp[i] = PinyinHelper.toHanyuPinyinStringArray(
                            srcChar[i], hanYuPinOutputFormat);
                     
                    if(temp[i] == null){//如果str.charAt(i)非汉字,则保持原样
                        temp[i] = newString[] { String.valueOf(srcChar[i]) };
                    }else{
                        //如果retType是RET_PINYIN_TYPE_HEADCHAR,则只取转换后的首字母
                        if(RET_PINYIN_TYPE_HEADCHAR.equalsIgnoreCase(retType)){
                            String[] temptemp = newString[temp[i].length];
                            for(intj = 0; j < temp[i].length; j++){
                                temptemp[j] =String.valueOf(temp[i][j].charAt(0));
                            }
                            temp[i] = temptemp;
                        }
                    }
                     
                } catch(BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }
            String[] pingyinArray = Exchange(temp);
            Set<String> pinyinSet = newHashSet<String>();
            for(inti = 0; i < pingyinArray.length; i++) {
                pinyinSet.add(pingyinArray[i]);
            }
            returnpinyinSet;
        }
        returnnull;
    }
     
    /**
     * 递归
     *
     * @param strJaggedArray
     * @return
     */
    publicstaticString[] Exchange(String[][] strJaggedArray) {
        String[][] temp = DoExchange(strJaggedArray);
        returntemp[0];
    }
 
    /**
     * 递归
     *
     * @param strJaggedArray
     * @return
     */
    privatestaticString[][] DoExchange(String[][] strJaggedArray) {
        intlen = strJaggedArray.length;
        if(len >= 2) {
            intlen1 = strJaggedArray[0].length;
            intlen2 = strJaggedArray[1].length;
            intnewlen = len1 * len2;
            String[] temp = newString[newlen];
            intIndex = 0;
            for(inti = 0; i < len1; i++) {
                for(intj = 0; j < len2; j++) {
                    temp[Index] = strJaggedArray[0][i] + strJaggedArray[1][j];
                    Index++;
                }
            }
            String[][] newArray = newString[len - 1][];
            for(inti = 2; i < len; i++) {
                newArray[i - 1] = strJaggedArray[i];
            }
            newArray[0] = temp;
            returnDoExchange(newArray);
        } else{
            returnstrJaggedArray;
        }
    }
 
    publicstaticvoidmain(String[] args) {
        String str = "单单芳abc";
        System.out.println(makeStringByStringSet(str2Pinyin(str)));
        System.out.println(makeStringByStringSet(str2Pinyin(str, RET_PINYIN_TYPE_HEADCHAR)));
    }
 
}

转载于:https://my.oschina.net/TAOH/blog/527235

你可能感兴趣的文章
一步一步學習partitions之管理partitions
查看>>
jspwiki学习
查看>>
rem详解及使用方法
查看>>
编程浪子的网络家园【我与51CTO的故事】
查看>>
Inode占满导致No space left on device解决
查看>>
centos和rhel性能差别之谜
查看>>
delphi操作流
查看>>
坚持UGC 酷6走上网络视频健康化模式
查看>>
设计模式——单一职责原则
查看>>
在linux下安装图形界面
查看>>
我的友情链接
查看>>
Linux常用系统资源查看
查看>>
扑克牌2
查看>>
SCOM2012功能测试(26)—新建报表文件夹
查看>>
解决NGINX+PHP-FPM failed to ptrace(PEEKDATA) Input/output error出错问题
查看>>
android和php开发的用户登录以及注册功能
查看>>
KVM真机服务器网卡调整,命令行创建虚拟机,LV快照虚拟机
查看>>
Nginx从入门到掌握【(第2节(共3节)】
查看>>
跟我一起写 Makefile
查看>>
Possible SYN flooding on port 80. Dropping request.
查看>>