使用pinyin4j实现汉字转拼音
2014-10-20 23:31
351 查看
1. maven项目,请在pom.xml里边添加包依赖相关配置:
2.编写实例代码:
3. 运行结果:alibaba
<dependency> <groupId>net.sourceforge.pinyin4j</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency>
2.编写实例代码:
/* * Copyright 2013 Alibaba.com All right reserved. This software is the * confidential and proprietary information of Alibaba.com ("Confidential * Information"). You shall not disclose such Confidential Information and shall * use it only in accordance with the terms of the license agreement you entered * into with Alibaba.com. */ package com.yunos.tv.server.controller.web; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; import org.junit.Test; /** * 类PinyinTest.java的实现描述:TODO 类实现描述 * @author riqi 2013-6-28 下午5:24:57 */ public class PinyinTest { @Test public void pinyinTest() { String input = "阿里巴巴"; StringBuilder pinyin = new StringBuilder(); for (int i = 0; i < input.length(); i++) { HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V); char c = input.charAt(i); String[] pinyinArray = null; try { pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat); } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } if (pinyinArray != null) { pinyin.append(pinyinArray[0]); } else if (c != ' ') { pinyin.append(input.charAt(i)); } } System.out.println(pinyin.toString().trim().toLowerCase()); } }
3. 运行结果:alibaba
相关文章推荐
- J2EE 使用pinyin4j实现汉字转换拼音
- AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字,Filterable的使用)
- 使用ORACLE函数实现按汉字拼音首字母查询
- 使用 pinyin4j API 将汉字转换为拼音 (学习笔记)
- Android使用pinyin4j完成简单汉字转拼音
- java 使用sourceforge.pinyin4j查询汉字拼音
- 如何使用pinyin4j的Java库进行汉字转拼音?
- 使用pinyin4j获取汉字的拼音和全角半角转换
- 使用pinyin4j将TreeMap按照key的汉字拼音排序
- 使用HanLP实现Android平台上汉字转拼音功能
- 使用PinYin4J汉字转拼音
- PHP实现的汉字拼音转换和公历农历转换类及使用示例
- pinyin4j实现汉字转拼音
- 用pinyin4j实现汉字转拼音
- 使用pinyin4j将汉字转换成拼音
- java使用PinYin4j将汉字转换为拼音
- 使用微软语言包实现汉字转拼音
- java实现中文汉字转拼音 Pinyin4j的基本用法
- java实现中文汉字转拼音 Pinyin4j的基本用法