你好,游客 登录 注册 搜索
背景:
阅读新闻

Lucene入门基础教程

分词器

[日期:2014-06-08] 来源:Linux社区  作者:xinxinqiu [字体:大 中 小]

/**
  * 经过该方法可以把分词后的结果输出
  * @param analyzer
  * @param text
  * @throws Exception
  */
 private void testAnalyzer(Analyzer analyzer,String text)throws Exception{
  TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(text));
  tokenStream.addAttribute(TermAttribute.class);
  while(tokenStream.incrementToken()){
   TermAttribute termAttribute = tokenStream.getAttribute(TermAttribute.class);
   System.out.println(termAttribute.term());
  }
 }

 

/**
 * 主要針對汉语
 * 英语
 *   
 * 汉语
 * @author Administrator
 *
 */
public class AnalyzerTest {
 @Test
 public void testEn() throws Exception{
  /**
  * Creates a searcher searching the index in the named directory
  */
  /**
  * 1、切分关键词
  * 2、去掉停用词
  * 3、把大写转化成小写
  */
  String text = "Creates a searcher searching the index in the named directory";
  Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
  this.testAnalyzer(analyzer, text);
 }
 
 @Test
 public void testZH() throws Exception{
  /**
  * 单字分词
  */
  Analyzer analyzer = new ChineseAnalyzer();
  String text = "传智播客的黎活明是UFO";
  this.testAnalyzer(analyzer, text);
 }
 
 @Test
 public void testZH2() throws Exception{
  /**
  * 单字分词
  */
  Analyzer analyzer = new CJKAnalyzer(Version.LUCENE_30);
  String text = "传智播客的黎活明是UFO";
  this.testAnalyzer(analyzer, text);
 }
 
 @Test
 public void testZH3() throws Exception{
  Analyzer analyzer = new IKAnalyzer();
  String text = "北京美女";
  this.testAnalyzer(analyzer, text);
 }
 /**
  * 经过该方法可以把分词后的结果输出
  * @param analyzer
  * @param text
  * @throws Exception
  */
 private void testAnalyzer(Analyzer analyzer,String text)throws Exception{
  TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(text));
  tokenStream.addAttribute(TermAttribute.class);
  while(tokenStream.incrementToken()){
   TermAttribute termAttribute = tokenStream.getAttribute(TermAttribute.class);
   System.out.println(termAttribute.term());
  }
 }
}

linux
【内容导航】
第1页:简介 第2页:准备Lucene的开发环境
第3页:保持数据库与索引库的同步 第4页:indexWriter
第5页:与索引库的交互 第6页:分词器
第7页:分页 第8页:搜索方式
第9页:排序+高亮 第10页:常用API
相关资讯       Lucene  Lucene教程  Lucene简介 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款