lucene-词干分析与保留空位和停用词

1、分析器可以剔除所有停用词,并将这些词本来所处的位置留空,还可以对词干过滤器进行选择

2、移除停用词

public class PositionStopFilter extends TokenFilter{

   private Set stopWords;

   publicPositioinStopFilter(TokenSteam in,Set stpWords){

      super(in);

      this.stopWords=stopWords;

   }

  

   public final Token next()throws IOException{ 

       int increment=0;

       for(Token token=input.next();token!=null;token=input.next()){

           if (!stopWords.contains(token.termText())){

                token.getPositionIncrement()+increment;

                 return token;

          }

       increment++;

   }

   return null;

3、自己定义的分析器

public class PositionalPorterStopAnalyer extends Analyer{

   private Set stopWords;

   publicPositionalPortStopAnalyer(){

     this(StopAnalyzer.ENGLISH_STOP_WORDS);

   }

   publicPositionalPortStopAnalyzer(String[] stopList){

       stopWords=stopFilter.makeStopset(stopList);

   }

  //下面的代码使用了词干还原PorterStemFilter

   public TokenStreamtokenStream(String fieldName,Reader reader){

    return new PorterStemFilter

         new PositionalStopFilter{

             new LowerCaseTokenizer(reader),stopwords));

         }

    )

   }

}

4、使用PharseQuery允许查询的短语有空缺,但是必须使用setPhraseSlop(n)进行设置,将空缺设置为n,空缺一般是停用词造成的。

您好!针对您的问题,可以使用 Lucene 的分器和分析器来进行去除停用词词干提取。以下是一个简单的示例: ```java 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.tokenattributes.CharTermAttribute; import org.apache.lucene.util.Version; import java.io.IOException; import java.io.StringReader; public class LuceneStemmingExample { public static void main(String[] args) throws IOException { String text = "Lucene is a Java full-text search engine. " + "Lucene is not a complete application, but rather a code library " + "and lucene is used to add search capability to applications."; Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_7_7_0); analyzer = new StopAnalyzer(Version.LUCENE_7_7_0); // 添加停用词 analyzer = new PorterStemFilter(analyzer); // 添加词干提取器 StringReader reader = new StringReader(text); TokenStream tokenStream = analyzer.tokenStream("", reader); CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class); tokenStream.reset(); while (tokenStream.incrementToken()) { System.out.println(charTermAttribute.toString()); } tokenStream.end(); tokenStream.close(); } } ``` 在上面的示例中,我们使用了 Lucene 的 `StandardAnalyzer` 分析器来对文本进行分,然后使用了 `StopAnalyzer` 停用词分析器来去除停用词,最后使用了 `PorterStemFilter` 词干提取器来对进行词干提取。最终输出的结果如下: ``` lucene java full text search engine lucene complete application rather code library lucene used add search capability applications ``` 可以看到,输出的已经被去除了停用词并被进行了词干提取。希望这个示例对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值