java 实现汉字转拼音,java汉字简体转繁体 java汉字繁体转简体

    java 实现汉字转拼音,java汉字简体转繁体 java汉字繁体转简体

 

一、前言

java实现汉字转拼音,我的思路是需要一个字符和拼音的映射库。“我”=wo,”们“=men。 幸运的是不小心找到 nlp-lang 包,包含汉字转拼音,简体、繁体互转,等等。 下面来看看。

 

二、示例代码

1、依赖 pom.xml

<!-- https://mvnrepository.com/artifact/org.nlpcn/nlp-lang -->
<dependency>
    <groupId>org.nlpcn</groupId>
    <artifactId>nlp-lang</artifactId>
    <version>1.7.7</version>
</dependency>

2、创建 ToPinYin 类

import java.util.List;
import org.nlpcn.commons.lang.jianfan.JianFan;
import org.nlpcn.commons.lang.pinyin.Pinyin;
/**
 * description: java 汉字转拼音、汉字简体和繁体互转
 * @version v1.0
 * @author w
 * @date 2019年12月30日下午3:27:48
 **/
public class ToPinYin {
	
	/**
	 * description: 汉字转拼音
	 * @param chinese
	 * @return List<String>
	 * @version v1.0
	 * @author w
	 * @date 2019年12月30日 下午3:35:03
	 */
	public static List<String> pinYin(String chinese) {
		return Pinyin.pinyin(chinese);
	}
	
	/**
	 * description: 汉字转拼音~获取首字母
	 * @param chinese
	 * @return List<String>
	 * @version v1.0
	 * @author w
	 * @date 2019年12月30日 下午3:35:23
	 */
	public static List<String> firstPinYin(String chinese) {
		return Pinyin.firstChar(chinese);
	}
	
	/**
	 * description: 汉字转拼音~带音标
	 * @param chinese
	 * @return List<String>
	 * @version v1.0
	 * @author w
	 * @date 2019年12月30日 下午4:10:54
	 */
	public static List<String> unicodePinyin(String chinese) {
		return Pinyin.unicodePinyin(chinese);
	}
	
	/**
	 * description:简体中文转繁体中文
	 * @param chinese
	 * @return String
	 * @version v1.0
	 * @author w
	 * @date 2019年12月30日 下午4:11:17
	 */
	public static String j2f(String chinese) {
		return JianFan.j2f(chinese);
	}
	
	/**
	 * description: 繁体中文转简体
	 * @param chinese
	 * @return String
	 * @version v1.0
	 * @author w
	 * @date 2019年12月30日 下午4:11:33
	 */
	public static String f2j(String chinese) {
		return JianFan.f2j(chinese);
	}
	
	public static void main(String[] args) {
		String str = "中华人民";
		String str2 = "人民幣"; 
		System.out.println(pinYin(str));
		System.out.println(firstPinYin(str));
		System.out.println(unicodePinyin(str));
		System.out.println(j2f(str));
		System.out.println(f2j(str2));
	}
}

 

3、输出结果如下:

[zhong, hua, ren, min]
[z, h, r, m]
[zhōng, huá, rén, mín]
中華人民
人民币

 

三、总结

1、示例代码中仅仅展示了 nlp-lang 包的常用功能,其他功能还有:词共现统计、文章摘要、分词,词类相关度统计等等。

2、更多内容请移驾: https://github.com/NLPchina/nlp-lang

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值