lucene 获取分词后的关键词

本文探讨了在Lucene中使用不同的分词器如何影响最终的关键词提取和所需时间。以IKAnalyzer为例,虽然其在中文分词上有优势,但在作者的电脑上,分词过程耗时约800毫秒。分词流程包括输入文本,进行关键词划分,移除停用词,形态还原以及转换为小写形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用不同的分词器, 最后得到的关键词不同, 需要的时间也不同

需要中文分词是, 用IKAnalyzer是不错的选择, 但相比时间, 我的电脑上大概分词需要800+ms


分词器工作流程:

输入文本(What's your name?)
    →关键词划分(What's ; your ; name), 不同分词器分法不同
        →消除停用词()
            →形态还原 (What's -> What)
                →转化小写(What -> what)


private long stime;
private long etime;
private Analyzer analyzer;
@Before
public void s(){
	stime = System.currentTimeMillis();
}
@After
public void e(){
	etime = System.currentTimeMillis();
	System.out.println("使用" + analyzer.getClass().getName() + "分词, 耗时" + (etime - stime) + "ms");
}

@Test
public void test() throws Exception {
	//analyzer = new SimpleAnalyzer(Version.LUCENE_35);
	//analyzer = new StandardAnalyzer(Version.LUCENE_35);
	analyzer = new IKAnalyzer();
	analyze(analyzer, "hTTp://www.baidu.com/s?wd=Lucene中文分词");
}
private void analyze(Analyzer analyzer, String text) throws Exception {
	TokenStream tokens = analyzer.reusableTokenStream("content", new StringReader(text));
	OffsetAttribute offsetAttr = tokens.getAttribute(OffsetAttribute.class);
	CharTermAttribute charTermAttr = tokens.getAttribute(CharTermAttribute.class);
	while (tokens.incrementToken()) {
		char[] charBuf = charTermAttr.buffer();
		String term = new String(charBuf, 0, offsetAttr.endOffset() - offsetAttr.startOffset());
		System.out.println(term + ", " + offsetAttr.startOffset() + ", " + offsetAttr.endOffset());
	}
	tokens.close();
	// while (ts.incrementToken()) {//过时
	// TermAttribute ta = ts.getAttribute(TermAttribute.class);
	// System.out.println(ta.term());
	// }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值