拼音搜索java_利用java程序将中文转换成汉语拼音(搜索引擎可用),pinyin4j.jar.......

该博客介绍了一个Java程序,使用pinyin4j库将中文文本转换为汉语拼音,适用于拼音搜索。程序读取源文件,检测并转换其中的中文字符串,然后将转换后的拼音写入目标文件。
摘要由CSDN通过智能技术生成

package com.change;

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:/workplace/source/test.txt";

private static final String

WRITE_FILE="E:/workplace/source/test1.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();

}

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

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();

}

}

注释:需导入的包为pinyin4j.jar....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值