Java realizes the method of converting Chinese characters into Hanyu Pinyin _java

Source: Internet
Author: User
Tags lowercase

This article describes the Java implementation of the transformation of Chinese characters into Hanyu Pinyin method. Share to everyone for your reference, specific as follows:

On the internet, accidentally saw a very interesting gadget, named Pinyin4j, can convert Chinese characters to Hanyu Pinyin, use his words and then with the Lucene, Chinese participle can make similar to google that input Hanyu Pinyin for full-text search function. The implementation code is as follows

Package pinyin4j;
Import Net.sourceforge.pinyin4j.PinyinHelper;
Import Net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
Import Net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
Import Net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
Import Net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
Import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; public class Pinyin4jtest {public static void main (string argsp[]) {try {string output = Pinyin4jtest.cntop
      Inyin ("You and Hello", null);
    SYSTEM.OUT.PRINTLN (output);
    catch (Badhanyupinyinoutputformatcombination e) {//TODO auto-generated catch block E.printstacktrace (); }/** * @parm INPUTCN input Chinese String * @parm seg Output Pinyin Separator * * Hanyupinyinoutputformat provides several output modes * Han Yupinyincasetype: Set the result of the input is uppercase or lowercase English lowercase: lowercase uppercase: Uppercase * Hanyupinyintonetype: output indicates pitch and accent With_tone_number: Mark tones like YE1 1-4 for 1-4 * without_tone: no diacritics hanyupinyinvchArtype: What phonetic code to use for output/public static string Cntopinyin (string inputcn, String seg) throws Badhanyupinyinoutputf
    ormatcombination {char[] Inputarray = Inputcn.tochararray ();
    if (seg = = null) SEG = "";
    Hanyupinyinoutputformat format = new Hanyupinyinoutputformat ();
    Format.setcasetype (hanyupinyincasetype.lowercase);
    Format.settonetype (Hanyupinyintonetype.without_tone);
    Format.setvchartype (HANYUPINYINVCHARTYPE.WITH_V);
    String output = "";
    string[] temp = new STRING[10]; for (int i = 0; i < inputarray.length i++) {temp = Pinyinhelper.tohanyupinyinstringarray (inputarray[i], format)
      ; If the input of Chinese characters is pronunciation will be the different pronunciations in temp[], if not pronunciation only temp[0] in the value for (int j = 0; J < Temp.length; j +) {output + = tem
      P[J] + seg;
  } return output;

 }
}

I hope this article will help you with your Java programming.

Related Article

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.