package cn.itcast.lucene.analyzer;
import java.io.StringReader;
import jeasy.analysis.MMAnalyzer;
import net.paoding.analysis.analyzer.PaodingAnalyzer;
import net.paoding.analysis.knife.Paoding;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.junit.Test;
public class AnalyzerTest {
String enText = "IndexWriter addDocument's a javadoc.txt";
// String zhText = "我们是中国人";
// String zhText = "小笑话_总统的房间 Room .txt";
String zhText = "一位绅士到旅游胜地的一家饭店要开个房间";
Analyzer en1 = new StandardAnalyzer(); // 单字分词
Analyzer en2 = new SimpleAnalyzer();
Analyzer zh1 = new CJKAnalyzer(); // 二分法分词
Analyzer zh2 = new MMAnalyzer(); // 词库分词
Analyzer zh3 = new PaodingAnalyzer();
@Test
public void test() throws Exception {
// analyze(en2, enText);
// analyze(en1, zhText);
// analyze(zh1, zhText);[size=xx-small][/size]
analyze(zh3, zhText);
}
public void analyze(Analyzer analyzer, String text) throws Exception {
System.out.println("--> 分词器:" + analyzer.getClass());
TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(text));
for (Token token = new Token(); (token = tokenStream.next(token)) != null;) {
System.out.println(token);
}
}
}
[color=darkred]在项目里创建了paoding-dic-home.properties这个文件,内容如下:[/color]
paoding.dic.home=E:/lucene/paoding-analysis-2.0.4-beta/dic
paoding.dic.detector.interval=60
[color=darkred]当加上PaoDing分词器的时候就报错,注释掉就没有问题。
错误如下:[/color]
net.paoding.analysis.exception.PaodingAnalysisException: please set a system env PAODING_DIC_HOME or Config paoding.dic.home in paoding-dic-home.properties point to the dictionaries!
at net.paoding.analysis.knife.PaodingMaker.setDicHomeProperties(PaodingMaker.java:320)
at net.paoding.analysis.knife.PaodingMaker.getDicHome(PaodingMaker.java:261)
at net.paoding.analysis.knife.PaodingMaker.loadProperties(PaodingMaker.java:189)
at net.paoding.analysis.knife.PaodingMaker.loadProperties(PaodingMaker.java:228)
at net.paoding.analysis.knife.PaodingMaker.loadProperties(PaodingMaker.java:228)
at net.paoding.analysis.knife.PaodingMaker.getProperties(PaodingMaker.java:130)
at net.paoding.analysis.analyzer.PaodingAnalyzer.init(PaodingAnalyzer.java:70)
at net.paoding.analysis.analyzer.PaodingAnalyzer.(PaodingAnalyzer.java:59)
at net.paoding.analysis.analyzer.PaodingAnalyzer.(PaodingAnalyzer.java:52)
at cn.itcast.lucene.analyzer.AnalyzerTest11.(AnalyzerTest11.java:30)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:171)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:216)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:213)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)