简体   繁体   中英

How to convert a pinyin string to chinese in C#

I have a touch screen keyboard in my WPF application and I would like to allow the users to write in chinese.

I saw that there is an IME in Windows that allows to write in chinese with Pinyin. It works great but I'd like to customize it for my WPF application. (Especially the candidate list). I didn't find any documentation for this.

The idea will be that the user write in Pinyin with the virtual keyboard and there will be a list of choice with chinese ideograms next to the textbox.

Do you have any advice to achieve it? Maybe there is a library (not from Microsoft) that can make it and in this case I won't use the IME from MS?

Not sure if there is any OS (Open Source) packages available. However, in theory, it is not too hard to build this kind of library. In Chinese, there are about 1300 single sounds: initial + final + tones. Each sound have group of Chinese characters, various number from 1 to 130 characters.

You may define an array of all Pinyin sounds:

string[] pinyins = new string[] {
  "a:c1c2c3...",      // pinyin 1 a: character1 character2...
  ...
  "zuo:z1z2z3z4z5..." // last pinyin (1300) zuo: character character...
};

The above array is a base for your mapping Pinyin to Chinese(Chinese characters and Pinyin tones are unicode strings). Then for each Pinyin input sound, a list of characters is obtained by a function like this:

string getCharacters(string aPinyin) {
   string characters = null;
   foreach(string item in pinyins) {
      string[] temp = item.split(':');
      if (temp[0].Equals(aPinyin)) {
          charaters = temp[1];
          break;
      }
   }
   return characters;
}

I wrote a JavaScipt long time ago, where I defined the relationship between Pinyin and Chinese characters. In my blog: Get Pinyin From Chinese Characters , the script can be found by view the source codes or Inspect Element in context menu. In my blog, the script is used to convert Chinese to Pinyin, but the relationship can be used as a reference.

在此处输入图片说明

To add smart Pinyin feature--displaying a list of words for Pinyin, this can be done by defining all the commonly used words in the similar pattern: pinyin:words.

http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=7D1DF9CE-4AEE-467F-996E-BEC826C5DAA2

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=15251

Microsoft in fact has good components/libraries for that, but they are hidden here in the Visual Studio International Feature Pack.

Note that You need 1.0 SR 1 which provides the basic libraries, while 2.0 adds many WinForms or WPF controls.

(Updated on Oct 26, 2017. Many guys have published NuGet packages on NuGet.org based on Microsoft's code, so you might also check those packages out.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM