자바 중국어 병 음 도구 클래스

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105

       
       
       
       
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 ;
/**
*
* @author qin
*
*/
public class PinyinUtil {
/**
* ,
* @param inputString
* @return
*/
public static String getPinYin ( String inputString ){
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat ();
format . setCaseType ( HanyuPinyinCaseType . LOWERCASE ); //
format . setToneType ( HanyuPinyinToneType . WITHOUT_TONE ); // api
format . setVCharType ( HanyuPinyinVCharType . WITH_V ); //
char [] input = inputString . trim (). toCharArray ();
String output = "" ;
try {
for ( int i = 0 ; i < input . length ; i ++){
if ( java . lang . Character . toString ( input [ i ]). matches ( "[\\u4E00-\\u9FA5]+" )){
String [] temp = PinyinHelper . toHanyuPinyinStringArray ( input [ i ], format );
output += temp [ 0 ];
} else {
output += input [ i ];
}
}
} catch ( Exception e ) {
e . printStackTrace ();
}
return output ;
}
/**
* ,
* @param chinese
* @return
*/
public static String getFirstSpell ( String chinese ){
StringBuffer pybf = new StringBuffer ();
char [] arr = chinese . toCharArray ();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat ();
defaultFormat . setCaseType ( HanyuPinyinCaseType . LOWERCASE );
defaultFormat . setToneType ( HanyuPinyinToneType . WITHOUT_TONE );
defaultFormat . setVCharType ( HanyuPinyinVCharType . WITH_V );
for ( int i = 0 ; i < arr . length ; i ++){
if ( arr [ i ]> 128 ){
try {
String [] temp = PinyinHelper . toHanyuPinyinStringArray ( arr [ i ], defaultFormat );
if ( temp != null ){
pybf . append ( temp [ 0 ]. charAt ( 0 ));
} else {
pybf . append ( arr [ i ]);
}
} catch ( BadHanyuPinyinOutputFormatCombination e ) {
e . printStackTrace ();
}
}
}
return pybf . toString (). replaceAll ( "\\W" , "" ). trim ();
}
/**
* ,
* @param chinese
* @return
*/
public static String getFullSpell ( String chinese ){
StringBuffer pybf = new StringBuffer ();
char [] arr = chinese . toCharArray ();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat ();
defaultFormat . setCaseType ( HanyuPinyinCaseType . LOWERCASE );
defaultFormat . setToneType ( HanyuPinyinToneType . WITHOUT_TONE );
defaultFormat . setVCharType ( HanyuPinyinVCharType . WITH_V );
try {
for ( int i = 0 ; i < arr . length ; i ++){
if ( arr [ i ]> 128 ){
pybf . append ( PinyinHelper . toHanyuPinyinStringArray ( arr [ i ], defaultFormat )[ 0 ]);
} else {
pybf . append ( arr [ i ]);
}
}
} catch ( BadHanyuPinyinOutputFormatCombination e ) {
e . printStackTrace ();
}
return pybf . toString ();
}
public static void main ( String [] args ) {
String str = " " ;
System . out . println ( PinyinUtil . getPinYin ( str ));
System . out . println ( PinyinUtil . getFirstSpell ( str ));
System . out . println ( PinyinUtil . getFullSpell ( str ));
}
}

좋은 웹페이지 즐겨찾기