lucene第一步-5.中文分词ikanalyzer和高亮highlighter的使用-llying-iteye技术网站

本文深入探讨了Lucene3.x版本中IKAnalyzer的部署与使用方法,包括中文分词与高亮显示的功能。介绍了IKAnalyzer的特性与优化,并提供了实践示例。
lucene第一步-5.中文分词ikanalyzer和高亮highlighter的使用-llying-iteye技术网站
2011年04月12日
   最近工作比较忙,一直没有更新,还请见谅。
  最近lucene已经更新到lucene 3.0版本了 2.X版本的一些用法已经彻底不在支持了。
  下面的例子主要是介绍中文分词器IKAnalyzer的使用和Lucene高亮显示。
  lucene 3.x版本中有些2.x方法已经完全被剔除了,这里会捎带一下3.x的用法,当然我这里用的还是2.X的版本。
  lucene自带的分词方式对中文分词十分的不友好,基本上可以用惨不忍睹来形容,所以这里推荐使用IKAnalyzer进行中文分词。
  IKAnalyzer分词器是一个非常优秀的中文分词器。
  下面是官方文档上的介绍
  采用了特有的“正向迭代最细粒度切分算法“,具有60万字/秒的高速处理能力。
  采用了多子处理器分析模式,支持:英文字母(IP地址、Email、URL)、数字(日期,常用中文数量词,罗马数字,科学计数法),中文词汇(姓名、地名处理)等分词处理。
  优化的词典存储,更小的内存占用。支持用户词典扩展定义.
  针对Lucene全文检索优化的查询分析器
  IKQueryParser(作者吐血推荐);采用歧义分析算法优化查询关键字的搜索排列组合,能极大的提高Lucene检索的命中率。
  1.IKAnalyzer的部署:将IKAnalyzer3.X.jar部署于项目的lib目录中;IKAnalyzer.cfg.xml与ext_stopword.dic文件放置在代码根目录下即可。
  ok 部署完IKAnalyzer我们先来测试一下
  package demo.test; import java.io.IOException; import java.io.StringReader; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.tokenattributes.TermAtt ribute; import org.apache.lucene.analysis.tokenattributes.TypeAtt ribute; import org.wltea.analyzer.lucene.IKAnalyzer; public class TestIKAnalyzer { public static void main(String[] args) throws IOException { Analyzer analyzer = new IKAnalyzer(); TokenStream tokenStream = analyzer.tokenStream("", new StringReader("永和服装饰品有限公司")); //2.x写法 3.0之后不支持了 /*Token token =new Token(); while(tokenStream.next(token)!=null){ System.out.println(token.term()); }*/ //3.x的写法 TermAttribute termAtt = (TermAttribute) tokenStream.getAttribute(TermAttribute.class); TypeAttribute typeAtt = (TypeAttribute) tokenStream.getAttribute(TypeAttribute.class); while (tokenStream.incrementToken()) { System.out.print(termAtt.term()); System.out.print(' '); System.out.println(typeAtt.type()); } } }
  分词结果 永和 和服 服装 装饰品 装饰 饰品 有限公司 有限 公司
  2.我们开始采用IKAnalyzer创建索引
  package demo.test; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.wltea.analyzer.lucene.IKAnalyzer; public class CreatIndex { @SuppressWarnings("deprecation") public static void main(String[] args) throws IOException { String path = "index";//索引目录 Analyzer analyzer = new IKAnalyzer();//采用的分词器 IndexWriter iwriter = new IndexWriter(path, analyzer, true); File dir = new File("data");//待索引的数据文件目录 File[] files = dir.listFiles(); for(int i=0;i","");//设定高亮显示的格式,也就是对高亮显示的词组加上前缀后缀 Highlighter highlighter = new Highlighter(simpleHtmlFormatter,new QueryScorer(query)); highlighter.setTextFragmenter(new SimpleFragmenter(150));//设置每次返回的字符数.想必大家在使用搜索引擎的时候也没有一并把全部数据展示出来吧,当然这里也是设定只展示部分数据 for(int i=0;i纯粹是赶潮流和恶搞。”
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值