package com.anxin.ssk.common;import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;/*** 取得给定汉字串的首字母串,即声母串 Title: ChineseCharToEn(含常用汉字,不常见汉字及多音字)* * @author: (le.qiao)* @e-mail: qiaolevip@gmail.com* @myblog: <a href="http://qiaolevip.iteye.com">http://qiaolevip.iteye.com</a>* @date: 2014-1-15 注:只支持GB2312字符集中的汉字* */
public class LetterUtil {private final static int[] li_SecPosValue = { 1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858, 4027, 4086,4390, 4558, 4684, 4925, 5249, 5590 };private final static String[] lc_FirstLetter = { "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "w", "x","y", "z" };private static Map<String, String> exceptWords = new HashMap<String, String>();static {exceptWords.put("a", "庵鳌");exceptWords.put("b", "璧亳並侼別匂");exceptWords.put("c", "茌丞丒丳刅");exceptWords.put("d", "渎砀棣儋丟");exceptWords.put("e", "");exceptWords.put("f", "邡冹兝");exceptWords.put("g", "崮藁莞丐丱乢亁仠冮匃匄");exceptWords.put("h", "骅珲潢湟丆冴匢");exceptWords.put("j", "泾蛟暨缙旌莒鄄丌丩丮丯丼亅伋冏匊匛匞");exceptWords.put("k", "丂匟");exceptWords.put("l", "崂涞栾溧漯浏耒醴泸阆崃両刢劽啰");exceptWords.put("m", "渑汨丏冐冺兞冇");exceptWords.put("n", "");exceptWords.put("o", "瓯");exceptWords.put("p", "邳濮郫丕伂冸");exceptWords.put("q", "喬綦衢岐朐邛丠丬亝冾兛匤");exceptWords.put("r", "榕刄");exceptWords.put("s", "泗睢沭嵊歙莘嵩鄯丄丗侺兙");exceptWords.put("t", "潼滕郯亣侹侻");exceptWords.put("w", "婺涠汶亾仼卍卐");exceptWords.put("x", "鑫盱浔荥淅浠亵丅伈兇");exceptWords.put("y", "懿眙黟颍兖郓偃鄢晏丣亜伇偐円匜");exceptWords.put("z", "梓涿诏柘秭圳伀冑刣");}private final static String polyphoneTxt = "重庆|cq,音乐|yy";/*** 取得给定汉字串的首字母串,即声母串* * @param str 给定汉字串* @return 声母串*/public static String getAllFirstLetter(String str) {if (str == null || str.trim().length() == 0) {return "";}// 多音字判定for (String polyphone : polyphoneTxt.split(",")) {String[] chinese = polyphone.split("[|]");if (str.indexOf(chinese[0]) != -1) {str = str.replace(chinese[0], chinese[1]);}}String _str = "";for (int i = 0; i < str.length(); i++) {_str = _str + getFirstLetter(str.substring(i, i + 1));}return _str;}/*** 取得给定汉字的首字母,即声母* * @param chinese 给定的汉字* @return 给定汉字的声母*/public static String getFirstLetter(String chinese) {if (chinese == null || chinese.trim().length() == 0) {return "";}String chineseTemp = chinese;chinese = conversionStr(chinese, "GB2312", "ISO8859-1");if (chinese.length() > 1) {// 判断是不是汉字int li_SectorCode = (int) chinese.charAt(0); // 汉字区码int li_PositionCode = (int) chinese.charAt(1); // 汉字位码li_SectorCode = li_SectorCode - 160;li_PositionCode = li_PositionCode - 160;int li_SecPosCode = li_SectorCode * 100 + li_PositionCode; // 汉字区位码if (li_SecPosCode > 1600 && li_SecPosCode < 5590) {for (int i = 0; i < 23; i++) {if (li_SecPosCode >= li_SecPosValue[i] && li_SecPosCode < li_SecPosValue[i + 1]) {chinese = lc_FirstLetter[i];break;}}} else {// 非汉字字符,如图形符号或ASCII码chinese = matchPinYin(chinese);}}// 如还是无法匹配,再次进行拼音匹配if (chinese.equals("?")) {chinese = matchPinYin(chineseTemp, false);}return chinese;}/*** 汉字匹配拼音对照* * @param chinese* @return*/private static String matchPinYin(String chinese, boolean needConvert) {String chineseTemp = chinese;if (needConvert) {chinese = conversionStr(chinese, "ISO8859-1", "GB2312");}chinese = chinese.substring(0, 1);// findRepeatWord(exceptWords);for (Entry<String, String> letterSet : exceptWords.entrySet()) {if (letterSet.getValue().indexOf(chinese) != -1) {chinese = letterSet.getKey();break;}}chinese = chineseTemp.equals(chinese) ? "?" : chinese;return chinese;}private static String matchPinYin(String chinese) {return matchPinYin(chinese, true);}/*** 字符串编码转换* * @param str 要转换编码的字符串* @param charsetName 原来的编码* @param toCharsetName 转换后的编码* @return 经过编码转换后的字符串*/private static String conversionStr(String str, String charsetName, String toCharsetName) {try {str = new String(str.getBytes(charsetName), toCharsetName);} catch (UnsupportedEncodingException ex) {System.out.println("字符串编码转换异常:" + ex.getMessage());}return str;}public static void main(String[] args) {String content = "丬";String pinyin = "";String contentAll = "";String pinyinAll = "";for (int i = 21000; i <= 22000; i++) {content = ((char) i) + "";pinyin = LetterUtil.getFirstLetter(content);if (pinyin.equals("?")) {contentAll += content;pinyinAll += pinyin;}}System.out.println("获取拼音首字母:" + contentAll + ":" + pinyinAll);// String address = "(金浜小区)栖山路1689弄";// address = address.substring(address.indexOf(")") + 1);// System.out.println("获取拼音首字母:" + LetterUtil.getFirstLetter(address));}@SuppressWarnings("unused")private static void findRepeatWord(Map<String, String> wordsMap) {String words = wordsMap.values().toString().replaceAll("[, ]", "");words = words.substring(1, words.length() - 1);for (char word : words.toCharArray()) {int count = findStr2(words, String.valueOf(word));if (count > 1) {System.out.println("汉字:【" + word + "】出现了" + count + "次!");}}}private static int findStr2(String srcText, String keyword) {int count = 0;Pattern p = Pattern.compile(keyword);Matcher m = p.matcher(srcText);while (m.find()) {count++;}return count;}
}

java取汉字拼音首字母含多音字及不常见的字相关推荐

  1. java获取汉字拼音首字母A

    public class GetChinessFirstSpell{ /// <summary> /// 汉字拼音首字母列表 本列表包含了20901个汉字,用于配合 GetChineseS ...

  2. 通过JS取汉字拼音首字母

    Javascript实现的取中文拼音首字母,提供了多音字的选择. <html> <head> <meta http-equiv="Content-Type&qu ...

  3. js取汉字拼音首字母

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  4. Sql Server取汉字拼音首字母和汉字首笔划

    --将中文字符串转化成文字首拼音的组合 create function fun_getPY(@str nvarchar(4000)) returns nvarchar(4000) as begin d ...

  5. 两种取汉字拼音首字母的方法.--函数为转抄而来.

    1.使用字库得到一个汉字的拼音首字母. 2.使用ORACLE 中的 NLSSORT,得到一个汉字的排序顺序,从而算出拼音的首字母, 但两种方法都存在有一字多音的问题.如一个字有多种拼音,就可能和你想要 ...

  6. java按照汉字拼音首字母排序

    实现按照汉字拼音字母排序的方法有两种: 一,就是在用java代码处理 要实现汉字按首字母排序,主要是设置语言环境,如下语句设置语言环境: 这里用到了Collator类,此类实现了Comparator接 ...

  7. 取汉字拼音首字母--生成不重复ID(汉字--拼音--首字母)

    工作上需要生成一些不重复的Id,并且Id是固定前缀加客户拼音首字母相连产生的,如有重复,末尾加数字 首先是汉字转拼音的jar包 public String getUniqueAk(String cus ...

  8. php取汉字拼音首字母,php获取汉字拼音首字母的函数(真正可以使用的)

    //php获取中文字符拼音首字母 function getFirstCharter($str){ if(empty($str)){return '';} $fchar=ord($str{0}); if ...

  9. 取汉子拼音首字母的C#和VB.Net方法

    最近修改了项目中的一个bug,是因为取汉子拼音首字母的方法有问题导致一些字的首字母获取错误,在网上找了一些现成的方法进行测试,发现下面的方法虽然比较笨,但准确率还是挺高. C#代码 /// <s ...

最新文章

  1. java创建线程的2种方式
  2. 学习ID,ClientID,UniqueID
  3. 6 不更新无法使用_win10更新后无线网络无法使用
  4. 初级程序员需要接触好的架构代码
  5. 统计xml文件中的标签出现框数及出现过的图片数
  6. 网站设计中很重要的概念div+浮动
  7. 浅析HTTP代理原理--转
  8. 配置主机间信任的一个简单办法
  9. soapUI Pro 4.5.1的新破解方案
  10. 施乐j75服务器电源管理系统,富士施乐推出印刷系统 Color J75 Press
  11. 个性化系统推送 java_个性化推送系统
  12. 需求分析与原型设计———记账软件
  13. Mybaits-Plus Invalid bound statement (not found) 问题
  14. 新版Android Studio Logcat view使用简明教程
  15. 计算机里面怎么建新的文档,【2人回答】怎样在电脑新建中添加Word文档?-3D溜溜网...
  16. 小白都能看得懂的ZBrush基础教学
  17. unity黑白滤镜_Unity图片处理类,包括压缩、截屏和滤镜
  18. 小米android手机同步数据,怎样将旧手机里面的数据,丝毫不差的转移到新手机?一键教你搞定...
  19. oracle和mysql查询条件排序_Oracle数据库中ORDERBY排序和查询按IN条件的顺序输出
  20. 需要用计算机权限才能删除,您需要计算机管理员提供的权限才能对此文件进行更改,删不掉文件怎么处理...

热门文章

  1. 乘幂法matlab,用Matlab进行线性回归、乘幂回归、指数回归方法之一(最小二乘法)...
  2. 只要2.5分钟!机器手轻松完成最危险复杂的手术
  3. 基于Python的复杂环境中车道线自动检测系统
  4. NIO详解Channel、Buffer、Selector看这一篇就够了
  5. 罗马字符转化为阿拉伯数字
  6. 帧动画和补间动画看这篇足够了
  7. 学习笔记:Qt与Matlab混合编程及遇到的诸多问题(附DEMO)
  8. Linux中使用SCP命令进行上传或者下载文件
  9. 【Nginx】使用nginx进行端口转发
  10. 栈的应用-括号匹配的检验