Lucene初学——3. 分词器学习

1.demo

public class AnalyzerStudy {

	private static String str = "分词器测试 Lucene 案例 开发 by future for study";
	
	private static void print(Analyzer analyzer) {
		StringReader reader = new StringReader(str);
		TokenStream stream = analyzer.tokenStream("", reader);
		try {
			stream.reset();
			CharTermAttribute term = stream.getAttribute(CharTermAttribute.class);
			System.out.println("分词技术: " + analyzer.getClass());
			while (stream.incrementToken()) {
				System.out.print(term.toString() + "|");
			}
			System.out.println();
			System.out.println("===========================================");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		/*
		 * 标准分词器
		 * 每个汉字是一个词,每段英文是一个词,会忽略某些词
		 */
		print(new StandardAnalyzer());
		// 空格分词器,以空格分词
		print(new WhitespaceAnalyzer());
		// 简单分词器,结果与空格分词器相似
		print(new SimpleAnalyzer());
		// 二分法分词器,对于英文,以空格分词,对于中文,会与前后的一个中文分别组词,会忽略指定默认单词
		print(new CJKAnalyzer());
		// 关键字分词器,整个一段作为分词结果
		print(new KeywordAnalyzer());
		// 被忽略词分词器,忽略指定单词
		/*
		 * "a", "an", "and", "are", "as", "at", "be", "but", "by",
		 * "for", "if", "in", "into", "is", "it",
		 * "no", "not", "of", "on", "or", "such",
		 * "that", "the", "their", "then", "there", "these",
		 * "they", "this", "to", "was", "will", "with"
		 */
		print(new StopAnalyzer());
	}
}

2.运行结果

分词技术: class org.apache.lucene.analysis.standard.StandardAnalyzer
分|词|器|测|试|lucene|案|例|开|发|future|study|
===========================================
分词技术: class org.apache.lucene.analysis.core.WhitespaceAnalyzer
分词器测试|Lucene|案例|开发|by|future|for|study|
===========================================
分词技术: class org.apache.lucene.analysis.core.SimpleAnalyzer
分词器测试|lucene|案例|开发|by|future|for|study|
===========================================
分词技术: class org.apache.lucene.analysis.cjk.CJKAnalyzer
分词|词器|器测|测试|lucene|案例|开发|future|study|
===========================================
分词技术: class org.apache.lucene.analysis.core.KeywordAnalyzer
分词器测试 Lucene 案例 开发 by future for study|
===========================================
分词技术: class org.apache.lucene.analysis.core.StopAnalyzer
分词器测试|lucene|案例|开发|future|study|
===========================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

future_1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值