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

发布了156 篇原创文章 · 获赞 159 · 访问量 49万+

猜你喜欢

转载自blog.csdn.net/HaHa_Sir/article/details/103971642