Android汉字转拼音的方案.docx
《Android汉字转拼音的方案.docx》由会员分享,可在线阅读,更多相关《Android汉字转拼音的方案.docx(104页珍藏版)》请在冰豆网上搜索。
Android汉字转拼音的方案
A*********Inc.Confidential
Android汉字转拼音的方案
变更说明
序号
版本
变更原因
变更内容
变更人
变更日期
备注
1
V0.1
初稿
初稿
叶蕾
08/14/2017
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
媒体文件扫描方案
1前言
该文档为开发人员阅读。
1.1文档目的
文档编写目的:
为项目参与者阅读。
2开发环境和方案说明
说明如下:
1)Android系统:
AndroidKK以上版本。
2)开发环境:
JDK1.6以上版本,NDK开发环境。
3)开发工具:
Eclipse,AndroidDeveloptools
2.1汉字编码说明
当前汉字的编码主要有下面几种:
●UTF-8编码:
例如从Linux环境中编辑的文本文件,大部分是UTF-8编码。
会占用三个字节。
●GBK,GB2312等汉字编码:
该类编码在中文编码中,用的比较多,一般默认是GBK编码。
从通用性来考虑,一般只考虑GBK编码和UTF-8编码。
2.2对比pinyin4j开源库方案
谈到汉字转拼音,很多人都想直接用pinyin4j来实现。
但是pinyin4j存在如下缺点:
1.只能处理GBK的编码的汉字,无法处理UTF-8的汉字。
2.其效率较为低下,比较耗时。
2.3此方案核心思路和设计特点
核心的思路和设计特点如下:
1)针对GBK编码,采用Java层的一维数组的变量进行拼音的转码。
改一维数组能够从网络上获取,能够对应各个汉字的评语。
2)针对UTF-8编码,通过JNIC实现,UTF-8编码需要对应一个大小为20902的数组,遍历起来比较耗时,所以使用C语言实现效率较高。
3核心代码
3.1PingYingTool文件
3.1.1包引用和变量说明
packagehaoke.lyb.mediaScanner;
importjava.util.Iterator;
importjava.util.LinkedHashMap;
importjava.util.Set;
importcom.file.server.debug.DebugLog;
importcom.file.server.scan.ScanJni;
publicclassPingYingTool
{
privatestaticbooleanPINYIN_DEBUG=true;
3.1.2针对一个字符串获得汉字拼音
publicstaticStringparseString(Stringstr){
if(str==null)
returnnull;
StringBufferretStr=newStringBuffer();
StringfirstStr=getFirstSpell(str);
if(firstStr.equals(str)){
retStr.append(ScanJni.getPY(str));
}else{
retStr.append(firstStr);
if(PINYIN_DEBUG){
retStr.append(";;");
retStr.append(getFullSpell(str));
}
}
returnretStr.toString();
}
3.1.3获得单个汉字的Ascii
/**
*获得单个汉字的Ascii.
*@paramcnchar汉字字符
*@returnint错误返回0,否则返回ascii
*/
privatestaticintgetCnAscii(charcn){
byte[]bytes=(String.valueOf(cn)).getBytes();
if(bytes==null||bytes.length>2||bytes.length<=0){//错误
DebugLog.e("Yearlay","errorbytes.length:
"+bytes.length);
return0;
}
if(bytes.length==1){//英文字符
returnbytes[0];
}
if(bytes.length==2){//中文字符
inthightByte=256+bytes[0];
intlowByte=256+bytes[1];
intascii=(256*hightByte+lowByte)-256*256;
returnascii;
}
return0;//错误
}
3.1.4根据ASCII码到SpellMap中查找对应的拼音
/**
*根据ASCII码到SpellMap中查找对应的拼音
*@paramasciiint
*字符对应的ASCII
*@returnString
*拼音,首先判断ASCII是否>0&<160,如果是返回对应的字符,
*
否则到SpellMap中查找,如果没有找到拼音,则返回null,如果找到则返回拼音.
*/
publicstaticStringgetSpellByAscii(intascii){
if(ascii>0&&ascii<160){//单字符
returnString.valueOf((char)ascii);
}
if(ascii<-20319||ascii>-10247){//不知道的字符
returnnull;
}
SetkeySet=spellMap.keySet();
Iteratorit=keySet.iterator();
Stringspell0=null;
Stringspell=null;
intasciiRang0=-20319;
intasciiRang;
while(it.hasNext()){
spell=(String)it.next();
ObjectvalObj=spellMap.get(spell);
if(valObjinstanceofInteger){
asciiRang=((Integer)valObj).intValue();
if(ascii>=asciiRang0&&asciireturn(spell0==null)?
spell:
spell0;
}
else{
spell0=spell;
asciiRang0=asciiRang;
}
}
}
returnnull;
}
3.1.5汉字转化为全拼
/**
*返回字符串的全拼,是汉字转化为全拼,其它字符不进行转换
*@paramcnStrString
*字符串
*@returnString
*转换成全拼后的字符串
*/
publicstaticStringgetFullSpell(StringcnStr){
if(null==cnStr||"".equals(cnStr.trim())){
returncnStr;
}
char[]chars=cnStr.toCharArray();
StringBufferretuBuf=newStringBuffer();
for(inti=0,Len=chars.length;iintascii=getCnAscii(chars[i]);
if(ascii==0){//取ascii时出错
retuBuf.append(chars[i]);
}
else{
Stringspell=getSpellByAscii(ascii);
if(spell==null){
retuBuf.append(chars[i]);
}
else{
retuBuf.append(spell);
}//endofifspell==null
}//endofifascii<=-20400
}//endoffor
returnretuBuf.toString();
}
3.1.6获得一个汉字的首拼
/**
*第一个字取全拼,后面的字取首字母
*@paramcnStr
*@return返回转换后的拼
*/
publicstaticStringgetFirstSpell(StringcnStr){
if(null==cnStr||"".equals(cnStr.trim())){
returncnStr;
}
char[]chars=cnStr.toCharArray();
StringBufferretuBuf=newStringBuffer();
for(inti=0,Len=chars.length;iintascii=getCnAscii(chars[i]);
if(ascii==0){//取ascii时出错
retuBuf.append(chars[i]);
}else{
Stringspell=getSpellByAscii(ascii);
if(spell==null){
retuBuf.append(chars[i]);
}else{
retuBuf.append(spell.substring(0,1));
}
}
}//endoffor
returnretuBuf.toString();
}
3.1.7存储的汉字拼音对应的数组
privatestaticLinkedHashMapspellMap=null;
static{
if(spellMap==null){
spellMap=newLinkedHashMap(500);
}
initialize();
}
privatestaticvoidspellPut(Stringspell,intascii){
spellMap.put(spell,newInteger(ascii));
}
privatestaticvoidinitialize(){
spellPut("a",-20319);
spellPut("ai",-20317);
spellPut("an",-20304);
spellPut("ang",-20295);
spellPut("ao",-20292);
spellPut("ba",-20283);
spellPut("bai",-20265);
spellPut("ban",-20257);
spellPut("bang",-20242);
spellPut("bao",-20230);
spellPut("bei",-20051);
spellPut("ben",-20036);
spellPut("beng",-20032);
spellPut("bi",-20026);
spellPut("bian",-20002);
spellPut("biao",-19990);
spellPut("bie",-19986);
spellPut("bin",-19982);
spellPut("bing",-19976);
spellPut("bo",-19805);
spellPut("bu",-19784);
spellPut("ca",-19775);
spellPut("cai",-19774);
spellPut("can",-19763);
spellPut("cang",-19756);
spellPut("cao",-19751);
spellPut("ce",-19746);
spellPut("ceng",-19741);
spellPut("cha",-19739);
spellPut("chai",-19728);
spellPut("chan",-19725);
spellPut("chang",-19715);
spellPut("chao",-19540);
spellPut("che",-19531);
spellPut("chen",-19525);
spellPut("cheng",-19515);
spellPut("chi",-19500);
spellPut("chong",-19484);
spellPut("chou",-19479);
spellPut("chu",-19467);
spellPut("chuai",-19289);
spellPut("chuan",-19288);
spellPut("chuang",-19281);
spellPut("chui",-19275);
spellPut("chun",-19270);
spellPut("chuo",-19263);
spellPut("ci",-19261);
spellPut("cong",-19249);
spellPut("cou",-19243);
spellPut("cu",-19242);
spellPut("cuan",-19238);
spellPut("cui",-19235);
spellPut("cun",-19227);
spellPut("cuo",-19224);
spellPut("da",-19218);
spellPut("dai",-19212);
spellPut("dan",-19038);
spellPut("dang",-19023);
spellPut("dao",-19018);
spellPut("de",-19006);
spellPut("deng",-19003);
spellPut("di",-18996);
spellPut("dian",-18977);
spellPut("diao",-18961);
spellPut("die",-18952);
spellPut("ding",-18783);
spellPut("diu",-18774);
spellPut("dong",-18773);
spellPut("dou",-18763);
spellPut("du",-18756);
spellPut("duan",-18741);
spellPut("dui",-18735);
spellPut("dun",-18731);
spellPut("duo",-18722);
spellPut("e",-18710);
spellPut("en",-18697);
spellPut("er",-18696);
spellPut("fa",-18526);
spellPut("fan",-18518);
spellPut("fang",-18501);
spellPut("fei",-18490);
spellPut("fen",-18478);
spellPut("feng",-18463);
spellPut("fo",-18448);
spellPut("fou",-18447);
spellPut("fu",-18446);
spellPut("ga",-18239);
spellPut("gai",-18237);
spellPut("gan",-18231);
spellPut("gang",-18220);
spellPut("gao",-18211);
spellPut("ge",-18201);
spellPut("gei",-18184);
spellPut("gen",-18183);
spellPut("geng",-18181);
spellPut("gong",-18012);
spellPut("gou",-17997);
spellPut("gu",-17988);
spellPut("gua",-17970);
spellPut("guai",-17964);
spellPut("guan",-17961);
spellPut("guang",-17950);
spellPut("gui",-17947);
spellPut("gun",-17931);
spellPut("guo",-17928);
spellPut("ha",-17922);
spellPut("hai",-17759);
spellPut("han",-17752);
spellPut("hang",-17733);
spellPut("hao",-17730);
spellPut("he",-17721);
spellPut("hei",-17703);
spellPut("hen",-17701);
spellPut("heng",-17697);
spellPut("hong",-17692);
spellPut("hou",-17683);
spellPut("hu",-17676);
spellPut("hua",-17496);
spellPut("huai",-17487);
spellPut("huan",-17482);
spellPut("huang",-17468);
spellPut("hui",-17454);
spellPut("hun",-17433);
spellPut("huo",-17427);
spellPut("ji",-17417);
spellPut("jia",-17202);
spellPut("jian",-17185);
spellPut("jiang",-16983);
spellPut("jiao",-16970);
spellPut("jie",-16942);
spellPut("jin",-16915);
spellPut("jing",-16733);
spellPut("jiong",-16708);
spellPut("jiu",-16706);
spellPut("ju",-16689);
spellPut("juan",-16664);
spellPut("jue",-16657);
spellPut("jun",-16647);
spellPut("ka",-16474);
spellPut("kai",-16470);
spellPut("kan",-16465);
spellPut("kang",-16459);
spellPut("kao",-16452);
spellPut("ke",-16448);
spellPut("ken",-16433);
spellPut("keng",-16429);
spellPut("kong",-16427);
spellPut("kou",-16423);
spellPut("ku",-16419);
spellPut("kua",-16412);
spellPut("kuai",-16407);
spellPut("kuan",-16403);
spellPut("kuang",-16401);
spellPut("kui",-16393);
spellPut("kun",-16220);
spellPut("kuo",-16216);
spellPut("la",-16212);
spellPut("lai",-16205);
spellPut("lan",-16202);
spellPut("lang",-16187);
spellPut("lao",-16180);
spellPut("le",-16171);
spellPut("lei",-16169);
spellPut("leng",-16158);
spellPut("li",-16155);
spellPut("lia",-15959);
spellPut("lian",-15958);
spellPut("liang",-15944);
spellPut("liao",-15933);
spellPut("lie",-15920);
spellPut("lin",-15915);
spe