汉字转换成拼音的工具----pinyin4j

 

   今天在无意中发现了pinyin4j这个强大的工具,它能帮助我们完成将汉字转换成拼音的工作,这给我们的开发带来了巨大的便利,意识到这一点,我果断的学习了。在开始记录我的学习成果前,我必须先感谢pinyin4j的作者Li Min
  (作者详细信息不详),感谢你为开发者提供了这个工具。

    先来看看pinyin4j有什么功能吧。

 Pinyin4j是sourceforge.net上的一个开源项目,支持同一汉字有多个发音,还支持拼音的格式化输出,比如第几声之类的,同时支持简体中文、繁体中文转换为拼音。下面是其官方网址,其中提供了下载:http://pinyin4j.sourceforge.net/
  

     下面就是我的学习成果演示了,可能存在诸多错误,希望大家能不吝指正,帮助我进步。

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
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;
import net.sourceforge.pinyin4j.*;
                                                                                                                                                                                                                 
public class Chinese2Pinyin {
                                                                                                                                                                                                                 
     /**
      * @param args
      * @throws BadHanyuPinyinOutputFormatCombination
      */
     public static void main(String[] args)
             throws BadHanyuPinyinOutputFormatCombination {
         // TODO Auto-generated method stub
                                                                                                                                                                                                                 
         Chinese chinese = new Chinese();
         String sentence = "自己选择的路,跪着也要走完" ;
         char word = '中' ;
                                                                                                                                                                                                                 
         // 不使用格式化
         String pinyins[] = chinese.getSingleWord(word);
         System.out.println( "不使用格式化的单字测试:" );
         for (String pinyin : pinyins) {
             System.out.println(pinyin);
         }
                                                                                                                                                                                                                 
         System.out.println( "不使用格式化的语句测试:" );
         String result = chinese.getSentence(sentence);
         System.out.println(result);
                                                                                                                                                                                                                 
         // 使用格式化
         HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
         format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
         format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
         format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
                                                                                                                                                                                                                 
         System.out.println( "使用格式化的单字测试:" );
         String pinyins2[] = chinese.getSingleWord(word, format);
         for (String pinyin : pinyins2) {
             System.out.println(pinyin);
         }
                                                                                                                                                                                                                 
         format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
         System.out.println( "使用格式化的语句测试:" );
         String result2 = chinese.getSentence(sentence, format);
         System.out.println(result2);
     }
                                                                                                                                                                                                                 
}
                                                                                                                                                                                                                 
class Chinese {
     public Chinese() {
     }
                                                                                                                                                                                                                 
     // 测试单字,不使用格式化方式
     public String[] getSingleWord( char word) {
         return PinyinHelper.toHanyuPinyinStringArray(word);
     }
                                                                                                                                                                                                                 
     // 测试单字,使用格式化方式
     public String[] getSingleWord( char word, HanyuPinyinOutputFormat format)
             throws BadHanyuPinyinOutputFormatCombination {
         return PinyinHelper.toHanyuPinyinStringArray(word, format);
     }
                                                                                                                                                                                                                 
     // 测试语句,不使用格式方式
     public String getSentence(String sentence) {
         StringBuilder sb = new StringBuilder();
         for ( int i = 0 , len = sentence.length(); i < len; i++) {
             char ch = sentence.charAt(i);
             String[] temp = getSingleWord(ch);
             if (temp == null ) {
                 sb.append(ch + ' ' );
             } else {
                 sb.append(temp[ 0 ] + ' ' );
             }
         }
         return sb.toString();
     }
                                                                                                                                                                                                                 
     // 测试语句,使用格式化方式
     public String getSentence(String sentence, HanyuPinyinOutputFormat format)
             throws BadHanyuPinyinOutputFormatCombination {
         StringBuilder sb = new StringBuilder();
         for ( int i = 0 , len = sentence.length(); i < len; i++) {
             char ch = sentence.charAt(i);
             String[] temp = getSingleWord(ch, format);
             if (temp == null ) {
                 sb.append(ch + ' ' );
             } else {
                 sb.append(temp[ 0 ] + ' ' );
             }
         }
         return sb.toString();
     }
}

 

    测试结果如下:

    

    OK,上面是我做的简单的测试程序,在做程序的时候我想到一个问题,如何来判断文字在语句中的读音该是怎样的呢?我认为这是这个工具需要再添加的功能,如果有了这个功能,那么我们就可以很方便的将汉语文章用拼音注解,这也就为小孩子提供了更多的阅读物了。

 

 

package com.bq.action;

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 PinyinMain {
	public static void main(String[] args) {
		System.out.println(stringToPinYin("大修包"));
	}

	public static String charToPinYin(char c) {
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE); // 设置大小写
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); // 不输出音调
		format.setVCharType(HanyuPinyinVCharType.WITH_V); // 拼音中的u输出为v 例如lv

		try {
			// 返回汉字的拼音,如果为多音字则返回所有读音
			String[] result = PinyinHelper.toHanyuPinyinStringArray(c, format);
			if (result == null) {
				return null; // 如果传入的不是汉字,例如A,则返回数组为null
			} else {
				return result[0]; // 返回汉字的第一个读音
			}
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String stringToPinYin(String str) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (charToPinYin(c) == null) {
				sb.append(c);
			} else {
				sb.append(charToPinYin(c));
			}
		}
		return sb.toString();

	}
}

 

你可能感兴趣的:(java)