package util import ( "errors" "strings" "unicode/utf8" "github.com/qiniu/iconv" ) func GetPinyin(text string) (string, string, error) { text = strings.Trim(text, " ") if text == "" { return "", "", nil } cd, _ := iconv.Open("gbk", "utf-8") // convert utf-8 to gbk defer cd.Close() buf := []byte(text) outBuf := make([]byte, 1024) cnBytes, _, err := cd.Conv(buf, outBuf) if err != nil { return "", "", err } var ( cnsize int pinyin string jianpin string tempSpell string ) for { r, size := utf8.DecodeRune(buf) if r == utf8.RuneError { //循环结束 break } //cnStr := cd.ConvString(string(r)) if size == 1 { cnsize = 1 } else if size == 3 { cnsize = 2 } else { return "", "", errors.New("wrong utf8 size") } ascode := GetCnAscii(cnBytes[0:cnsize]) tempSpell = GetSpellByAscii(ascode) pinyin += tempSpell if cnsize == 2 { if len(tempSpell) > 1 && tempSpell[1] == 'h' { jianpin += tempSpell[0:2] } else if len(tempSpell) > 0 { jianpin += tempSpell[0:1] } } else { jianpin += tempSpell } buf = buf[size:] cnBytes = cnBytes[cnsize:] } return pinyin, jianpin, nil } func GetCnAscii(buf []byte) int { blen := len(buf) if blen <= 0 || blen > 2 { return 0 } if blen == 1 { return int(buf[0]) } if blen == 2 { ascii := (256*int(buf[0]) + int(buf[1])) - 256*256 return ascii } return 0 } func GetSpellByAscii(ascii int) string { if ascii > 0 && ascii < 160 { //单字符 return string([]byte{uint8(ascii)}) } if ascii < -20319 || ascii > -10247 { //不知道的字符 return "" } var ( asciiRangeLow = -20319 asciiRangeHigh int spellLow string spellHigh string ) pinyinList := getPyList() for _, pitem := range *pinyinList { spellHigh = pitem.Pinyin asciiRangeHigh = pitem.Code if ascii >= asciiRangeLow && ascii < asciiRangeHigh { if spellLow == "" { return spellHigh } else { return spellLow } } else { spellLow = spellHigh asciiRangeLow = asciiRangeHigh } } return "" } type pyItem struct { Pinyin string Code int } type PyList *[]pyItem var pyList PyList func getPyList() PyList { if pyList == nil { pyList = &[]pyItem{ pyItem{"a", -20319}, pyItem{"ai", -20317}, pyItem{"an", -20304}, pyItem{"ang", -20295}, pyItem{"ao", -20292}, pyItem{"ba", -20283}, pyItem{"bai", -20265}, pyItem{"ban", -20257}, pyItem{"bang", -20242}, pyItem{"bao", -20230}, pyItem{"bei", -20051}, pyItem{"ben", -20036}, pyItem{"beng", -20032}, pyItem{"bi", -20026}, pyItem{"bian", -20002}, pyItem{"biao", -19990}, pyItem{"bie", -19986}, pyItem{"bin", -19982}, pyItem{"bing", -19976}, pyItem{"bo", -19805}, pyItem{"bu", -19784}, pyItem{"ca", -19775}, pyItem{"cai", -19774}, pyItem{"can", -19763}, pyItem{"cang", -19756}, pyItem{"cao", -19751}, pyItem{"ce", -19746}, pyItem{"ceng", -19741}, pyItem{"cha", -19739}, pyItem{"chai", -19728}, pyItem{"chan", -19725}, pyItem{"chang", -19715}, pyItem{"chao", -19540}, pyItem{"che", -19531}, pyItem{"chen", -19525}, pyItem{"cheng", -19515}, pyItem{"chi", -19500}, pyItem{"chong", -19484}, pyItem{"chou", -19479}, pyItem{"chu", -19467}, pyItem{"chuai", -19289}, pyItem{"chuan", -19288}, pyItem{"chuang", -19281}, pyItem{"chui", -19275}, pyItem{"chun", -19270}, pyItem{"chuo", -19263}, pyItem{"ci", -19261}, pyItem{"cong", -19249}, pyItem{"cou", -19243}, pyItem{"cu", -19242}, pyItem{"cuan", -19238}, pyItem{"cui", -19235}, pyItem{"cun", -19227}, pyItem{"cuo", -19224}, pyItem{"da", -19218}, pyItem{"dai", -19212}, pyItem{"dan", -19038}, pyItem{"dang", -19023}, pyItem{"dao", -19018}, pyItem{"de", -19006}, pyItem{"deng", -19003}, pyItem{"di", -18996}, pyItem{"dian", -18977}, pyItem{"diao", -18961}, pyItem{"die", -18952}, pyItem{"ding", -18783}, pyItem{"diu", -18774}, pyItem{"dong", -18773}, pyItem{"dou", -18763}, pyItem{"du", -18756}, pyItem{"duan", -18741}, pyItem{"dui", -18735}, pyItem{"dun", -18731}, pyItem{"duo", -18722}, pyItem{"e", -18710}, pyItem{"en", -18697}, pyItem{"er", -18696}, pyItem{"fa", -18526}, pyItem{"fan", -18518}, pyItem{"fang", -18501}, pyItem{"fei", -18490}, pyItem{"fen", -18478}, pyItem{"feng", -18463}, pyItem{"fo", -18448}, pyItem{"fou", -18447}, pyItem{"fu", -18446}, pyItem{"ga", -18239}, pyItem{"gai", -18237}, pyItem{"gan", -18231}, pyItem{"gang", -18220}, pyItem{"gao", -18211}, pyItem{"ge", -18201}, pyItem{"gei", -18184}, pyItem{"gen", -18183}, pyItem{"geng", -18181}, pyItem{"gong", -18012}, pyItem{"gou", -17997}, pyItem{"gu", -17988}, pyItem{"gua", -17970}, pyItem{"guai", -17964}, pyItem{"guan", -17961}, pyItem{"guang", -17950}, pyItem{"gui", -17947}, pyItem{"gun", -17931}, pyItem{"guo", -17928}, pyItem{"ha", -17922}, pyItem{"hai", -17759}, pyItem{"han", -17752}, pyItem{"hang", -17733}, pyItem{"hao", -17730}, pyItem{"he", -17721}, pyItem{"hei", -17703}, pyItem{"hen", -17701}, pyItem{"heng", -17697}, pyItem{"hong", -17692}, pyItem{"hou", -17683}, pyItem{"hu", -17676}, pyItem{"hua", -17496}, pyItem{"huai", -17487}, pyItem{"huan", -17482}, pyItem{"huang", -17468}, pyItem{"hui", -17454}, pyItem{"hun", -17433}, pyItem{"huo", -17427}, pyItem{"ji", -17417}, pyItem{"jia", -17202}, pyItem{"jian", -17185}, pyItem{"jiang", -16983}, pyItem{"jiao", -16970}, pyItem{"jie", -16942}, pyItem{"jin", -16915}, pyItem{"jing", -16733}, pyItem{"jiong", -16708}, pyItem{"jiu", -16706}, pyItem{"ju", -16689}, pyItem{"juan", -16664}, pyItem{"jue", -16657}, pyItem{"jun", -16647}, pyItem{"ka", -16474}, pyItem{"kai", -16470}, pyItem{"kan", -16465}, pyItem{"kang", -16459}, pyItem{"kao", -16452}, pyItem{"ke", -16448}, pyItem{"ken", -16433}, pyItem{"keng", -16429}, pyItem{"kong", -16427}, pyItem{"kou", -16423}, pyItem{"ku", -16419}, pyItem{"kua", -16412}, pyItem{"kuai", -16407}, pyItem{"kuan", -16403}, pyItem{"kuang", -16401}, pyItem{"kui", -16393}, pyItem{"kun", -16220}, pyItem{"kuo", -16216}, pyItem{"la", -16212}, pyItem{"lai", -16205}, pyItem{"lan", -16202}, pyItem{"lang", -16187}, pyItem{"lao", -16180}, pyItem{"le", -16171}, pyItem{"lei", -16169}, pyItem{"leng", -16158}, pyItem{"li", -16155}, pyItem{"lia", -15959}, pyItem{"lian", -15958}, pyItem{"liang", -15944}, pyItem{"liao", -15933}, pyItem{"lie", -15920}, pyItem{"lin", -15915}, pyItem{"ling", -15903}, pyItem{"liu", -15889}, pyItem{"long", -15878}, pyItem{"lou", -15707}, pyItem{"lu", -15701}, pyItem{"lv", -15681}, pyItem{"luan", -15667}, pyItem{"lue", -15661}, pyItem{"lun", -15659}, pyItem{"luo", -15652}, pyItem{"ma", -15640}, pyItem{"mai", -15631}, pyItem{"man", -15625}, pyItem{"mang", -15454}, pyItem{"mao", -15448}, pyItem{"me", -15436}, pyItem{"mei", -15435}, pyItem{"men", -15419}, pyItem{"meng", -15416}, pyItem{"mi", -15408}, pyItem{"mian", -15394}, pyItem{"miao", -15385}, pyItem{"mie", -15377}, pyItem{"min", -15375}, pyItem{"ming", -15369}, pyItem{"miu", -15363}, pyItem{"mo", -15362}, pyItem{"mou", -15183}, pyItem{"mu", -15180}, pyItem{"na", -15165}, pyItem{"nai", -15158}, pyItem{"nan", -15153}, pyItem{"nang", -15150}, pyItem{"nao", -15149}, pyItem{"ne", -15144}, pyItem{"nei", -15143}, pyItem{"nen", -15141}, pyItem{"neng", -15140}, pyItem{"ni", -15139}, pyItem{"nian", -15128}, pyItem{"niang", -15121}, pyItem{"niao", -15119}, pyItem{"nie", -15117}, pyItem{"nin", -15110}, pyItem{"ning", -15109}, pyItem{"niu", -14941}, pyItem{"nong", -14937}, pyItem{"nu", -14933}, pyItem{"nv", -14930}, pyItem{"nuan", -14929}, pyItem{"nue", -14928}, pyItem{"nuo", -14926}, pyItem{"o", -14922}, pyItem{"ou", -14921}, pyItem{"pa", -14914}, pyItem{"pai", -14908}, pyItem{"pan", -14902}, pyItem{"pang", -14894}, pyItem{"pao", -14889}, pyItem{"pei", -14882}, pyItem{"pen", -14873}, pyItem{"peng", -14871}, pyItem{"pi", -14857}, pyItem{"pian", -14678}, pyItem{"piao", -14674}, pyItem{"pie", -14670}, pyItem{"pin", -14668}, pyItem{"ping", -14663}, pyItem{"po", -14654}, pyItem{"pu", -14645}, pyItem{"qi", -14630}, pyItem{"qia", -14594}, pyItem{"qian", -14429}, pyItem{"qiang", -14407}, pyItem{"qiao", -14399}, pyItem{"qie", -14384}, pyItem{"qin", -14379}, pyItem{"qing", -14368}, pyItem{"qiong", -14355}, pyItem{"qiu", -14353}, pyItem{"qu", -14345}, pyItem{"quan", -14170}, pyItem{"que", -14159}, pyItem{"qun", -14151}, pyItem{"ran", -14149}, pyItem{"rang", -14145}, pyItem{"rao", -14140}, pyItem{"re", -14137}, pyItem{"ren", -14135}, pyItem{"reng", -14125}, pyItem{"ri", -14123}, pyItem{"rong", -14122}, pyItem{"rou", -14112}, pyItem{"ru", -14109}, pyItem{"ruan", -14099}, pyItem{"rui", -14097}, pyItem{"run", -14094}, pyItem{"ruo", -14092}, pyItem{"sa", -14090}, pyItem{"sai", -14087}, pyItem{"san", -14083}, pyItem{"sang", -13917}, pyItem{"sao", -13914}, pyItem{"se", -13910}, pyItem{"sen", -13907}, pyItem{"seng", -13906}, pyItem{"sha", -13905}, pyItem{"shai", -13896}, pyItem{"shan", -13894}, pyItem{"shang", -13878}, pyItem{"shao", -13870}, pyItem{"she", -13859}, pyItem{"shen", -13847}, pyItem{"sheng", -13831}, pyItem{"shi", -13658}, pyItem{"shou", -13611}, pyItem{"shu", -13601}, pyItem{"shua", -13406}, pyItem{"shuai", -13404}, pyItem{"shuan", -13400}, pyItem{"shuang", -13398}, pyItem{"shui", -13395}, pyItem{"shun", -13391}, pyItem{"shuo", -13387}, pyItem{"si", -13383}, pyItem{"song", -13367}, pyItem{"sou", -13359}, pyItem{"su", -13356}, pyItem{"suan", -13343}, pyItem{"sui", -13340}, pyItem{"sun", -13329}, pyItem{"suo", -13326}, pyItem{"ta", -13318}, pyItem{"tai", -13147}, pyItem{"tan", -13138}, pyItem{"tang", -13120}, pyItem{"tao", -13107}, pyItem{"te", -13096}, pyItem{"teng", -13095}, pyItem{"ti", -13091}, pyItem{"tian", -13076}, pyItem{"tiao", -13068}, pyItem{"tie", -13063}, pyItem{"ting", -13060}, pyItem{"tong", -12888}, pyItem{"tou", -12875}, pyItem{"tu", -12871}, pyItem{"tuan", -12860}, pyItem{"tui", -12858}, pyItem{"tun", -12852}, pyItem{"tuo", -12849}, pyItem{"wa", -12838}, pyItem{"wai", -12831}, pyItem{"wan", -12829}, pyItem{"wang", -12812}, pyItem{"wei", -12802}, pyItem{"wen", -12607}, pyItem{"weng", -12597}, pyItem{"wo", -12594}, pyItem{"wu", -12585}, pyItem{"xi", -12556}, pyItem{"xia", -12359}, pyItem{"xian", -12346}, pyItem{"xiang", -12320}, pyItem{"xiao", -12300}, pyItem{"xie", -12120}, pyItem{"xin", -12099}, pyItem{"xing", -12089}, pyItem{"xiong", -12074}, pyItem{"xiu", -12067}, pyItem{"xu", -12058}, pyItem{"xuan", -12039}, pyItem{"xue", -11867}, pyItem{"xun", -11861}, pyItem{"ya", -11847}, pyItem{"yan", -11831}, pyItem{"yang", -11798}, pyItem{"yao", -11781}, pyItem{"ye", -11604}, pyItem{"yi", -11589}, pyItem{"yin", -11536}, pyItem{"ying", -11358}, pyItem{"yo", -11340}, pyItem{"yong", -11339}, pyItem{"you", -11324}, pyItem{"yu", -11303}, pyItem{"yuan", -11097}, pyItem{"yue", -11077}, pyItem{"yun", -11067}, pyItem{"za", -11055}, pyItem{"zai", -11052}, pyItem{"zan", -11045}, pyItem{"zang", -11041}, pyItem{"zao", -11038}, pyItem{"ze", -11024}, pyItem{"zei", -11020}, pyItem{"zen", -11019}, pyItem{"zeng", -11018}, pyItem{"zha", -11014}, pyItem{"zhai", -10838}, pyItem{"zhan", -10832}, pyItem{"zhang", -10815}, pyItem{"zhao", -10800}, pyItem{"zhe", -10790}, pyItem{"zhen", -10780}, pyItem{"zheng", -10764}, pyItem{"zhi", -10587}, pyItem{"zhong", -10544}, pyItem{"zhou", -10533}, pyItem{"zhu", -10519}, pyItem{"zhua", -10331}, pyItem{"zhuai", -10329}, pyItem{"zhuan", -10328}, pyItem{"zhuang", -10322}, pyItem{"zhui", -10315}, pyItem{"zhun", -10309}, pyItem{"zhuo", -10307}, pyItem{"zi", -10296}, pyItem{"zong", -10281}, pyItem{"zou", -10274}, pyItem{"zu", -10270}, pyItem{"zuan", -10262}, pyItem{"zui", -10260}, pyItem{"zun", -10256}, pyItem{"zuo", -10254}, } } return pyList }