簡體   English   中英

忽略Lucene的IncrementToken()方法中的令牌輸出

[英]Ignore a Token output in Lucene's IncrementToken() method

我正在嘗試在Lucene中創建一個自定義過濾器,該過濾器可以簡單地識別文本中的兩個后續單詞是否以大寫字母開頭,其余是否為小寫字母,在這種情況下,這兩個單詞將作為一個標記連接在一起。

被覆蓋的增量令牌方法具有以下代碼

@Override
    public boolean incrementToken() throws IOException {
        if(!input.incrementToken()){ 
     return false;}
     //Case were the previous token WAS NOT starting with capital letter and the     rest small   
     if(previousTokenCanditateMainName==false)
     {
            if(CheckIfMainName(termAtt.term()))
            {
                previousTokenCanditateMainName=true;
                tempString=this.termAtt.term() ;                           /*This is the*/
               // myToken.offsetAtt=this.offsetAtt;                             /*Token i need to "delete"*/
            tempStartOffset=this.offsetAtt.startOffset();
            tempEndOffset=this.offsetAtt.endOffset();
            return true;
        }
        else
        {
            return true;
        }
 }
 //Case were the previous token WAS a Proper name (starting with Capital and continuiing with small letters)
     else
         {
         if(CheckIfMainName(termAtt.term()))
         {
             previousTokenCanditateMainName=false;
             posIncrAtt.setPositionIncrement(0);
             termAtt.setTermBuffer(tempString+TOKEN_SEPARATOR+this.termAtt.term());
             offsetAtt.setOffset(tempStartOffset, this.offsetAtt.endOffset());
             return true;
         }
         else
         {
             previousTokenCanditateMainName=false;
             return true;
         }
     }

}

我的問題是,一旦找到符合我要求的第一個令牌,我該如何“忽略”它。 目前,代碼可以完美地結合兩個令牌使用,但是我還可以從我確定的兩個令牌中的第一個獲得一個額外的令牌。 我嘗試使用與內置stopFilter相同的方法setEnableIncrementsPosition(true),但是在這種情況下,我的過濾器需要是TokenFilter類型,不允許我覆蓋increasingToken方法。

我希望我正確地表達了我的問題

您可能有一個自定義方法:

private void tokenize() 

在其中進行拆分和自定義聯接的位置。 結果List<String> tokens需要作為標記生成器的屬性保存。

incrementToken方法中,您只需檢查此attribute是否為null並在必要時對其進行初始化。

您還需要將incrementToken()方法中的標記添加到termAttribute

termAttribute.append(tokens.get(tokenIndex));

這包括您的令牌生成器需要具有這樣的屬性:

private CharTermAttribute termAttribute = addAttribute(CharTermAttribute.class);

可能還需要一些微調。 但這僅僅是關於如何以一種非常簡單的方式實現這一目標的草案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM