汉语拼音工具包相关实用方法
1.工具包
pinyin4j-2.5.0.jar
2.工具类,其中包含方法:
(1)getPingYin---用来获取汉字的汉语拼音,英文以及符号不变 多音字多为常用的拼音,无法具体识别
(2)getFirstSpell---获取汉字的首首个字母,英文不变,中文符号不允许出现
(3)getFullSpell---将汉字变成汉语拼音,英文不变,不允许出现中文符号
(4)toDBC--- 将某些中文标点符号变为英文标点符号
(5)firstWordToUp---将英文语句每个单词首字母变大写
(6)changePingYin---把一段汉字变成汉语拼音式的语句
代码如下:
public class PingYinUtil {
/**
* 获取汉字的汉语拼音,英文以及符号不变 多音字多为常用的拼音,无法具体识别
*
* @param inputString
* @return
*/
public static String getPingYin(String inputString) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
char[] input = inputString.trim().toCharArray();
String output = "";
try {
for (int i = 0; i < input.length; i++) {
if (java.lang.Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {// 正则表达式,代表至少匹配一个到汉字
String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);// 匹配到汉字就将其变成拼音并拼接
output += temp[0];
} else
output += java.lang.Character.toString(input[i]);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
return output;
}
/**
* 获取汉字的首首个字母,英文不变,中文符号不允许出现
*
* @param chinese
* @return
*/
public static String getFirstSpell(String chinese) {
StringBuffer pybf = new StringBuffer();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 128) {// 用于判断是不是键盘上的单个字符,是就不用变直接拼接,不是,就将其汉语拼音化
try {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
if (temp != null) {
pybf.append(temp[0].charAt(0));
} else {
System.out.println("出现非法字符");
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(arr[i]);
}
}
// 用于去除除单词字符的所有其他字符
return pybf.toString().replaceAll("\\W", "").trim();// 正则表达式表示匹配任何非单词字符。等价于“[^A-Za-z0-9_]”
}
/**
* 将汉字变成汉语拼音,英文不变,不允许出现中文符号
*
* @param chinese
* @return
*/
public static String getFullSpell(String chinese) {
StringBuffer pybf = new StringBuffer();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 128) {
try {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
// 是否过滤非法字符
if (temp != null) {
pybf.append(temp[0]);
} else {
pybf.append(arr[i]);// 不过滤非法字符
System.out.println("出现非法字符");
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(arr[i]);
}
}
return pybf.toString();
}
/**
* 将某些中文标点符号变为英文标点符号
*/
public static String toDBC(String input) {
char[] c = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == 12288) {
c[i] = (char) 32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char) (c[i] - 65248);
}
return new String(c);
}
/**
*将英文语句每个单词首字母变大写
*
*/
public static String firstWordToUp(String input) {
StringBuffer pybf = new StringBuffer();
char[] arr = input.toCharArray();
boolean flag=false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < 128) {// 用于判断是不是键盘上的单个字符
String temp = Character.toString(arr[i]);
if (i != 0) {
if (temp!=null) {
if(!flag){
if(temp.equals("!")||temp.equals(".")||temp.equals("?")){
flag=true;
pybf.append(temp);
}else
pybf.append(temp);
}else{
pybf.append(temp.toUpperCase());
flag=false;
}
}else{
System.out.println("包含非法字符");
}
}else{
pybf.append(temp.toUpperCase());
}
} else {
System.out.println("包含非法字符!");
}
}
return pybf.toString();
}
/**
* 把一段话变成汉语拼音式的语句
*
* @param inputString
* @return
*/
public static String changePingYin(String inputString) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
char[] input = inputString.trim().toCharArray();
String output = "";
try {
for (int i = 0; i < input.length; i++) {
String code=java.lang.Character.toString(input[i]);
if (code.matches("[\\u4E00-\\u9FA5]+")) {// 正则表达式,代表至少匹配一个到汉字
String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);// 匹配到汉字就将其变成拼音并拼接
output += temp[0];
output +=" ";
} else if(code.equals("!")||code.equals("!")||code.equals(".")||code.equals("。")||code.equals("?")||code.equals("?")||code.equals(",")||code.equals(","))
output += java.lang.Character.toString(input[i]);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
return output;
}
3.工具类有些地方不完善,很多地方写的也不严谨,有网上查阅的,也有自己编写的,标点符号转换的时候,由于句号不能实现,个人觉得多加个判断应该就可以了,汉语拼音化那里,标点前多了个空格,应该也需要多个判断,自己懒得写了,也没有考虑其他标点符号的情况,总之,很多地方不完善啦,主要最近公司没有事做,随便写写,理理自己的逻辑判断能力,也温习一下基本的方法,以后也算做个参考吧*-*
汉语拼音工具包相关实用方法相关推荐
- ipa解包打包工具_ios打包ipa的四种实用方法(.app转.ipa)
总结一下,目前.app包转为.ipa包的方法有以下几种: 1.Apple推荐的方式,即实用xcode的archive功能 Xcode菜单栏->Product->Archive->三选 ...
- linux 查看防火墙对应的进程,Linux系统防火墙进程查看的实用方法
Linux系统防火墙进程查看的实用方法 作者 HonestQiao 2008年03月17日 16:00 启动防火墙 1) 重启后生效 开启: chkconfig iptables on 关闭: chk ...
- Android客户端连接服务器- OKHttp的简单实用方法
文章目录 一 .OKHttp简介 二. OkHttp3使用 1.创建HTTPClient实例 2.Get请求 3.POST请求 三.Timeouts(超时) **写在前面:本文只是对OKHttp3的简 ...
- 有效的网络推广超级实用方法
我叫龙雨,先后在百度搜狗工作过3年,后来一直负责一家公司的的网络营销! 不知道大家有没有听过1+1+1>3这样一个概念,简单来说一下这概念! 第一呢就是自己的资源,把自己的资源维护好开发好;第二 ...
- 6种数据分析实用方法,终于有人讲明白了
导读:在<终于有人把AI.BI.大数据.数据科学讲明白了>中,我们讨论了分析以及相关的一些概念,如大数据和数据科学.现在我们将注意力转向分析中使用的实用方法,包括各种分析工具. 具体来说, ...
- 网站推广策略-网站推广120种实用方法_打杂的_新浪博客
介绍实用有效的企业网站推广方法,网站推广计划,网站推广方案和网站推广案例等.<网站推广策略>用网络营销的系统性思想研究网站推广,将网站推广作为企业网络营销策略的组成部分. 网站推广120种 ...
- 网站推广120种实用方法系列连载
·关联网站推广策略(1)--关联网站的作用 (2005-04-12) [摘要]:关联网站推广策略的基本思想是值得借鉴的,只要在实际应用中合理利用关联网站之间的关系,不要使之成为虚假网站流量的工具. ...
- 网站关键词排名7天上首页的实用方法
话说都说深夜的酒不如凌晨的豆浆,我看是白天的热闹不及凌晨的灵感,广东的初冬,半夜微微一凉,由于个人身体的原因,2019年极少的去关注SEO方面的资讯,也极少去写文章,今年也许我错过了很多,所以现在想为 ...
- 弥合鸿沟:一种生成内部威胁数据的实用方法(Bridging the Gap: A Pragmatic Approach to Generating Insider Threat Data )
Bridging the Gap: A Pragmatic Approach to Generating Insider Threat Data 弥合鸿沟:一种生成内部威胁数据的实用方法 摘要:恶意内 ...
最新文章
- Rsync和Sersync(企业实时同步方案)
- 「Ubuntu」Problem executing scripts APT::Update::Post-Invoke-Success ‘if /usr/bin/test -w /var/lib
- 5.2k Star!一款 Python 实现的美观终端资源监视器
- 【转】LDA数学八卦
- C++计算函数执行时间的两种方法
- Windows - 修复所有快捷方式的打开方式
- Delphi XE10.4字体字号对应的Font Size的点或像素换算
- 360全景图PS更换天空背景
- 量化交易8-backtrader回测道氏理论中反转形态K线形态图
- contiki学习笔记(五)ctimer和etimer
- 水题poj1423 解题报告
- 点击echarts柱状图动态改变数据项颜色样式
- 正则测试以及练习网站
- 归纳编程语言的语法grammer
- js计时器实现页面刷新和幻灯片效果
- 常用的概率分布:伯努利分布、二项分布、多项式分布、高斯分布、指数分布、拉普拉斯分布和Dirac-delta分布
- 玻璃心?App Store下架PP助手与360手机助手,做aso会被苹果下架吗
- 用户画像系列——什么是用户画像?金融行业大数据用户画像实践
- 十大单身狗最需要的APP
- 童夫尧:进入物联网时代 中国如何弯道超车
热门文章
- 网线每根的含义以及类别和距离传输问题
- u盘怎么样在w ndows7上使用,用U盘给MAC BOOK AIR安装windows7系统
- 面向对象分析与设计中的基本事件处理的机制及其优缺点对比
- Xshell实现windows上传文件到Linux系统
- Web GIS多种方式发布动态地图服务及显示(2)
- openstack核心组件-块存储 (Block Storage) : Cinder
- 计算机 总线带宽计算,电脑各种硬件带宽的计算
- 网络工程师提高篇 | 路由重发布你了解多少?从原理到配置,瑞哥带你学习一波!
- [C++] 小游戏—三国杀
- 人工智能给我们带来新的创业机会,你愿意做局外人吗?