Java get man pinyin

Source: Internet
Author: User

1. Look at the http://pinyin4j.sourceforge.net here/

2. Download pinyin4j-1.x.x.jar

3. Check the demo.

Import net. sourceforge. pinyin4j. pinyinHelper; <br/> import net. sourceforge. pinyin4j. format. hanyuPinyinCaseType; <br/> import net. sourceforge. pinyin4j. format. hanyuPinyinOutputFormat; <br/> import net. sourceforge. pinyin4j. format. hanyuPinyinToneType; <br/> import net. sourceforge. pinyin4j. format. hanyuPinyinVCharType; <br/>/** <br/> * A demostration to show how does the pinyin4j work <br/> * @ author Li Min (xmlerlimin@gmail.com) <br/> */<br/> public class MainDemo {<br/>/** <br/> * @ param args <br/> */ <br/> public static void main (String [] args) {<br/> // deal with Chinese sentence <br/> {<br/> HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat (); <br/> // defautlt output format <br/> System. out. println ("defautlt output format"); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("My Chinese heart", outputFormat, ""); <br/> System. out. println ("***"); <br/> // with seperater <br/> System. out. println ("with seperater"); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("My Chinese heart", outputFormat, ""); <br/> System. out. println ("***"); <br/> // output uppercase <br/> System. out. println ("output uppercase"); <br/> outputFormat. setCaseType (HanyuPinyinCaseType. UPPERCASE); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("My Chinese heart", outputFormat, ""); <br/> System. out. println ("***"); <br/> // without tone numbers or tone marks <br/> System. out. println ("without tone numbers or tone marks"); <br/> outputFormat. restoreDefault (); <br/> outputFormat. setToneType (HanyuPinyinToneType. WITHOUT_TONE); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("My Chinese heart", outputFormat, ""); <br/> System. out. println ("***"); <br/> // use u: as an alternative of ü' <br/> System. out. println ("use u: as an alternative of ü'"); <br/> outputFormat. restoreDefault (); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("", outputFormat, ""); <br/> System. out. println ("***"); <br/> // use v as an alternative of ü' <br/> System. out. println ("use v as an alternative of ü'"); <br/> outputFormat. setVCharType (HanyuPinyinVCharType. WITH_V); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("", outputFormat, ""); <br/> System. out. println ("***"); <br/> // directly output ü' in Unicode form <br/> System. out. println ("directly output ü' in Unicode form"); <br/> outputFormat. setVCharType (HanyuPinyinVCharType. WITH_U_UNICODE); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("", outputFormat, ""); <br/> System. out. println ("***"); <br/> // deal with combination of Chinese and non-Chinese <br/> System. out. println ("deal with combination of Chinese and non-Chinese"); <br/> outputFormat. restoreDefault (); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("Dow Jones index rose 5%", outputFormat, ""); <br/> System. out. println ("***"); <br/>}< br/> // deal with Chinese character <br/>{< br/> HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat (); <br/>{< br/> // single pronounciation <br/> String [] pinyinArray = PinyinHelper. toHanyuPinyinStringArray ('my', outputFormat); <br/> System. out. println ("***"); <br/> for (int I = 0; I <pinyinArray. length; I ++) {<br/> StringBuffer stringbuf = new StringBuffer (); <br/> stringbuf. append ("pinyin"); <br/> stringbuf. append (I); <br/> stringbuf. append (":"); <br/> stringbuf. append (pinyinArray [I]); <br/> System. out. println (stringbuf. toString (); <br/>}< br/> System. out. println ("***"); <br/>}< br/>{< br/> // multiple pronounciations <br/> String [] pinyinArray = PinyinHelper. tohanyupinypolicingarray ('中', outputFormat); <br/> System. out. println ("***"); <br/> for (int I = 0; I <pinyinArray. length; I ++) {<br/> StringBuffer stringbuf = new StringBuffer (); <br/> stringbuf. append ("pinyin"); <br/> stringbuf. append (I); <br/> stringbuf. append (":"); <br/> stringbuf. append (pinyinArray [I]); <br/> System. out. println (stringbuf. toString (); <br/>}< br/> System. out. println ("***"); <br/>}< br/>}

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.