Lucene分词器(搜索关键字解析器)

27 篇文章 0 订阅
23 篇文章 0 订阅
  

import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.Date;


import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.KeywordAnalyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.util.Version;


public class luncens {


public static void analyze(Analyzer analyzer, String text) throws Exception {
   System.out.println("分词器:" + analyzer.getClass());
   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());                                          
        } 
}


public static void main(String[] args) throws Exception {
   String enText = "China is a great country!JC-46C(16tt)dd";
  // String chText = "昨天,一阵雷阵雨后,空气异常  地清新!46C";
   String chText = "SICAO新潮半导体电子酒柜JC-46C(16支)";
   SimpleAnalyzer analyzer1 = new SimpleAnalyzer();
   analyze(analyzer1,enText);
   analyze(analyzer1,chText);
   StopAnalyzer analyzer2 = new StopAnalyzer(Version.LUCENE_CURRENT);
   analyze(analyzer2,enText);
   analyze(analyzer2,chText);
   StandardAnalyzer analyzer3 = new StandardAnalyzer(Version.LUCENE_CURRENT);
   analyze(analyzer3,enText);
   analyze(analyzer3,chText);//逐个字切分
   WhitespaceAnalyzer analyzer4 = new WhitespaceAnalyzer();
   analyze(analyzer4,enText);
   analyze(analyzer4,chText);
   KeywordAnalyzer analyzer5 = new KeywordAnalyzer();
   analyze(analyzer5,enText);
   analyze(analyzer5,chText);
   
   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

Date date = new Date();
//当前时间
String endtime = format.format(date).substring(11, 13); 
System.out.println("时间="+endtime);
   }

}
















  •  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值