custom token filter generates empty tokens

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

custom token filter generates empty tokens

G.Long
Hi :)

I wrote a custom token filter which removes special characters.
Sometimes, all characters of the token are removed so the filter
procudes an empty token. I would like to remove this token from the
tokenstream but i'm not sure how to do that.

Is there something missing in my custom token filter or do I need to
chain another custom token filter to remove empty tokens?

Regards :)

ps:

this is the code of my custom filter :

public class SpecialCharFilter extends TokenFilter {

     private final CharTermAttribute termAtt =
addAttribute(CharTermAttribute.class);

     protected SpecialCharFilter(TokenStream input) {
         super(input);
     }

     @Override
     public boolean incrementToken() throws IOException {

         if (!input.incrementToken()) {
             return false;
         }

         final char[] buffer = termAtt.buffer();
         final int length = termAtt.length();
         final char[] newBuffer = new char[length];

         int newIndex = 0;
         for (int i = 0; i < length; i++) {
             if (!isFilteredChar(buffer[i])) {
                 newBuffer[newIndex] = buffer[i];
                 newIndex++;
             }
         }

         String term = new String(newBuffer);
         term = term.trim();
         char[] characters = term.toCharArray();
         termAtt.setEmpty();
         termAtt.copyBuffer(characters, 0, characters.length);

         return true;
     }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: custom token filter generates empty tokens

Jose Fernandez
When you return true from incrementToken() you tell Lucene to add your token to the token stream. At the end of incrementToken() check for an empty token. If it's empty then return incrementToken() to process the next token. This will affect your positions so if you're doing phrase search you will need to adjust the position attribute to account for the now-empty token.

-----Original Message-----
From: G.Long [mailto:[hidden email]]
Sent: Thursday, October 09, 2014 7:54 AM
To: [hidden email]
Subject: custom token filter generates empty tokens

Hi :)

I wrote a custom token filter which removes special characters.
Sometimes, all characters of the token are removed so the filter procudes an empty token. I would like to remove this token from the tokenstream but i'm not sure how to do that.

Is there something missing in my custom token filter or do I need to chain another custom token filter to remove empty tokens?

Regards :)

ps:

this is the code of my custom filter :

public class SpecialCharFilter extends TokenFilter {

     private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);

     protected SpecialCharFilter(TokenStream input) {
         super(input);
     }

     @Override
     public boolean incrementToken() throws IOException {

         if (!input.incrementToken()) {
             return false;
         }

         final char[] buffer = termAtt.buffer();
         final int length = termAtt.length();
         final char[] newBuffer = new char[length];

         int newIndex = 0;
         for (int i = 0; i < length; i++) {
             if (!isFilteredChar(buffer[i])) {
                 newBuffer[newIndex] = buffer[i];
                 newIndex++;
             }
         }

         String term = new String(newBuffer);
         term = term.trim();
         char[] characters = term.toCharArray();
         termAtt.setEmpty();
         termAtt.copyBuffer(characters, 0, characters.length);

         return true;
     }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

SDL PLC confidential, all rights reserved.
If you are not the intended recipient of this mail SDL requests and requires that you delete it without acting upon or copying any of its contents, and we further request that you advise us.
SDL PLC is a public limited company registered in England and Wales.  Registered number: 02675207.
Registered address: Globe House, Clivemont Road, Maidenhead, Berkshire SL6 7DY, UK.



This message has been scanned for malware by Websense. www.websense.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: custom token filter generates empty tokens

Ahmet Arslan
In reply to this post by G.Long
Hi G.Long,

You can use TrimFilter+LengthFilter to remove empty/whitespace tokens.


Ahmet

On Thursday, October 9, 2014 5:54 PM, G.Long <[hidden email]> wrote:
Hi :)

I wrote a custom token filter which removes special characters.
Sometimes, all characters of the token are removed so the filter
procudes an empty token. I would like to remove this token from the
tokenstream but i'm not sure how to do that.

Is there something missing in my custom token filter or do I need to
chain another custom token filter to remove empty tokens?

Regards :)

ps:

this is the code of my custom filter :

public class SpecialCharFilter extends TokenFilter {

     private final CharTermAttribute termAtt =
addAttribute(CharTermAttribute.class);

     protected SpecialCharFilter(TokenStream input) {
         super(input);
     }

     @Override
     public boolean incrementToken() throws IOException {

         if (!input.incrementToken()) {
             return false;
         }

         final char[] buffer = termAtt.buffer();
         final int length = termAtt.length();
         final char[] newBuffer = new char[length];

         int newIndex = 0;
         for (int i = 0; i < length; i++) {
             if (!isFilteredChar(buffer[i])) {
                 newBuffer[newIndex] = buffer[i];
                 newIndex++;
             }
         }

         String term = new String(newBuffer);
         term = term.trim();
         char[] characters = term.toCharArray();
         termAtt.setEmpty();
         termAtt.copyBuffer(characters, 0, characters.length);

         return true;
     }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: custom token filter generates empty tokens

G.Long
In reply to this post by Jose Fernandez
Thanks Ahmet and Jose for you help :)

Regards,

Le 09/10/2014 18:29, Jose Fernandez a écrit :

> When you return true from incrementToken() you tell Lucene to add your token to the token stream. At the end of incrementToken() check for an empty token. If it's empty then return incrementToken() to process the next token. This will affect your positions so if you're doing phrase search you will need to adjust the position attribute to account for the now-empty token.
>
> -----Original Message-----
> From: G.Long [mailto:[hidden email]]
> Sent: Thursday, October 09, 2014 7:54 AM
> To: [hidden email]
> Subject: custom token filter generates empty tokens
>
> Hi :)
>
> I wrote a custom token filter which removes special characters.
> Sometimes, all characters of the token are removed so the filter procudes an empty token. I would like to remove this token from the tokenstream but i'm not sure how to do that.
>
> Is there something missing in my custom token filter or do I need to chain another custom token filter to remove empty tokens?
>
> Regards :)
>
> ps:
>
> this is the code of my custom filter :
>
> public class SpecialCharFilter extends TokenFilter {
>
>       private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
>
>       protected SpecialCharFilter(TokenStream input) {
>           super(input);
>       }
>
>       @Override
>       public boolean incrementToken() throws IOException {
>
>           if (!input.incrementToken()) {
>               return false;
>           }
>
>           final char[] buffer = termAtt.buffer();
>           final int length = termAtt.length();
>           final char[] newBuffer = new char[length];
>
>           int newIndex = 0;
>           for (int i = 0; i < length; i++) {
>               if (!isFilteredChar(buffer[i])) {
>                   newBuffer[newIndex] = buffer[i];
>                   newIndex++;
>               }
>           }
>
>           String term = new String(newBuffer);
>           term = term.trim();
>           char[] characters = term.toCharArray();
>           termAtt.setEmpty();
>           termAtt.copyBuffer(characters, 0, characters.length);
>
>           return true;
>       }
> }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
> SDL PLC confidential, all rights reserved.
> If you are not the intended recipient of this mail SDL requests and requires that you delete it without acting upon or copying any of its contents, and we further request that you advise us.
> SDL PLC is a public limited company registered in England and Wales.  Registered number: 02675207.
> Registered address: Globe House, Clivemont Road, Maidenhead, Berkshire SL6 7DY, UK.
>
>
>
> This message has been scanned for malware by Websense. www.websense.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]