C++中获取汉字拼音首字缩写/全拼及生僻字的处理

  最近一直在修改关于搜索不到生僻字的问题,最后得出结论:对生僻字的处理,办法只有一个,建立一个字库,然后查表找。

  可以参考一下:http://download.csdn.net/detail/lshlwy/8920047,枚举了两万多的汉字和拼音音调,直接去搜就好了。

1、关于生僻字的处理,加载一个汉字库,然后库中查找。(仅适用于生僻字,频繁查询字库文件,会非常的慢)对于这种循环查询的方法,可能会把程序变慢,匹配度一般。

 1         bool existFlag = false;
 2             if (firstPY.Find(strLowName) == -1 && util::existChinese(util::cStringToString(firstPY, CP_ACP)))//判断获得的首字拼音中是否还存在汉字,如果存在,说明有生僻字。
 3             {
 4                 std::vector  specialCharaterPy;
 5                 module::getMiscModule()->GetSentencePinyin(util::cStringToString(RealName, CP_ACP), specialCharaterPy);//这里是查找汉字库,返回全拼。
 6  CString Namepy; 7 Namepy = util::stringToCString(specialCharaterPy.at(0)); 8 int count = 0; 9 int findPos = 0; 10 int startPos = 0; 11 for (int i = 0; i < sShortName.GetLength(); i++) 12  { 13 findPos = Namepy.Find(sShortName[i], startPos); 14 if (findPos == -1) 15  { 16 break; 17  } 18 else 19  { 20 count++; 21 startPos = findPos + 1;//从查找到位置后+1继续寻找 22  } 23  } 24 if (count != 0 && count == sShortName.GetLength()) 25  { 26 existFlag = true; 27  } 28 }

 2.生僻字的存储跟普通的汉子有所区别,贴一个找到汉字拼音缩写的代码(无法查找到生僻字,生僻字的两个一个大于160,一个小于160,会返回原汉字)。

 1 std::string FirstLett(const char* strChs)
 2 {
 3     static int li_SecPosValue[] = {
 4         1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787, 3106, 3212,
 5         3472, 3635, 3722, 3730, 3858, 4027, 4086, 4390, 4558, 4684, 4925, 5249
 6     };
 7     static char* lc_FirstLetter[] = {
 8         "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "o",
 9         "p", "q", "r", "s", "t", "w", "x", "y", "z"
10     };
11     static char* ls_SecondSecTable =
12         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;
26     int W = 0;
27     UINT stringlen = strlen(strChs);
28     for (UINT i = 0; i < stringlen; i++) {
29         H = (UCHAR)(strChs[i + 0]);
30         L = (UCHAR)(strChs[i + 1]);
31         if (H < 0xA1 || L < 0xA1) { //这里出现生僻字问题
32             result += strChs[i];
33             continue;
34         }
35         else {
36             W = (H - 160) * 100 + L - 160;
37         }
38         if (W > 1600 && W < 5590) {
39             for (int j = 22; j >= 0; j--) {
40                 if (W >= li_SecPosValue[j]) {
41                     result += lc_FirstLetter[j];
42                     i++;
43                     break;
44                 }
45             }
46             continue;
47         }
48         else {
49             i++;
50             W = (H - 160 - 56) * 94 + L - 161;
51             if (W >= 0 && W <= 3007)
52                 result += ls_SecondSecTable[W];
53             else {
54                 result += (char)H;
55                 result += (char)L;
56             }
57         }
58     }
59 
60     return result;
61 }

 

3.获取到汉字全拼的方法代码:(对于生僻字无法处理)

  1 char* ChineseToPyt(wchar_t* chrstr)
  2 {
  3     const  int pyvalue[] = { -20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257, -20242, -20230, -20051, -20036, -20032, -20026,
  4         -20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775, -19774, -19763, -19756, -19751, -19746, -19741, -19739, -19728,
  5         -19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484, -19479, -19467, -19289, -19288, -19281, -19275, -19270, -19263,
  6         -19261, -19249, -19243, -19242, -19238, -19235, -19227, -19224, -19218, -19212, -19038, -19023, -19018, -19006, -19003, -18996,
  7         -18977, -18961, -18952, -18783, -18774, -18773, -18763, -18756, -18741, -18735, -18731, -18722, -18710, -18697, -18696, -18526,
  8         -18518, -18501, -18490, -18478, -18463, -18448, -18447, -18446, -18239, -18237, -18231, -18220, -18211, -18201, -18184, -18183,
  9         -18181, -18012, -17997, -17988, -17970, -17964, -17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752, -17733, -17730,
 10         -17721, -17703, -17701, -17697, -17692, -17683, -17676, -17496, -17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202,
 11         -17185, -16983, -16970, -16942, -16915, -16733, -16708, -16706, -16689, -16664, -16657, -16647, -16474, -16470, -16465, -16459,
 12         -16452, -16448, -16433, -16429, -16427, -16423, -16419, -16412, -16407, -16403, -16401, -16393, -16220, -16216, -16212, -16205,
 13         -16202, -16187, -16180, -16171, -16169, -16158, -16155, -15959, -15958, -15944, -15933, -15920, -15915, -15903, -15889, -15878,
 14         -15707, -15701, -15681, -15667, -15661, -15659, -15652, -15640, -15631, -15625, -15454, -15448, -15436, -15435, -15419, -15416,
 15         -15408, -15394, -15385, -15377, -15375, -15369, -15363, -15362, -15183, -15180, -15165, -15158, -15153, -15150, -15149, -15144,
 16         -15143, -15141, -15140, -15139, -15128, -15121, -15119, -15117, -15110, -15109, -14941, -14937, -14933, -14930, -14929, -14928,
 17         -14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889, -14882, -14873, -14871, -14857, -14678, -14674, -14670, -14668,
 18         -14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399, -14384, -14379, -14368, -14355, -14353, -14345, -14170, -14159,
 19         -14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123, -14122, -14112, -14109, -14099, -14097, -14094, -14092, -14090,
 20         -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905, -13896, -13894, -13878, -13870, -13859, -13847, -13831, -13658,
 21         -13611, -13601, -13406, -13404, -13400, -13398, -13395, -13391, -13387, -13383, -13367, -13359, -13356, -13343, -13340, -13329,
 22         -13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095, -13091, -13076, -13068, -13063, -13060, -12888, -12875, -12871,
 23         -12860, -12858, -12852, -12849, -12838, -12831, -12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556, -12359, -12346,
 24         -12320, -12300, -12120, -12099, -12089, -12074, -12067, -12058, -12039, -11867, -11861, -11847, -11831, -11798, -11781, -11604,
 25         -11589, -11536, -11358, -11340, -11339, -11324, -11303, -11097, -11077, -11067, -11055, -11052, -11045, -11041, -11038, -11024,
 26         -11020, -11019, -11018, -11014, -10838, -10832, -10815, -10800, -10790, -10780, -10764, -10587, -10544, -10533, -10519, -10331,
 27         -10329, -10328, -10322, -10315, -10309, -10307, -10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254 };
 28     const char pystr[396][7] = { "a", "ai", "an", "ang", "ao", "ba", "bai", "ban", "bang", "bao", "bei", "ben", "beng", "bi", "bian", "biao",
 29         "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao", "ce", "ceng", "cha", "chai", "chan", "chang", "chao", "che", "chen",
 30         "cheng", "chi", "chong", "chou", "chu", "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", "cong", "cou", "cu", "cuan", "cui",
 31         "cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng", "di", "dian", "diao", "die", "ding", "diu", "dong", "dou", "du", "duan",
 32         "dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", "fu", "ga", "gai", "gan", "gang", "gao",
 33         "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui", "gun", "guo", "ha", "hai", "han", "hang",
 34         "hao", "he", "hei", "hen", "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang", "hui", "hun", "huo", "ji", "jia", "jian",
 35         "jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", "kan", "kang", "kao", "ke", "ken",
 36         "keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", "lan", "lang", "lao", "le", "lei",
 37         "leng", "li", "lia", "lian", "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", "lv", "luan", "lue", "lun", "luo",
 38         "ma", "mai", "man", "mang", "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", "min", "ming", "miu", "mo", "mou", "mu",
 39         "na", "nai", "nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang", "niao", "nie", "nin", "ning", "niu", "nong",
 40         "nu", "nv", "nuan", "nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao", "pie",
 41         "pin", "ping", "po", "pu", "qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que", "qun",
 42         "ran", "rang", "rao", "re", "ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang",
 43         "sao", "se", "sen", "seng", "sha", "shai", "shan", "shang", "shao", "she", "shen", "sheng", "shi", "shou", "shu", "shua",
 44         "shuai", "shuan", "shuang", "shui", "shun", "shuo", "si", "song", "sou", "su", "suan", "sui", "sun", "suo", "ta", "tai",
 45         "tan", "tang", "tao", "te", "teng", "ti", "tian", "tiao", "tie", "ting", "tong", "tou", "tu", "tuan", "tui", "tun", "tuo",
 46         "wa", "wai", "wan", "wang", "wei", "wen", "weng", "wo", "wu", "xi", "xia", "xian", "xiang", "xiao", "xie", "xin", "xing",
 47         "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yang", "yao", "ye", "yi", "yin", "ying", "yo", "yong", "you",
 48         "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao", "ze", "zei", "zen", "zeng", "zha", "zhai", "zhan", "zhang",
 49         "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu", "zhua", "zhuai", "zhuan", "zhuang", "zhui", "zhun", "zhuo",
 50         "zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo" };
 51 
 52     int chrasc = 0;
 53 
 54     char* pcReturnString = NULL;
 55 
 56     //unicode转换为ANSI
 57     int length = WideCharToMultiByte(CP_ACP, 0, chrstr, -1, 0, 0, 0, 0);
 58     char* nowchar = new char[length + 1];
 59     memset(nowchar, 0, length + 1);
 60     WideCharToMultiByte(CP_ACP, 0, chrstr, -1, nowchar, length, 0, 0);
 61 
 62     //转换ANSI,字符部分不变,汉字转换成相应的拼音
 63     char *returnstr = new char[6 * length + 1];
 64     memset(returnstr, 0, 6 * length + 1);
 65 
 66     int offset = 0;
 67     for (int j = 0; j < length;) // 循环处理字节数组
 68     {
 69         if (nowchar[j] >= 0 && nowchar[j] < 128) // 非汉字处理
 70         {
 71             returnstr[offset] = nowchar[j];
 72             offset++;
 73             j++;
 74             continue;
 75         }
 76 
 77         // 汉字处理
 78         chrasc = nowchar[j] * 256 + nowchar[j + 1] + 256;
 79 
 80         if (chrasc > 0 && chrasc < 160)
 81         {
 82             returnstr[offset] = nowchar[j];
 83             offset++;
 84             j++;
 85         }
 86         else
 87         {
 88             for (int i = (sizeof(pyvalue) / sizeof(pyvalue[0]) - 1); i >= 0; i--)
 89             {
 90                 if (pyvalue[i] <= chrasc)
 91                 {
 92                     strcpy(returnstr + offset, pystr[i]);
 93 
 94                     offset += strlen(pystr[i]);
 95                     break;
 96                 }
 97             }
 98             j += 2;
 99         }
100     }
101     if (strlen(returnstr) > 0)
102     {
103         pcReturnString = new char[strlen(returnstr) + 1];
104         memset(pcReturnString, 0, strlen(returnstr) + 1);
105         strcpy(pcReturnString, returnstr);
106     }
107     delete[]returnstr;
108     delete[]nowchar;
109 
110     return pcReturnString;
111 
112 }

 

 4.关于字库的处理。(自行百度,效果更好)

 1 void GetHZpinyin(unsigned int hz, std::vectorstring>& pinyinArr)
 2 {
 3     TCHAR ch = (TCHAR)hz;
 4     if (m_hz2PyObj == NULL)
 5     {
 6         m_hz2PyObj = new HZ2Pinyin;
 7         CString strAppPath = util::getAppPath();
 8         CString strDataFile = _T("hzpinyin.dat");
 9         std::string strFile = util::cStringToString(strAppPath + strDataFile);
10         m_hz2PyObj->LoadData(strFile);
11     }
12 
13     m_hz2PyObj->ObtainPinyin(hz, pinyinArr);
14 }
void ObtainPinyin(unsigned int word, std::vectorstring>& pinyinArr)
{
    if (m_wordMap.empty())
    {
        return;
    }
    std::pair iterRange = m_wordMap.equal_range(word);
    HZ2PINYINMAP::iterator pos = iterRange.first;
    HZ2PINYINMAP::iterator posEnd = iterRange.second;
    for (; pos != posEnd; ++pos)
    {
        pinyinArr.push_back(pos->second);
    }
}

 

转载于:https://www.cnblogs.com/lhwblog/p/6547139.html

你可能感兴趣的:(C++中获取汉字拼音首字缩写/全拼及生僻字的处理)