今天想测试一下“庖丁”分词的效果,编写了一个测试小程序,从文件中读入文本,并将分词结果显示到控制台。
环境平台:Win7+eclipse
过程如下:
1.编辑paoding-analysis.jar中的paoding-dic-home.properties文件,去掉“#paoding.dic.home=dic”前面的#号,并将等号后面的dic改为dic文件夹在你本地存放的具体路劲,如:F://workspace//data//dic
(注:编辑paoding-analysis.jar中的文件,可先用WinRAR将paoding-analysis.jar打开,再用记事本或写字板直接编辑paoding-dic-home.properties文件后保存即可)
2. 将paoding-analysis.jar、commons-logging.jar、lucene-analyzers-2.2.0.jar和lucene-core-2.2.0.jar四个包导入工程:
①在工程下新建lib文件夹,将这四个包复制进来;
②右键单击工程->Properties->Java Build Path,在右侧选中第三个标签Libraries,点击Add JARs…,导入上述四个包;
③再选择第四个标签Order and Export,勾选上这四个包,点击OK按钮。
3. 创建一个主类,编写测试小程序,如下:
import Java.io.*;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import net.paoding.analysis.analyzer.PaodingAnalyzer;
public class FenciTest {
public static void main(String[] args)
{
Analyzer analyzer = new PaodingAnalyzer();
String docText = null;
File file = new File("F://Work//workSpace//FenciTest//data//test1.txt");
docText = readText(file);
TokenStream tokenStream = analyzer.tokenStream(docText, new StringReader(docText));
try {
Token t;
//System.out.println(docText);
while ((t = tokenStream.next()) != null)
{
System.out.println(t);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static String readText(File file) {
String text = null;
try
{
InputStreamReader read1 = new InputStreamReader(new FileInputStream(file), "GBK");
BufferedReader br1 = new BufferedReader(read1);
StringBuffer buff1 = new StringBuffer();
while((text = br1.readLine()) != null)
{
buff1.append(text + "/r/n");
}
br1.close();
text = buff1.toString();
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
return text;
}
}
如果您愿意花几块钱请我喝杯茶的话,可以用手机扫描下方的二维码,通过 支付宝 捐赠。我会努力写出更好的文章。
(捐赠不显示捐赠者的个人信息,如需要,请注明您的联系方式)
Thank you for your kindly donation!!