今天在网上看了一篇关于中文分词的文章,有一类是关于基于字符串匹配的中文分词,里面谈到了一个“庖丁解牛分词器”,于是本人就想尝试跑跑这个分词工具。下面是具体的使用步骤:
1. 从网上下载了一个庖丁解牛中文分词工具压缩包,放入D:\SoftwarePackage中。解压D:\SoftwarePackage中的paoding-analysis-2.0.4-beta压缩包到文件夹D:\software\paoding-analysis-2.0.4-beta中。
2. 新建java工程PaodingAnalysisTest,在工程下新建文件夹lib,将解压后文件里面的paoding-analysis.jar以及lib目录下的commons-logging.jar,lucene-analyzer-2.2.0.jar,lucene-core-2.2.0.jar四个文件拷贝到lib目录下,在这里需要修改下paoding-analysis.jar里面的内容,用解压工具打开paoding-analysis.jar,修改paoding-dic-home.properties里面的内容,将#paoding.dic.home=dic修改成paoding.dic.home=E://WorkSpace//data//dic(此处路径可根据字典存放的位置进行相应更改),保存即可。
3. 右键单击工程->Properties->Java Build Path,在右侧选中第三个标签Libraries,点击Add JARs…,导入上述四个包;再选择第四个标签Order and Export,勾选上这四个包,点击OK按钮。
4. 然后创建一个主类,编写测试小程序(此程序是从网上copy过来的)
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{
/**
* @param args
*/
public static voidmain(String[] args) {
// TODOAuto-generated method stub
Analyzeranalyzer=new PaodingAnalyzer();
StringdocText=null;
Filefile=new File("E:\\WorkSpace\\data\\FenciTest1.txt");
docText=readText(file);
TokenStreamtokenStream=analyzer.tokenStream(docText, newStringReader(docText));
try{
Tokent;
//System.out.println(docText);
while((t=tokenStream.next())!=null){
System.out.println(t);
}
}catch(IOExceptione){
e.printStackTrace();
}
}
private static StringreadText(File file) {
// TODOAuto-generated method stub
String text=null;
try{
InputStreamReaderread1=new InputStreamReader(newFileInputStream(file),"GBK");
BufferedReaderbr1=new BufferedReader(read1);
StringBufferbuff1=new StringBuffer();
while((text=br1.readLine())!=null){
buff1.append(text+"/r/n");
}
br1.close();
text=buff1.toString();
}catch(FileNotFoundExceptione){
System.out.println(e);
}catch(IOExceptione){
System.out.println(e);
}
return text;
}
}
说明:此测试程序对lucene2.2适用,但对lucene3.0不适用,因为其去掉了tokenStream.next()方法,具体可参 考:http://www.cnblogs.com/LeftNotEasy/archive/2010/01/14/1647778.html
5. 运行后的结果: