Lucene04---分词器
来源:互联网 发布:面向对象编程的语言 编辑:程序博客网 时间:2024/06/11 10:42
我们知道,Lucene所做的事情主要就两件,一是创建索引,一是搜索。那么这里就有一个很重要的东西就是分词器,分词器在http://xdwangiflytek.iteye.com/blog/1389308里就提到了,这里再说说,分词器,对文本资源进行切分,将文本按规则切分成一个个进行索引的最小单位(关键词)。建立索引和进行搜索时都要用到分词器,为了保证正确的搜索到结果,在建立索引与进行搜索时使用的分词器应为同一个。
分词器,我们涉及到的是两种,一是英文的,一是中文的,英文的相对中文来说要简单点。
直接上代码吧
AnalyzerDemo.java:
- package com.iflytek.lucene;
- import java.io.StringReader;
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.SimpleAnalyzer;
- import org.apache.lucene.analysis.TokenStream;
- import org.apache.lucene.analysis.cjk.CJKAnalyzer;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
- import org.apache.lucene.util.Version;
- /**
- * @author xudongwang 2012-2-4
- *
- * Email:xdwangiflytek@gmail.com
- */
- public class AnalyzerDemo {
- public void analyze(Analyzer analyzer, String text) throws Exception {
- System.out.println("----------------------->分词器:" + analyzer.getClass());
- TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(text));
- CharTermAttribute termAtt = (CharTermAttribute) tokenStream.getAttribute(CharTermAttribute.class);
- // TypeAttribute typeAtt = (TypeAttribute) tokenStream
- // .getAttribute(TypeAttribute.class);
- while (tokenStream.incrementToken()) {
- System.out.println(termAtt.toString());
- // System.out.println(typeAtt.type());
- }
- }
- public static void main(String[] args) throws Exception {
- AnalyzerDemo demo = new AnalyzerDemo();
- System.out.println("---------------->测试英文");
- String enText = "Hello, my name is wang xudong, I in iteye blog address is xdwangiflytek.iteye.com";
- System.out.println(enText);
- System.out.println("By StandardAnalyzer 方式分词:");
- Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
- demo.analyze(analyzer, enText);
- System.out.println("By SimpleAnalyzer 方式分词:");
- Analyzer analyzer2 = new SimpleAnalyzer(Version.LUCENE_35);
- demo.analyze(analyzer2, enText);
- System.out.println("通过上面的结果发现StandardAnalyzer分词器不会按.来区分的,而SimpleAnalyzer是按.来区分的");
- System.out.println();
- System.out.println("---------------->测试中文");
- String znText = "我叫王旭东";
- System.out.println(znText);
- System.out.println("By StandardAnalyzer 方式分词:");
- // 通过结果发现它是将每个字都作为一个关键字,这样的话效率肯定很低咯
- demo.analyze(analyzer, znText);
- System.out.println("By CJKAnalyzer 方式(二分法分词)分词:");
- Analyzer analyzer3 = new CJKAnalyzer(Version.LUCENE_35);
- demo.analyze(analyzer3, znText);
- }
- }
package com.iflytek.lucene;import java.io.StringReader;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.SimpleAnalyzer;import org.apache.lucene.analysis.TokenStream;import org.apache.lucene.analysis.cjk.CJKAnalyzer;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;import org.apache.lucene.util.Version;/** * @author xudongwang 2012-2-4 * * Email:xdwangiflytek@gmail.com */public class AnalyzerDemo {public void analyze(Analyzer analyzer, String text) throws Exception {System.out.println("----------------------->分词器:" + analyzer.getClass());TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(text));CharTermAttribute termAtt = (CharTermAttribute) tokenStream.getAttribute(CharTermAttribute.class);// TypeAttribute typeAtt = (TypeAttribute) tokenStream// .getAttribute(TypeAttribute.class);while (tokenStream.incrementToken()) {System.out.println(termAtt.toString());// System.out.println(typeAtt.type());}}public static void main(String[] args) throws Exception {AnalyzerDemo demo = new AnalyzerDemo();System.out.println("---------------->测试英文");String enText = "Hello, my name is wang xudong, I in iteye blog address is xdwangiflytek.iteye.com";System.out.println(enText);System.out.println("By StandardAnalyzer 方式分词:");Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);demo.analyze(analyzer, enText);System.out.println("By SimpleAnalyzer 方式分词:");Analyzer analyzer2 = new SimpleAnalyzer(Version.LUCENE_35);demo.analyze(analyzer2, enText);System.out.println("通过上面的结果发现StandardAnalyzer分词器不会按.来区分的,而SimpleAnalyzer是按.来区分的");System.out.println();System.out.println("---------------->测试中文");String znText = "我叫王旭东";System.out.println(znText);System.out.println("By StandardAnalyzer 方式分词:");// 通过结果发现它是将每个字都作为一个关键字,这样的话效率肯定很低咯demo.analyze(analyzer, znText);System.out.println("By CJKAnalyzer 方式(二分法分词)分词:");Analyzer analyzer3 = new CJKAnalyzer(Version.LUCENE_35);demo.analyze(analyzer3, znText);}}
运行结果:
---------------->测试英文
Hello, my name is wang xudong, I in iteye blog address is xdwangiflytek.iteye.com
By StandardAnalyzer 方式分词:
----------------------->分词器:class org.apache.lucene.analysis.standard.StandardAnalyzer
hello
my
name
wang
xudong
i
iteye
blog
address
xdwangiflytek.iteye.com
By SimpleAnalyzer 方式分词:
----------------------->分词器:class org.apache.lucene.analysis.SimpleAnalyzer
hello
my
name
is
wang
xudong
i
in
iteye
blog
address
is
xdwangiflytek
iteye
com
通过上面的结果发现StandardAnalyzer分词器不会按.来区分的,而SimpleAnalyzer是按.来区分的
---------------->测试中文
我叫王旭东
By StandardAnalyzer 方式分词:
----------------------->分词器:class org.apache.lucene.analysis.standard.StandardAnalyzer
我
叫
王
旭
东
By CJKAnalyzer 方式(二分法分词)分词:
----------------------->分词器:class org.apache.lucene.analysis.cjk.CJKAnalyzer
我叫
叫王
王旭
旭东
上面讲的分词方式中对于中文来说最好的还是语义分词,就是中科院的那个。
后面对于具体中文分词器,我会在专门的专题中去说的,这里先简单了解一下.
- Lucene04---分词器
- lucene分词器分词
- 分词器
- 分词器
- lucene分词器分词demo
- word分词器、ansj分词器、IKanalyzer分词器、mmseg4j分词器、jcseg分词器对比
- word分词器、ansj分词器、IKanalyzer分词器、mmseg4j分词器、jcseg分词器对比
- lucene4.7 分词器 自定义分词器
- 自定义lucene分词器,单字分词
- 指定分词器测试分词结果
- Lucene 分词器学习
- 分词器的测试
- Java 中文分词器
- IKanalyzer 分词器(???)
- 分词器比较
- Lucene 自定义分词器
- 中文分词器 jcseg
- lucene 分词器Analyzer
- [Oracle数据库] 急!关于在AIX4.3.3版本下导8.0.5数据库DMP文件大小的限制!
- Lucene03---索引位置的优化(内存和磁盘配合使用)
- Android多媒体分析(三)通过MediaStore获取Audio信息
- SQL*LOADER操作篇
- android使用google map api 出现INSTALL_FAILED_MISSING_SHARED_LIBRARY 错误
- Lucene04---分词器
- Http 请求
- 全文检索与Lucene学习
- usleep函数
- 发送arp包获取mac
- ubuntu 32 bit系统下编译android 2.3
- Lucene05---Highlighter
- unix下面kill oracle里面已经处于killed状态的session
- Android多媒体分析(四)AudioManager