使用PinYin4j.jar将汉字转换为拼音使用实例
来源:互联网 发布:算法的正确性 编辑:程序博客网 时间:2024/05/16 19:24
- package com.wlh.lucene.test2;
-
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- 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;
-
- public class Test {
-
- private static final String READ_FILE="E:/workspace65/paoding-analysis_test/src/com/wlh/lucene/test2/txt.txt";
- private static final String WRITE_FILE="E:/workspace65/paoding-analysis_test/src/com/wlh/lucene/test2/txt1.txt";
- private static HanyuPinyinOutputFormat spellFormat = new HanyuPinyinOutputFormat();
- private static BufferedWriter writer = null;
- private static BufferedReader reader = null;
-
- public static void init() throws IOException{
- writer = new BufferedWriter(new FileWriter(new File(WRITE_FILE),false));
- reader = new BufferedReader(new FileReader(new File(READ_FILE)));
- spellFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
- spellFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
- spellFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
- }
-
- public static boolean isChinese(String str) {
- String regex = "[\\u4e00-\\u9fa5]";
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(str);
- return matcher.find();
- }
-
- public static String chineneToSpell(String chineseStr){
- return PinyinHelper.toHanyuPinyinString(chineseStr , spellFormat ,"");
- }
-
- public static void writeToFile(String spellStr) throws IOException{
- writer.write(spellStr);
- }
-
- public static void readFromFile() throws IOException{
- String line = null;
- while ((line = reader.readLine()) != null) {
- line = line.trim();
-
- if(isChinese(line)){
- line = chineneToSpell(line);
- }
- writeToFile(line + "\n");
- }
- }
-
- public static void destory() throws IOException{
- reader.close();
- writer.close();
- }
- public static void main(String[] args) throws IOException {
- init();
- readFromFile();
- destory();
- }
- }