【Lucene】TokenStream-语汇单元的项、偏移量、类型和位置增量

代码:
package com.tan.code;

import java.io.IOException;
import java.io.StringReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
import org.apache.lucene.util.Version;

public class TokenStreamDetails {

	public void tokeStrem(String text) throws IOException {
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
		StringReader reader = new StringReader(text);
		TokenStream tokenStream = analyzer.tokenStream("", reader);

		// 语汇单元对应的文本
		CharTermAttribute charTermAttribute = tokenStream
				.addAttribute(CharTermAttribute.class);
		// 其实字符和终止字符的偏移量
		OffsetAttribute offsetAttribute = tokenStream
				.addAttribute(OffsetAttribute.class);
		// 位置增量(默认为1)
		PositionIncrementAttribute positionIncrementAttribute = tokenStream
				.addAttribute(PositionIncrementAttribute.class);
		// 语汇单元类型(默认为单词)
		TypeAttribute typeAttribute = tokenStream
				.addAttribute(TypeAttribute.class);

		int position = 0;
		// 递归处理所有语汇单元
		while (tokenStream.incrementToken()) {
			int increment = positionIncrementAttribute.getPositionIncrement();
			if (increment > 0) {
				// 计算位置信息
				System.out.println("position:" + (position += increment));
			}
			// 打印所有语汇单元详细信息
			System.out.println("【Trem:" + charTermAttribute.toString()
					+ "】【StartOffset:" + offsetAttribute.startOffset()
					+ "】【EndOffset:" + offsetAttribute.endOffset() + "】【Type:"
					+ typeAttribute.type()+"】");
		}
	}
}

测试

@Test
	public void testTokenStream() throws IOException {
		TokenStreamDetails tokenStreamDetails = new TokenStreamDetails();
		
		String text = "打印所有的TokenStream的詳細信息!Print the TokenStream Data";
		tokenStreamDetails.tokeStrem(text);
	}

输出:

position:1
【Trem:打】【StartOffset:0】【EndOffset:1】【Type:<IDEOGRAPHIC>】
position:2
【Trem:印】【StartOffset:1】【EndOffset:2】【Type:<IDEOGRAPHIC>】
position:3
【Trem:所】【StartOffset:2】【EndOffset:3】【Type:<IDEOGRAPHIC>】
position:4
【Trem:有】【StartOffset:3】【EndOffset:4】【Type:<IDEOGRAPHIC>】
position:5
【Trem:的】【StartOffset:4】【EndOffset:5】【Type:<IDEOGRAPHIC>】
position:6
【Trem:tokenstream】【StartOffset:5】【EndOffset:16】【Type:<ALPHANUM>】
position:7
【Trem:的】【StartOffset:16】【EndOffset:17】【Type:<IDEOGRAPHIC>】
position:8
【Trem:詳】【StartOffset:17】【EndOffset:18】【Type:<IDEOGRAPHIC>】
position:9
【Trem:細】【StartOffset:18】【EndOffset:19】【Type:<IDEOGRAPHIC>】
position:10
【Trem:信】【StartOffset:19】【EndOffset:20】【Type:<IDEOGRAPHIC>】
position:11
【Trem:息】【StartOffset:20】【EndOffset:21】【Type:<IDEOGRAPHIC>】
position:12
【Trem:print】【StartOffset:22】【EndOffset:27】【Type:<ALPHANUM>】
position:14
【Trem:tokenstream】【StartOffset:32】【EndOffset:43】【Type:<ALPHANUM>】
position:15
【Trem:data】【StartOffset:44】【EndOffset:48】【Type:<ALPHANUM>】


语汇单元的组成【截图】,其中各个属性的作用可以参考《Lucene In Action》:

从代码示例中看到,可以通过调用addAttribute(class)来获取这些属性;使用tokenStream.incrementToken()递归访问所有的语汇单元,如果该方法到达下一个新的语汇单元则返回true,若已经对stream处理完毕则返回false。然后就可以与先前获取的属性对象进行交互得到针对每个语汇单元的属性值。当incrementToken返回true时,其中所有的属性都会将内部状态修改为下一个语汇单元



Lucene是一个全文检索引擎,它的核心数据结构包括倒排索引和正排索引。其中,倒排索引是Lucene最重要的数据结构之一,它通过将文档中的每个词都映射到包含该词的文档列表来实现快速的文本搜索。 Lucene中的Term Dictionary和Term Index是倒排索引中的两个重要组成部分。Term Dictionary用于存储所有唯一的词(term),而Term Index则用于快速定位某个词位置。 在Lucene中,Term Dictionary和Term Index通常存储在磁盘上。Term Dictionary通常使用一种称为Trie树的数据结构来实现。Trie树是一种树形数据结构,它可以快速地查找某个字符串是否存在,以及在字符串集合中查找前缀匹配的字符串。 Term Index则通常存储在一个称为倒排索引表(Inverted Index Table)的结构中。倒排索引表是由一系列的倒排索引条目(Inverted Index Entry)组成的,每个倒排索引条目包含了一个词及其在倒排索引中的位置信息,例如该词在文档列表中出现的次数、该词在哪些文档中出现等。 当进行文本搜索时,Lucene会首先在Term Dictionary中查找搜索关键词是否存在,然后通过Term Index快速定位到包含该词的文档列表,最后根据文档列表中的文档ID查找正排索引中具体的文档内容。这种基于倒排索引的搜索方式可以实现非常高效的文本搜索,是Lucene等全文检索引擎的核心技术之一。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值