The open-source conversion program on the network is valuable for converting table resources in Chinese pinyin.
Relatively small. In addition, the code table provided on the network may not cover all the polyphonic words,
Uncommon words, so the program written based on such a code table has some limitations.
This article provides a complete design thought for converting Chinese characters into pinyin without tones
Path, all code, and a usable Chinese character Pinyin Conversion table to a certain extent
Void StringManipulation: FormatPinYinMap
(Map <wstring, vector <string> & hptable)
{
Ifstream ifile ("mycodebook.txt ");
String line;
While (getline (ifile, line ))
{
If (! Line. empty ())
{Wstring wsResult = String2Wstring (line );
Vector <wstring> goodWordstemp;
Boost: split
(GoodWordstemp, wsResult, boost: is_any_of (""));
Wstring mykey = goodWordstemp [0];
Vector <string> myval;
For (vector <wstring >:: iterator
It = goodWordstemp. begin () + 1; it! = GoodWordstemp. end (); it ++)
{
Myval. push_back
(Wstring2String (* it ));
}
If (! Hptable. count (mykey ))
{
Hptable [mykey] = myval;
}
}
}
}
Code for converting a single Chinese character into pinyin
Vector <string> StringManipulation: HZ2Py (wstring
Character, map <wstring, vector <string> & hptable)
{
Vector <string> candidates;
If (hptable. count (character ))
{
Candidates = hptable [character];
}
Else
{
Candidates. push_back ("[A-Z] + ");
}
Return candidates;
}
Returns a Chinese character string.
/*************************************** ******************
***************/
/* Obtain the pinyin string of a Chinese character string
*/
/*************************************** ******************
***************/
Vector <string> StringManipulation: Han2Py (string
Shan, map <wstring, vector <string> & hptable)
{
Vector <string> result;
GraphRepresentation gr;
Gr. Vertex [0] = "@";
Int mycount = 0; // whether to add "regular Edge"
Int prevsize = 0; // The pronunciation of the previous Chinese character
Int laslabel = 0; // The maximum value of the last node number
If (! Shan. empty ())
{
Wstring whan = String2Wstring (shan );
For (wstring: iterator it = whan. begin
(); It! = Whan. end (); it ++)
{
Wstring tmp;
Tmp. assign (1, * it );
Vector <string> tmpcandidates = HZ2Py
(Tmp, hptable );
If (mycount = 0)
{
For (int
I = 0; I <tmpcandidates. size (); I ++)
{
Gr. Vertex
[I + laslabel + 1] = tmpcandidates [I];
Gr. GraphR
[Make_pair (0, I + laslabel + 1)] = "&";
}
Prevsize = tmpcandidates. size ();
Laslabel = tmpcandidates. size ();
}
Else
{
For (int
I = 0; I <tmpcandidates. size (); I ++)
{
Gr. Vertex
[I + laslabel + 1] = tmpcandidates [I];
For (int
J = laslabel; j> laslabel-prevsize; j --)
{
Gr. GraphR
[Make_pair (j, I + laslabel + 1)] = ".*? ";
}
}
Laslabel + = tmpcandidates. size ();
Prevsize = tmpcandidates. size ();
}
Mycount ++;
}
Gr. Vertex [laslabel + 1] = "@";
For (int j = laslabel; j> laslabel-prevsize; j
--)
{
Gr. GraphR [make_pair
(J, laslabel + 1)] = "&";
}
Vector <int> paths;
Gr. GetPaths (paths, 0, laslabel + 1 );
For (vector <int >:: iterator
It = paths. begin (); it! = Paths. end (); it ++)
{
Deque <int> tmpque;
String singlecandidate;
For (vector <int >:: reverse_iterator
Rit = it-> rbegin (); rit! = It-> rend (); rit ++)
{
Tmpque. push_back (* rit );
If (tmpque. size () = 2)
{
Int
Tmp1 = tmpque. front ();
Tmpque. pop_front
();
Singlecandidate + = gr. Vertex [tmp1];
Int
Tmp2 = tmpque. front ();
String
Edge = gr. GraphR [make_pair (tmp1, tmp2)];
Singlecandidate + = edge;
}
}
TrimString (singlecandidate ,"@");
TrimString (singlecandidate ,"&");
Result. push_back (singlecandidate );
}
}
Return result;
}