asp.net 漢字轉換成無聲調標註的拼音

來源:互聯網
上載者:User

由於漢字拼音轉換表資源比較寶貴的緣故,網路上開源的轉換程式

比較少。另外,網路上給出的碼錶,可能不能覆蓋全部的多音字,

生僻字,所以基於此類碼錶寫成的程式,也就有一定的局限性。
  本文給出一份完畢的,將漢字轉換成無聲調標註的拼音的設計思

路、全部代碼並且給出一份在一定程度上可用的漢字拼音轉換表
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;
   }


  }

 

 

 }

}


單個漢字轉換拼音的代碼

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;

}

獲得一漢字字串的拼音串


/*********************************************************

***************/
/*獲得一漢字字串的拼音串                                

                                    */
/*********************************************************

***************/
vector<string> StringManipulation::Han2Py(string

shan,map<wstring,vector<string>>&hptable)
{
 vector<string>result;
 GraphRepresentation gr;
 gr.Vertex[0]="@";
 int mycount=0;//是否添加“正則邊”
 int prevsize=0;//上一個漢字有幾個讀音
 int laslabel=0;//上一次節點編號的最大值
 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<vector<int>>paths;
  gr.GetPaths(paths,0,laslabel+1);
  for (vector<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;
 
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.