Lucene_demo02_分词

本文介绍了使用Lucene进行文本分词的方法,并通过具体示例展示了如何针对英文和中文文本应用不同的分词器,包括标准分词器、单字分词器、二分法分词器及IK分词器。

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

[size=xx-large][color=orange][b]Lucene_demo02_分词[/b][/color][/size]



/**
* 英文的分词器 中文的分词器
*/
public class AnalyzerTest {

/**
* 英文分词:(Lucene自带包)
* @throws Exception
*/
@Test
public void testEN() throws Exception {
String text = "Creates a searcher searching the index in the named directory";
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
this.testAnalyzer(analyzer, text);
}

/**
* 中文分词:单字分词器(Lucene自带包)
* @throws Exception
*/
@Test
public void testCH1() throws Exception {
String text = "LBJ和韦德能带领热火在2013赛季拿到NBA总冠军吗?";
Analyzer analyzer = new ChineseAnalyzer();
this.testAnalyzer(analyzer, text);
}

/**
* 中文分词:二分法分词器(Lucene自带包)
* @throws Exception
*/
@Test
public void testCH2() throws Exception {
String text = "LBJ和韦德能带领热火在2013赛季拿到NBA总冠军吗";
Analyzer analyzer = new CJKAnalyzer(Version.LUCENE_30);
this.testAnalyzer(analyzer, text);
}

/**
* 中文分词:IK分词器(Lucene自带包)
* @throws Exception
*/
@Test
public void testCH3() throws Exception {
String text = "fasd";
Analyzer analyzer = new IKAnalyzer();
this.testAnalyzer(analyzer, text);
}

/**
* 输出分词后的结果
* @param analyzer
* @param text
* @throws Exception
*/
private void testAnalyzer(Analyzer analyzer, String text) throws Exception {
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());
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值