使用PinYin4j.jar将汉字转换为拼音使用实例

来源:互联网 发布:算法的正确性 编辑:程序博客网 时间:2024/05/16 19:24
  1. package com.wlh.lucene.test2;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.File;  
  6. import java.io.FileReader;  
  7. import java.io.FileWriter;  
  8. import java.io.IOException;  
  9. import java.util.regex.Matcher;  
  10. import java.util.regex.Pattern;  
  11.   
  12. import net.sourceforge.pinyin4j.PinyinHelper;  
  13. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;  
  14. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;  
  15. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;  
  16. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;  
  17.   
  18. public class Test {   
  19.     //源文件与目标文件的全路径名  
  20.     private static final String READ_FILE="E:/workspace65/paoding-analysis_test/src/com/wlh/lucene/test2/txt.txt";  
  21.     private static final String WRITE_FILE="E:/workspace65/paoding-analysis_test/src/com/wlh/lucene/test2/txt1.txt";  
  22.     private static HanyuPinyinOutputFormat spellFormat = new HanyuPinyinOutputFormat();    
  23.     private static BufferedWriter writer = null;  
  24.     private static BufferedReader reader = null;  
  25.     //初始化信息  
  26.     public static void init() throws IOException{  
  27.         writer = new BufferedWriter(new FileWriter(new File(WRITE_FILE),false));  
  28.         reader = new BufferedReader(new FileReader(new File(READ_FILE)));    
  29.         spellFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);    
  30.         spellFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);    
  31.         spellFormat.setVCharType(HanyuPinyinVCharType.WITH_V);   
  32.     }  
  33.     // 判断字符串是否包含有中文   
  34.     public static boolean isChinese(String str) {    
  35.         String regex = "[\\u4e00-\\u9fa5]";    
  36.         Pattern pattern = Pattern.compile(regex);    
  37.         Matcher matcher = pattern.matcher(str);    
  38.         return matcher.find();    
  39.     }    
  40.     //使用PinYin4j.jar将汉字转换为拼音  
  41.     public static String chineneToSpell(String chineseStr){  
  42.         return PinyinHelper.toHanyuPinyinString(chineseStr , spellFormat ,"");  
  43.     }  
  44.     //将转换后的字符串写入目标文件  
  45.     public static void writeToFile(String spellStr) throws IOException{  
  46.         writer.write(spellStr);  
  47.     }  
  48.     //从源文件读取按行数据  
  49.     public static void readFromFile() throws IOException{  
  50.         String line = null;  
  51.          while ((line = reader.readLine()) != null) {    
  52.              line = line.trim();    
  53.              //是中文  
  54.              if(isChinese(line)){  
  55.                  line = chineneToSpell(line);  
  56.              }  
  57.              writeToFile(line + "\n");  
  58.          }    
  59.     }  
  60.     //关闭文件流  
  61.     public static void destory() throws IOException{  
  62.         reader.close();  
  63.         writer.close();  
  64.     }  
  65.     public static void main(String[] args) throws IOException {  
  66.         init();  
  67.         readFromFile();  
  68.         destory();  
  69.     }  


原创粉丝点击