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

来源:互联网 发布:微信单删软件 编辑:程序博客网 时间:2024/05/18 01:44
  

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);
   }

}
















  •  



  • 原创粉丝点击
    热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 飞机上烟瘾犯了怎么办 怀孕一个月吸烟了怎么办 烟瘾犯了没烟怎么办 押金交了不租了怎么办 买车首付款不够怎么办 双11订金不退怎么办 在商场买到假货怎么办 网上买到假手机怎么办 网上买了假手机怎么办 网银转账被骗了怎么办 支付宝被骗了钱怎么办 被支付宝骗了钱怎么办 头发出油怎么办小妙招 照相的时候脸歪怎么办 怀孕两个月同床了怎么办 小姐被警察抓了怎么办 我的世界迷路了怎么办 砸到手指甲很痛怎么办 手被锤子砸肿了怎么办 拇指突然疼肿了怎么办 手指甲压紫了痛怎么办 砸到手指头肿了怎么办 小婴儿体重不长怎么办 2岁半宝宝不说话怎么办 米兰叶子掉光了怎么办 qq雷霆战机闪退怎么办 微信限额20万怎么办 欧拉方程x<0怎么办 柿子核吃下去了怎么办 橡胶底白色变黄怎么办 20了丁丁还很小怎么办 5角硬币吞进肚子怎么办 孩子吞了5角硬币怎么办 左腿比右腿微粗怎么办 一个腿长一个腿短怎么办 刚买的毛笔很硬怎么办 我哥哥太爱我了怎么办 如果婚姻无法维持该怎么办 捡了一只流浪狗怎么办 微博加载不出来怎么办 我的脾气很暴躁怎么办