比较稳定的其它的汉字转拼音的方法

这里就不在多说了。

 

核心部分:

 1 std::string HanziTable_impl2::HZsToPYsEx(const std::string &fHanzis, const bool fIsFisrt)
 2 {
 3     bool m_blnSimaple = false;
 4     bool m_blnFirstBig = true;
 5     bool m_blnAllBiG = true;
 6     bool m_LetterEnd = false;
 7 
 8     unsigned char ucHigh, ucLow;
 9     int fCode;
10     std::string strPinyin = "";
11     for (size_t i = 0; i < fHanzis.length(); i++)
12     {
13         if ((unsigned char)fHanzis[i] < 0x80)
14         {
15             strPinyin = strPinyin + fHanzis[i];
16             if (m_LetterEnd) strPinyin = strPinyin + '\'';
17             continue;
18         }
19         ucHigh = (unsigned char)fHanzis[i];
20         ucLow = (unsigned char)fHanzis[i + 1];
21         if (ucHigh < 0xa1 || ucLow < 0xa1)
22             continue;
23         else
24             fCode = (ucHigh - 0xa0) * 100 + ucLow - 0xa0;
25         std::string strRes = GetPinyinByCode(fCode);
26         if (m_blnSimaple&&strRes.length() > 0)
27         {
28             strRes = strRes.substr(0, 1);
29         }
30         if (!m_blnFirstBig)
31         {
32             //strRes.SetAt(0,strRes.GetAt(0)+32);
33             strRes[0] = strRes[0] + 32;
34         }
35         if (!m_blnAllBiG)
36         {
37             for (size_t j = 1; j < strRes.length(); j++)
38             {
39                 //strRes.SetAt(j,(strRes.GetAt(j)+32));
40                 strRes[j] = strRes[j] + 32;
41             }
42         }
43 
44         if (fIsFisrt)
45         {
46             strPinyin = strPinyin + strRes.substr(0, 1);
47         }
48         else
49         {
50             strPinyin = strPinyin + strRes;
51         }
52 
53         i++;
54         if (m_LetterEnd) strPinyin = strPinyin + '\'';
55     }
56     return strPinyin;
57 }

测试部分:

1     HanziTable_impl2 test_4;
2     std::string test_ret_1;
3     std::string test_ret_2;
4     std::string test_str_1 = "搜的法律是地方上了飞机饿死;了俄方谁巨,额浪费ijfs老地方司法是低级房间诶你们djkjfdjf";
5     test_ret_1 = test_4.HZsToPYsEx(test_str_1, true);
6     test_ret_2 = test_4.HZsToPYsEx(test_str_1, false);

 

完整源码下载,请点击这里

 

*注:这个方法和终结篇里面的方法相比不够完美,对标点符号处理不好。例如“;”在这里就翻译不过来

 

posted @ 2015-04-27 16:39  仙人球球  Views(924)  Comments(0Edit  收藏  举报