Lucene分词的注意事项

来源:互联网 发布:快递如何找淘宝客户 编辑:程序博客网 时间:2024/04/29 12:59

版本问题


JE分词目前只用于Lucene的1.9-2.4版本,3.0版本的分词不可用,原因是NoSuchMethodError,也就是类没有相应的方法

IK Analyzer 2012下载地址:

https://code.google.com/p/ik-analyzer/

http://ik-analyzer.googlecode.com/files/IKAnalyzer2012_u5.zip

参考:

http://www.cnblogs.com/luxh/archive/2012/06/23/2559260.html


paoding的使用会用到,所以请下载此包

commons-logging.jar 

由于3.0的JAR包还没有编译下载,所以需要自己checkout后,编译使用

其他配置可参考:

http://zengzhaoshuai.iteye.com/blog/986314


CJK分词没有词库,分词方式较为简单:

http://blog.csdn.net/liugangr/article/details/12690123

需要下载.java文件


中科院分词:

下载地址

http://ictclas.org/down/50/ICTCLAS50_Windows_64_JNI.rar


分词原理的介绍:

  • 单字分词:就是按照中文一个字一个字地进行分词。如:"我们是中国人"
    效果:""""""""""""。(StandardAnalyzer就是这样)。
  • 二分法分词:按两个字进行切分。如:"我们是中国人",效果:"我们""们是""是中""中国""国人"。(CJKAnalyzer就是这样)。
  • 词库分词:按某种算法构造词,然后去匹配已建好的词库集合,如果匹配到就切分出来成为词语。通常词库分词被认为是最理想的中文分词算法。如:"我们是中国人",效果为:"我们""中国人"。(使用极易分词的MMAnalyzer。可以使用"极易分词",或者是"庖丁分词"分词器、IKAnalyzer)。

 参照:

http://www.cnblogs.com/ibook360/archive/2011/10/18/2216631.html



3.0的代码可以参照下面:

package Mylucene.com.IndexWriter;


import java.io.File;
import java.io.StringReader;


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.Tokenizer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;


import jeasy.analysis.MMAnalyzer;


//主要测试3.0的分词,缺点是没有输出词频
public class AnalyzerTest {


private static String Dest_Index_path = "C:\\CC1"; 
public static void main(String[] args) {
// TODO Auto-generated method stub



try{
MaxFieldLength len = new MaxFieldLength(1024);
//Analyzer textAna = new MMAnalyzer();
//Analyzer textAna = new StandardAnalyzer(Version.LUCENE_30);
Analyzer textAna = new SimpleAnalyzer();
Directory d = FSDirectory.open(new File(Dest_Index_path));
IndexWriter writer = new IndexWriter(d, textAna, true, len);
Document doc = new Document();

String str = "中华人民共和国成立了,欢迎。来。北京 谢谢大家";
//String str = "welcome to beijing, my friend,";
Field field = new Field("Content", str, Field.Store.YES, Field.Index.ANALYZED);

doc.add(field);

TokenStream stream = textAna.tokenStream("Content", new StringReader(str));
System.out.println("begin");
while(stream.incrementToken())
{

TermAttribute ta = stream.getAttribute(TermAttribute.class);

System.out.println(ta.toString());
}

writer.optimize();
writer.close();
System.out.println("over");
}catch(Exception e)
{
e.printStackTrace();
}
}


}

0 0
原创粉丝点击