Introduction to the Chinese character to PinYin open-source toolkit jpinyin

Source: Internet
Author: User

Recently, I want to implement a function to obtain pinyin corresponding to words based on words. I found jpinyin, an open-source toolkit, which is very powerful and fully meets my needs, let's give a brief introduction to it, hoping to help friends who need it.


I. project introduction:

Jpinyin is an open-source Java class library for converting Chinese characters to pinyin. Based on the functions of pinyin4j, some improvements have been made.


[Main features of jpinyin]
1. accurate and complete font library;
Unicode encoding can be used to convert all Chinese characters except 46 Chinese characters (excluding standard Chinese characters) in the range of 4e00-9fa5 and 3007 (20903;
2. Fast PinYin conversion;
It was tested that the conversion of Unicode encoding from 20902 Chinese characters in the range of 4e00-9fa5 took about 100 milliseconds.
3. Support for output of multiple pinyin formats;
Jpinyin supports multiple pinyin output formats: output formats with phonetic symbols, without phonetic symbols, numbers indicating phonetic symbols, and first letter of Pinyin;
4. Recognition of common polyphonic words;
Jpinyin supports the recognition of common polyphonic words, including phrases, idioms, and place names;
5. Simplified and Traditional Chinese Conversion

Project address: jpinyin is a Java open-source class library for converting Chinese characters to pinyin.


II. Implementation principle:

By reading the source code, we can find that the implementation principle of jpinyin is to store new words, phrases, Pinyin and simplified Chinese characters in the database, then, you can use the code to operate the database to convert Chinese characters/phrases into Pinyin and simplified Chinese characters. The database is encrypted and cannot be expanded. However, this tool is already quite complete, there is no need to expand the database on your own. After testing, no conversion error is found.



Iii. Core methods:

There are four classes in jpinyin:

Chinesehelper. Java simplified and Traditional Chinese conversion class

Pinypinyin. Java pinyin format class

Pinyinhelper. Java Chinese character to PinYin class

Pinyinresource. Java resource file loading class


This article only introduces the conversion of Chinese characters to PinYin pinyinhelper, and the simplified and traditional conversion of chinesehelper is not described. The public interfaces of pinyinhelper are as follows:

/**

* Convert a single Chinese character to PinYin in the corresponding format
* @ Param C: The Chinese character to be converted to PinYin
* @ Param pinyinformat pinyin format: with_tone_number -- numbers represent tones, without_tone -- without tones, with_tone_mark -- with tones
* @ Return pinyin for Chinese Characters
*/
Public static string [] converttopinyinarray (char C, piny?piny=)


/**
* Convert a single Chinese character into a pinyin with a tone format
* @ Param C: The Chinese character to be converted to PinYin
* @ Return: Pinyin of the string
*/
Public static string [] converttopinyinarray (char C)


/**
* Convert the string to PinYin in the corresponding format
* @ Param STR the string to be converted
* @ Param separator: pinyin Separator
* @ Param pinyinformat pinyin format: with_tone_number -- numbers represent tones, without_tone -- without tones, with_tone_mark -- with tones
* @ Return: Pinyin of the string
*/
Public static string converttopinyinstring (string STR, string separator, piny?piny=)


/**
* Convert a string to a pinyin string in tone format.
* @ Param STR the string to be converted
* @ Param separator: pinyin Separator
* @ Return: pinyin with tone after conversion
*/
Public static string converttopinyinstring (string STR, string separator)


/**
* Determines whether a Chinese character is a multi-tone character.
* @ Param C Chinese Characters
* @ Return indicates the result. If it is a Chinese character, true is returned; otherwise, false is returned.
*/
Public static Boolean hasmultipinyin (char C)


/**
* Obtain the first letter of the pinyin string
* @ Param STR the string to be converted
* @ Return refers to the first letter of the pinyin alphabet.
*/
Public static string getpolicpinyin (string Str)


Iv. Demo

The following is a demo program I wrote to call the interfaces for converting Chinese characters to pinyin. It is very easy to use.

Public class jpinyindemoactivity extends baseactivity {@ overridepublic void setcontentview () {setcontentview (R. layout. activity_jpinyin_demo_layout);} @ overridepublic void findviews () {mwordsedittxt = (edittext) findviewbyid (R. id. wordsedittextid); mresulttxt = (textview) findviewbyid (R. id. resulttxtid); inputlenlimit. lengthfilter (this, mwordsedittxt) ;}@ overridepublic void getdata () {}@ overridepublic Void showcontent () {testjpinyin ();} public void onclick (view v) {Switch (v. GETID () {case R. id. topinyinbtnid: {clickwordstopinyin ();} break; default: {} break;} private string wordstopinyin (string words) {If (textutils. isempty (words) {return NULL;} string pinyin = pinyinhelper. converttopinyinstring (words, ""); Return pinyin;} private void clickwordstopinyin () {string pinyin = wordstopinyin (mwor Dsedittxt. gettext (). tostring (); If (! Textutils. isempty (pinyin) {mresulttxt. settext (pinyin) ;}} private void testjpinyin () {string words = "and fortune"; Boolean hasmultipinyin = false; string pinyin = NULL; string [] pinyins = NULL; final string separator = ""; // héq %sh %ng cáipinyin = pinyinhelper. converttopinyinstring (words, separator); println (pinyin); // with_tone_number -- numbers represent tones, without_tone -- without tones, with_tone_mark -- tone // héq %sh %ng cáipinyin = pinyinhelper. converttopinyinstring (words, separator, pinyator. with_tone_mark); println (pinyin); // he2 qi4 sheng1 cai2pinyin = pinyinhelper. converttopinyinstring (words, separator, pinyator. with_tone_number); println (pinyin); // He qi sheng caipinyin = pinyinhelper. converttopinyinstring (words, separator, pinyator. without_tone); println (pinyin); // héhè huóhu ~húpinyins = pinyinhelper. converttopinyinarray (words. tochararray () [0]); println (pinyins); // Hé hè huóhu ~húpinyins = pinyinhelper. converttopinyinarray (words. tochararray () [0], pinytasks. with_tone_mark); println (pinyins); // hqscpinyin = pinyinhelper. getshortpinyin (words); println (pinyin); // truehasmultipinyin = pinyinhelper. hasmultipinyin (words. tochararray () [0]); println (hasmultipinyin);} private void println (string result) {system. out. println ("result =" + result);} private void println (string [] Results) {for (string result: Results) {system. out. println ("result =" + Result + "") ;}} private void println (Boolean hasmultipinyin) {system. out. println ("result =" + hasmultipinyin);} private textview mresulttxt = NULL; private edittext mwordsedittxt = NULL ;}

Example: converting Chinese characters to PinYin jpinyin


Introduction to the Chinese character to PinYin open-source toolkit jpinyin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.