第四步:查看StandardAnalyzer的分词效果并添加停用词

LUCENE的创建索引有好多种分词方式,这里我们用的StandardAnalyzer分词

package cn.lucene;

import java.io.IOException;

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.util.CharArraySet;

public class test1 {
	public static final String[] china_stop = {"着", "的", "之", "式"};
	public static void main(String[] args) throws IOException {
		//把数组赋值到CharArraySet里
		CharArraySet cnstop=new CharArraySet(china_stop.length, true);
	    for(String value : china_stop) {
	    	cnstop.add(value);
	    }
	    //并把StandardAnalyzer默认的停用词加进来
	    cnstop.addAll(StandardAnalyzer.STOP_WORDS_SET);
	    System.out.println(cnstop);		
		
		Analyzer analyzer = new StandardAnalyzer(cnstop);
		TokenStream stream=  analyzer.tokenStream("", "中秋be之夜,享受着月华的孤独,享受着爆炸式的思维跃迁");
		//获取每个单词信息,获取词元文本属性
		CharTermAttribute cta = stream.addAttribute(CharTermAttribute.class);
		stream.reset();
        while(stream.incrementToken()){
            System.out.print("[" + cta + "]");
        }
        System.out.println();
		analyzer.close();
	}
}

输出结果如下:

输入所有的停止词,可以看到新的停止词已经加进去了

[着, but, be, 的, with, such, then, for, 之, 式, no, will, not, are, and, their, if, this, on, into, a, or, there, in, that, they, was, is, it, an, the, as, at, of, by, to, these]

分词结果,"着", "的", "之", "式"四个词已经被停止分词了
[中][秋][夜][享][受][月][华][孤][独][享][受][爆][炸][思][维][跃][迁]

通过上面的分词效果应该就知道StandardAnalyzer是怎么分词了吧!

转载于:https://www.cnblogs.com/lb809663396/p/5856662.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,我可以给出以下的解答:要使用Lucene进行去除干提取,您需要按照以下步骤进行操作: 1. 下载并导入Lucene库:您需要下载Lucene库并将其导入到Java项目中。 2. 创建分词器:使用Lucene库中的分词器对文本进行分词处理。 3. 去除:使用Lucene库中的过滤器去除。 4. 进行干提取:使用Lucene库中的干提取器进行干提取处理。 下面是一个使用Lucene对文本进行去除干提取的示例代码: ``` import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.core.StopAnalyzer; import org.apache.lucene.analysis.en.PorterStemFilter; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import java.io.IOException; import java.io.StringReader; public class LuceneStemmingExample { public static void main(String[] args) throws IOException { // 创建分词器 Analyzer analyzer = new StandardAnalyzer(); // 创建过滤器 StopAnalyzer stopAnalyzer = new StopAnalyzer(); // 创建干提取器 PorterStemFilter stemFilter = new PorterStemFilter(analyzer.tokenStream(null, new StringReader("running"))); // 去除 TokenStream tokenStream = stopAnalyzer.tokenStream(null, new StringReader("I am running in the park")); // 获取处理结果 CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class); tokenStream.reset(); while (tokenStream.incrementToken()) { // 进行干提取 stemFilter.reset(); stemFilter.incrementToken(); // 输出处理结果 System.out.println(charTermAttribute.toString()); } tokenStream.end(); tokenStream.close(); } } ``` 这个示例代码中,我们使用了StandardAnalyzer分词器和StopAnalyzer过滤器,同时使用了PorterStemFilter干提取器对文本进行处理。在输出的结果中,我们可以看到已经被去除,而单“running”也被转换为了“run”。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值