对HanLp汉语言处理包开源组件的简单使用demo
maven中引入依赖
<dependency>
<groupId>com.hackcs</groupId>
<artifactId>hanlp</artifactId>
<version>portable-1.8.4</version>
</dependency>
注意需在配置文件中引入依赖提供的data包data.zip
直接上代码
public class ChineseUtil {
public static void main(String[] args) {
String chongqing = new String("重庆");
String chengdu = new String("成都");
System.out.println(chineseToPingyin(chongqing));
System.out.println(chineseToPingyin(chengdu));
}
/**
* 将中文转换为拼音
* @return
*/
public static String chineseToPingyin(String chineseInput) {
List<Pinyin> pinyinList = PinyinDictionary.convertToPinyin(chineseInput);
StringBuilder stringBuilder = new StringBuilder();
pinyinList.stream().forEach(pinyin -> {
stringBuilder.append(pinyin.getPinyinWithoutTone());
});
return stringBuilder.toString();
}
}
运行结果