You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by David Byrne <da...@gmail.com> on 2011/03/03 15:36:14 UTC

Unintuitive NGramTokenizer behavior

I have a minor quibble about Lucene's NGramTokenizer.

Before I tokenize my strings, I am padding them with white space:

String foobar = " " + foo + " " + bar + " ";

When constructing term vectors from ngrams, this strategy has a couple
benefits.  First, it places special emphasis on the starting and ending of a
word.  Second, it improves the similarity between phrases with swapped
words.  " foo bar " matches " bar foo " more closely than "foo bar" matches
"bar foo".

The problem is that Lucene's NGramTokenizer trims whitespace.  This forces
me to do some preprocessing on my strings before I can tokenize them:

foobar.replaceAll(" ","$"); //arbitrary char not in my data

This is undocumented, so users won't realize their strings are being
trim()'ed, unless they look through the source, or examine the tokens
manually.

I am proposing NGramTokenizer should be changed to respect whitespace.  Is
there a compelling reason against this?

-Dave

Re: Unintuitive NGramTokenizer behavior

Posted by Robert Muir <rc...@gmail.com>.
On Thu, Mar 3, 2011 at 2:06 PM, Grant Ingersoll <gs...@apache.org> wrote:
>
> On Mar 3, 2011, at 1:10 PM, Robert Muir wrote:
>
>> On Thu, Mar 3, 2011 at 1:00 PM, Grant Ingersoll <gs...@apache.org> wrote:
>>>
>>> Unfortunately, I'm not following your reasons for doing it.  I won't say I'm
>>> against it at this point, but I don't see a compelling reason to change it
>>> either so if you could clarify that would be great.  It's been around for
>>> quite some time in it's current form and I think fits most people's
>>> expectations of ngrams.
>>
>> Grant I'm sorry, but I couldnt disagree more.
>>
>> There are many variations on ngram tokenization (word-internal,
>> word-spanning, skipgrams), besides allowing flexibility for what
>> should be a "word character" and what should not be (e.g.
>> punctuation), and how to handle the specifics of these.
>>
>> But our n-gram tokenizer is *UNARGUABLY* completely broken for these reasons:
>> 1. it discards anything after the first 1024 code units of the document.
>> 2. it uses partial characters (UTF-16 code units) as its fundamental
>> measure, potentially creating lots of invalid unicode.
>> 3. it forms n-grams in the wrong order, contributing to #1. I
>> explained this in LUCENE-1224
>
> Sure, but those are ancillary to the whitespace question that was asked about.
>

Not really? its the more general form of the whitespace question.

I'm saying you should be able to say 'this is part of a word', but
then also specify if you want to fold runs of "non-characters" into a
single thing (e.g. '_') or into nothing at all, or whatever.

Additionally NGramTokenizer should also support option to treat
"start" and "end" of string as "non-characters"... in my opinion this
should be the default and is the root cause of Dave's issue?

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


Re: Unintuitive NGramTokenizer behavior

Posted by Grant Ingersoll <gs...@apache.org>.
On Mar 3, 2011, at 1:10 PM, Robert Muir wrote:

> On Thu, Mar 3, 2011 at 1:00 PM, Grant Ingersoll <gs...@apache.org> wrote:
>> 
>> Unfortunately, I'm not following your reasons for doing it.  I won't say I'm
>> against it at this point, but I don't see a compelling reason to change it
>> either so if you could clarify that would be great.  It's been around for
>> quite some time in it's current form and I think fits most people's
>> expectations of ngrams.
> 
> Grant I'm sorry, but I couldnt disagree more.
> 
> There are many variations on ngram tokenization (word-internal,
> word-spanning, skipgrams), besides allowing flexibility for what
> should be a "word character" and what should not be (e.g.
> punctuation), and how to handle the specifics of these.
> 
> But our n-gram tokenizer is *UNARGUABLY* completely broken for these reasons:
> 1. it discards anything after the first 1024 code units of the document.
> 2. it uses partial characters (UTF-16 code units) as its fundamental
> measure, potentially creating lots of invalid unicode.
> 3. it forms n-grams in the wrong order, contributing to #1. I
> explained this in LUCENE-1224

Sure, but those are ancillary to the whitespace question that was asked about.

> 
> Its these reasons that I suggested we completely rewrite it... people
> that are just indexing english documents with < 1024 chars per
> document and don't care about these things can use
> ClassicNGramTokenizer.


Fair enough.  Always open to improvements.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


Re: Unintuitive NGramTokenizer behavior

Posted by Robert Muir <rc...@gmail.com>.
On Thu, Mar 3, 2011 at 1:00 PM, Grant Ingersoll <gs...@apache.org> wrote:
>
> Unfortunately, I'm not following your reasons for doing it.  I won't say I'm
> against it at this point, but I don't see a compelling reason to change it
> either so if you could clarify that would be great.  It's been around for
> quite some time in it's current form and I think fits most people's
> expectations of ngrams.

Grant I'm sorry, but I couldnt disagree more.

There are many variations on ngram tokenization (word-internal,
word-spanning, skipgrams), besides allowing flexibility for what
should be a "word character" and what should not be (e.g.
punctuation), and how to handle the specifics of these.

But our n-gram tokenizer is *UNARGUABLY* completely broken for these reasons:
1. it discards anything after the first 1024 code units of the document.
2. it uses partial characters (UTF-16 code units) as its fundamental
measure, potentially creating lots of invalid unicode.
3. it forms n-grams in the wrong order, contributing to #1. I
explained this in LUCENE-1224

Its these reasons that I suggested we completely rewrite it... people
that are just indexing english documents with < 1024 chars per
document and don't care about these things can use
ClassicNGramTokenizer.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


Re: Unintuitive NGramTokenizer behavior

Posted by David Byrne <da...@gmail.com>.
Grant,

To explain the advantage:

Trigrams for "foo bar": 'foo', 'oo ', 'o b', ' ba', 'bar'
Trigrams for "bar foo": 'bar', 'ar ', 'r f', ' fo', 'foo'

Only two out of eight unique trigrams match.

Trigrams for " foo bar ": ' fo', 'foo', 'oo ', 'o b', ' ba', 'bar', 'ar '
Trigrams for " bar foo ": ' ba', 'bar', 'ar ', 'r f', ' fo', 'foo', 'oo '

Six out of eight unique trigrams match.

I can't do the character replacement up front, because foo and bar might
already contain whitespace as well.  Anyways, its a hack, and if my
arbitrary character ever gets introduced into the data I am in trouble.

Not only is this undocumented, but it seems unintentional if you look at the
comments in the code.

FYI, I opened up an issue regarding this: http://bit.ly/eqhTO1
 On Mar 3, 2011 1:00 PM, "Grant Ingersoll" <gs...@apache.org> wrote:
>
> On Mar 3, 2011, at 9:36 AM, David Byrne wrote:
>
>> I have a minor quibble about Lucene's NGramTokenizer.
>>
>> Before I tokenize my strings, I am padding them with white space:
>>
>> String foobar = " " + foo + " " + bar + " ";
>>
>> When constructing term vectors from ngrams, this strategy has a couple
benefits. First, it places special emphasis on the starting and ending of a
word. Second, it improves the similarity between phrases with swapped words.
" foo bar " matches " bar foo " more closely than "foo bar" matches "bar
foo".
>>
>>
>
> I'm not following this argument. What does the extra whitespace give you
here?
>
>> The problem is that Lucene's NGramTokenizer trims whitespace. This forces
me to do some preprocessing on my strings before I can tokenize them:
>>
>> foobar.replaceAll(" ","$"); //arbitrary char not in my data
>>
>>
>
> I'm confused. If you are padding them up front, then why don't you just do
the arbitrary char trick then? Where is the extra processing?
>
>> This is undocumented, so users won't realize their strings are being
trim()'ed, unless they look through the source, or examine the tokens
manually.
>>
>>
>
> It may be undocumented, but I think it is pretty standard as to what users
expect out of a tokenizer.
>
>> I am proposing NGramTokenizer should be changed to respect whitespace. Is
there a compelling reason against this?
>>
>>
>
> Unfortunately, I'm not following your reasons for doing it. I won't say
I'm against it at this point, but I don't see a compelling reason to change
it either so if you could clarify that would be great. It's been around for
quite some time in it's current form and I think fits most people's
expectations of ngrams.
>
> -Grant

Re: Unintuitive NGramTokenizer behavior

Posted by Grant Ingersoll <gs...@apache.org>.
On Mar 3, 2011, at 9:36 AM, David Byrne wrote:

> I have a minor quibble about Lucene's NGramTokenizer.
> 
> Before I tokenize my strings, I am padding them with white space:
> 
> String foobar = " " + foo + " " + bar + " ";
> 
> When constructing term vectors from ngrams, this strategy has a couple benefits.  First, it places special emphasis on the starting and ending of a word.  Second, it improves the similarity between phrases with swapped words.  " foo bar " matches " bar foo " more closely than "foo bar" matches "bar foo".
> 
> 

I'm not following this argument.  What does the extra whitespace give you here?  

> The problem is that Lucene's NGramTokenizer trims whitespace.  This forces me to do some preprocessing on my strings before I can tokenize them:
> 
> foobar.replaceAll(" ","$"); //arbitrary char not in my data
> 
> 

I'm confused.  If you are padding them up front, then why don't you just do the arbitrary char trick then?  Where is the extra processing?

> This is undocumented, so users won't realize their strings are being trim()'ed, unless they look through the source, or examine the tokens manually.
> 
> 

It may be undocumented, but I think it is pretty standard as to what users expect out of a tokenizer.

> I am proposing NGramTokenizer should be changed to respect whitespace.  Is there a compelling reason against this?
> 
> 

Unfortunately, I'm not following your reasons for doing it.  I won't say I'm against it at this point, but I don't see a compelling reason to change it either so if you could clarify that would be great.  It's been around for quite some time in it's current form and I think fits most people's expectations of ngrams.

-Grant