[pinyin4j] java版汉字转换拼音(大小写)

pinyin4J 是一个可以将汉字转换成拼音的lib,非常实用,其maven地址为:http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2.5.0


pinyin4J 提供PinyinHelper这个静态类对外提供拼音转换的服务,主要有一下方法:

static public String[] toHanyuPinyinStringArray(char ch)

将char(必须为汉字单字)转化为拼音,实用的是通用的格式,如果ch为非汉字,返回null。

输入:重 输出:[zhong4, chong2] 说明重字有两个读音,拼音后面的1,2,3,4 代表的是读音


static public String[] toHanyuPinyinStringArray(char ch,HanyuPinyinOutputFormat outputFormat)

同上,但是这个方法可以设置输出的格式。HanyuPinyinOutputFormat   可以设置拼音大小写、是否后面加读音数字、特殊读音的显示方式,定义如下:

    /**
     * The option indicates that the output of 'ü' is "u:"
     */
    public static final HanyuPinyinVCharType WITH_U_AND_COLON = new HanyuPinyinVCharType("WITH_U_AND_COLON");
    /**
     * The option indicates that the output of 'ü' is "v"
     */
    public static final HanyuPinyinVCharType WITH_V = new HanyuPinyinVCharType("WITH_V")
    /**
     * The option indicates that the output of 'ü' is "ü" in Unicode form
     */
    public static final HanyuPinyinVCharType WITH_U_UNICODE = new HanyuPinyinVCharType("WITH_U_UNICODE");


static public String[] toTongyongPinyinStringArray(char ch)

转换为通用拼音。通用拼音的介绍见:http://zh.wikipedia.org/zh-cn/%E9%80%9A%E7%94%A8%E6%8B%BC%E9%9F%B3


static public String[] toWadeGilesPinyinStringArray(char ch)

转换为威妥玛拼音:http://zh.wikipedia.org/wiki/%E5%A8%81%E5%A6%A5%E7%91%AA%E6%8B%BC%E9%9F%B3


static public String[] toMPS2PinyinStringArray(char ch)

转换为注音符号拼音:http://zh.wikipedia.org/zh-cn/%E6%B3%A8%E9%9F%B3%E7%AC%A6%E8%99%9F


static public String[] toYalePinyinStringArray(char ch)

转换为耶魯拼音:http://zh.wikipedia.org/zh-cn/%E8%80%B6%E9%AD%AF%E6%8B%BC%E9%9F%B3


static public String[] toGwoyeuRomatzyhStringArray(char ch)

转换为国语罗马字:http://zh.wikipedia.org/wiki/%E5%9C%8B%E8%AA%9E%E7%BE%85%E9%A6%AC%E5%AD%97


对于”重“的拼音转换,以上方法分别得到的结果是:

汉语拼音:[zhong4, chong2]
通用拼音:[jhong4, chong2]
威妥玛拼音:[chung4, ch`ung2]
注音符号拼音:[jung4, chung2]
耶魯拼音:[jung4, chung2]
国语罗马字:[jonq, chorng]

好了,有了上面的基础,我们可以封装一个工具类,用来将汉字转换成拼音,这里只使用了汉字拼音。

首先要将pinyin4j加入项目中,如果是maven项目,可以添加引用:

<span style="white-space:pre">	</span><!-- 增加pinyin4j -->
        <dependency>
            <groupId>com.belerweb</groupId>
            <artifactId>pinyin4j</artifactId>
            <version>2.5.0</version>
        </dependency>
非maven的可以直接将下载好的jar包放入classpath。

然后编写工具类 PinyinTool.java:

package org.nerve.d3lesson.common.tools;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.Arrays;

/**
 *
 * Created by zengxm on 2014/12/4.
 */
public class PinyinTool {
    HanyuPinyinOutputFormat format = null;
    public static enum Type {
        UPPERCASE,              //全部大写
        LOWERCASE,              //全部小写
        FIRSTUPPER              //首字母大写
    }

    public PinyinTool(){
        format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    }

    public String toPinYin(String str) throws BadHanyuPinyinOutputFormatCombination{
        return toPinYin(str, "", Type.UPPERCASE);
    }

    public String toPinYin(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{
        return toPinYin(str, spera, Type.UPPERCASE);
    }

    /**
     * 将str转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换
     * 如: 明天 转换成 MINGTIAN
     * @param str
     * @param spera
     * @return
     * @throws BadHanyuPinyinOutputFormatCombination
     */
    public String toPinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination {
        if(str == null || str.trim().length()==0)
            return "";
        if(type == Type.UPPERCASE)
            format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        else
            format.setCaseType(HanyuPinyinCaseType.LOWERCASE);

        String py = "";
        String temp = "";
        String[] t;
        for(int i=0;i<str.length();i++){
            char c = str.charAt(i);
            if((int)c <= 128)
                py += c;
            else{
                t = PinyinHelper.toHanyuPinyinStringArray(c, format);
                if(t == null)
                    py += c;
                else{
                    temp = t[0];
                    if(type == Type.FIRSTUPPER)
                        temp = t[0].toUpperCase().charAt(0)+temp.substring(1);
                    py += temp+(i==str.length()-1?"":spera);
                }
            }
        }
        return py.trim();
    }
}

写个测试用例看看结果:




  • 18
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
好的,关于Java中判断汉字音调的问题,我可以给您提供一些思路和参考。 首先,Java本身并没有提供直接判断汉字音调的方法,但是通过使用第三方库可以实现这个功能。其中,pinyin4j库是一个比较常用的选择,它可以将汉字转换成带音调的拼音字符串。 以下是一个使用pinyin4j库的示例代码,可以将一个汉字字符串转换成带音调的拼音字符串: ```java import net.sourceforge.pinyin4j.PinyinHelper; public class TestPinyin { public static void main(String[] args) { String hanzi = "你好世界"; StringBuilder pinyin = new StringBuilder(); for (int i = 0; i < hanzi.length(); i++) { String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(hanzi.charAt(i)); if (pinyinArray != null) { pinyin.append(pinyinArray[0]); } else { pinyin.append(hanzi.charAt(i)); } } System.out.println(pinyin.toString()); } } ``` 上述代码中,我们通过遍历汉字字符串的每个字符,调用PinyinHelper类的toHanyuPinyinStringArray方法将其转换拼音字符串,并将结果拼接到StringBuilder对象中。如果该字符没有对应的拼音,则直接将其加入到结果中。 需要注意的是,pinyin4j库中的拼音字符串并不包含音调信息,而是使用数字代表不同的音调。例如,"nǐ hǎo shì jiè"中的音调分别为1、3、4、4,对应的拼音字符串为"ni3 hao3 shi4 jie4"。 如果您需要判断汉字字符串中的音调,可以在上述代码的基础上进行扩展。一种可行的思路是,通过判断每个拼音字符串中的数字来确定对应的音调。例如,以下代码可以统计汉字字符串中一共有几个一声、二声、三声和四声: ```java import net.sourceforge.pinyin4j.PinyinHelper; public class TestPinyin { public static void main(String[] args) { String hanzi = "你好世界"; StringBuilder pinyin = new StringBuilder(); int[] tones = new int[4]; // 存储四个音调的数量 for (int i = 0; i < hanzi.length(); i++) { String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(hanzi.charAt(i)); if (pinyinArray != null) { pinyin.append(pinyinArray[0]); for (int j = 0; j < pinyinArray[0].length(); j++) { char c = pinyinArray[0].charAt(j); if (c >= '1' && c <= '4') { int tone = c - '0' - 1; tones[tone]++; } } } else { pinyin.append(hanzi.charAt(i)); } } System.out.println(pinyin.toString()); System.out.println("一声:" + tones[0]); System.out.println("二声:" + tones[1]); System.out.println("三声:" + tones[2]); System.out.println("四声:" + tones[3]); } } ``` 上述代码中,我们首先定义了一个长度为4的整型数组tones,用于存储不同音调的数量。然后在遍历汉字字符串的每个字符时,将其转换拼音字符串,并统计其中的音调数量。最后输出结果时,可以根据tones数组中的数量来判断不同音调的出现次数。 希望这些提示能够对您有所帮助。如果您有其他问题,可以继续问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

集成显卡

码字不易,需要您的鼓励😄

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值