You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Israel Tsadok <it...@gmail.com> on 2008/12/25 14:20:27 UTC
Any way to ignore repeated terms in TF calculation?
A recurring problem I have with Lucene results is when a document contains
the same word over and over again. If for some reason I have a document
containing "badger badger badger badger badger badger badger badger", it
would appear high on the search results for "badger", even though it's
usually a useless document.
What I would like to do is ignore repeating words when counting the term
frequency. At first, I thought I could achieve this by indexing with a
TokenFilter that would skip repeated tokens, but then a search for e.g.
"Rochelle Rochelle" would return no results.
What I would like is to index all 8 "badger"s, but have the frequency of
"badger" saved as 1. Is that even possible?
Digging around in Lucene code, I found term frequency calculations
in FreqProxTermsWriterPerField.addTerm() - is that where I need to look?
Any help would be appreciated.
Israel
Re: Any way to ignore repeated terms in TF calculation?
Posted by Paul Libbrecht <pa...@activemath.org>.
Shouldn't your analyzer also convert "Rochelle Rochelle" to "Rochelle" ?
paul
Le 25-déc.-08 à 14:20, Israel Tsadok a écrit :
> A recurring problem I have with Lucene results is when a document
> contains
> the same word over and over again. If for some reason I have a
> document
> containing "badger badger badger badger badger badger badger
> badger", it
> would appear high on the search results for "badger", even though it's
> usually a useless document.
> What I would like to do is ignore repeating words when counting the
> term
> frequency. At first, I thought I could achieve this by indexing with a
> TokenFilter that would skip repeated tokens, but then a search for
> e.g.
> "Rochelle Rochelle" would return no results.
>
> What I would like is to index all 8 "badger"s, but have the
> frequency of
> "badger" saved as 1. Is that even possible?
>
> Digging around in Lucene code, I found term frequency calculations
> in FreqProxTermsWriterPerField.addTerm() - is that where I need to
> look?
>
> Any help would be appreciated.
> Israel
Re: Any way to ignore repeated terms in TF calculation?
Posted by Chris Hostetter <ho...@fucit.org>.
: you can solve your problem at search time by passing a custom Similarity class
In particular, consider subclassing SeweetSpotSimilrity ... instead of a
truely "flat" tf function, it makes it easy for you to define a
"sweetspot" so 2 instances of a word can score a lot higher then 1
instance, and 3 can score slightly higher then 2, but 3-1000 instances can
all score equivilently.
-Hoss
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org
Re: Any way to ignore repeated terms in TF calculation?
Posted by Chris Hostetter <ho...@fucit.org>.
: This is not quite what I was talking about. I was talking about documents
: with a single field. I want the text "Badgers are mammals. Badgers are cute"
: to score higher than the text "Badger Badger" for the term query
: "text:badger".
: Ideally, what I want is to add another factor to the scoring at index time,
: a "sparsity factor" which should cancel out the term frequency as the
: average distance between terms nears 1.
something else you my want to consider: you can omitNorms (or alter the
lengthNorm function) when indexing so that longer fields aren't penalized
compared to shorter fields ... in which case a field containing "Badger
Badger" won't score *higher* then "Badgers are mammals. Badgers are cute"
because it won't get the short lengthNorm bonus ... if it met your use
case, you could even make *longer* docs get a higher lengthNorm.
: Sorry about the weird math, I just mean (as I said above) that the sparsity
: factor should cancel out the tf completely if avg_d<=1 and become 1 as avg_d
: gets larger.
it wouldn't exactly match your match, but a simpler approach to consider
that might be equally effective would be counting the number of unique
terms in this field at index time (or the ratio of unique terms to total
terms), and then use that number as the fieldBoost (or index as a numeric
field that you build a function query on) ... then you can reward docs
that have a higher number of unique terms, and penalize docs that only
have a few terms repeated over and over.
-Hoss
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org
Re: Any way to ignore repeated terms in TF calculation?
Posted by Israel Tsadok <it...@gmail.com>.
Hi Umesh,
> I am trying to put the problem more concisely.
> 1. Fields where term frequency is very very relevant. E.g.
> Body:
> Example:
> if TF of badger in Body of doc 1 > TF of badger in Body of doc 2
> doc 1 scores higher.
>
> 2. Fields where term frequency is irrevalent
> Page_Title:
> Example:
> TF of badger in PageTitle doesn't affect the score.
>
This is not quite what I was talking about. I was talking about documents
with a single field. I want the text "Badgers are mammals. Badgers are cute"
to score higher than the text "Badger Badger" for the term query
"text:badger".
Ideally, what I want is to add another factor to the scoring at index time,
a "sparsity factor" which should cancel out the term frequency as the
average distance between terms nears 1.
i.e. if the score formula is:
score(q,d) = coord(q,d) x queryNorm(q) x sigma(t in q) of ( tf(t in d) x
idf(t)^2 x t.getBoost() x norm(t,d) )
I want to make it:
score(q,d) = coord(q,d) x queryNorm(q) x sigma(t in q) of ( tf(t in d) x
idf(t)^2 x t.getBoost() x norm(t,d) x sparsity(t in d) )
where
sparsity(t in d) = 1 / (1 + ( tf(t in d) - 1) / (1 + e ^ (avg_d(t in d) -
7)))
where
avg_d(t in d) = average distance between terms t in document d
Sorry about the weird math, I just mean (as I said above) that the sparsity
factor should cancel out the tf completely if avg_d<=1 and become 1 as avg_d
gets larger.
I looked at Similarity.computeNorm(), which may make it possible for me to
add this inside the normalization value, but I'm not sure if that's really
possible, plus the method is not available yet in 2.4.
Having unloaded all that off my chest, I have to say that I really like your
proposal, and it might solve 90% of my problems without resorting to my
overreaching redesign of Lucene core...
If that is the case:
> then one solution is
> 1. Build the query programmatically.
> 2. Form Normal Queries on FieldType 1 ( e.g. Body)
> 3. Form ConstantScore variation of queries on FieldType 2 (e.g. Page_Title,
> ConstantScoreTermQuery)
>
> There is no need to change anything at index time.
>
OK, so I really like this. The only problem is that it's not going to be
easy to build the query programmatically, since currently I'm using
QueryParser with a little help from MultiFieldQueryParser and
PerFieldAnalyzerWrapper.
I think that the best course of action would be to subclass
MultiFieldQueryParser so that for Body fields it will behave normally, but
for Page_Title fields it will emit a ConstantScoreQuery wrapping the
original field query.
Can you think of an easier way to do this?
Thanks,
Israel
Re: Any way to ignore repeated terms in TF calculation?
Posted by Umesh Prasad <um...@gmail.com>.
Hi Israel,
I am trying to put the problem more concisely.
1. Fields where term frequency is very very relevant. E.g.
Body:
Example:
if TF of badger in Body of doc 1 > TF of badger in Body of doc 2
doc 1 scores higher.
2. Fields where term frequency is irrevalent
Page_Title:
Example:
TF of badger in PageTitle doesn't affect the score.
If that is the case:
then one solution is
1. Build the query programmatically.
2. Form Normal Queries on FieldType 1 ( e.g. Body)
3. Form ConstantScore variation of queries on FieldType 2 (e.g. Page_Title,
ConstantScoreTermQuery)
There is no need to change anything at index time.
I hope that helps.
Thanks
Umesh
On Sun, Jan 11, 2009 at 8:30 PM, Israel Tsadok <it...@gmail.com> wrote:
> >
> > you can solve your problem at search time by passing a custom Similarity
> > class that looks something like this:
> >
> > private Similarity similarity = new DefaultSimilarity() {
> >> public float tf(float v) {
> >> return 1f;
> >> }
> >> public float tf(int i) {
> >> return 1f;
> >> }
> >> };
> >>
> >
> Thanks, but it seems that this solution would make all words completely
> equal without regard to their frequency. This is more extreme than what I
> had in mind. Chris Hostetter's suggestion of SweetSpotSimilarity makes the
> situation a little better, but still doesn't make the distinction between
> repeated words and words that appear in different location in the text.
>
> For example, an encyclopedic article about badgers would probably have the
> word "badger" many times throughout its text. I would like to make such
> article score much higher than an unrelated article that simply used the
> word badger three times in 800 words. Term Frequency works well in this
> regard, but fails to make the encyclopedic article rank higher than
> documents that simply contain the word "badger" and not much else (
> http://tinyurl.com/8p5jsj).
>
> Paul Libberecht's comment has a point - if I eliminate duplicates in the
> tokenizer both at indexing time and in the query parser, I should be able
> to
> make search work with reduced effect for repeated terms. However, that
> approach has two downsides:
> 1. It will be impossible to find articles with (specifically) "badger
> badger
> badger" in them.
> 2. Sometimes two words are repeated (barack obama barack obama barack
> obama)
> which makes the tokenizer approach unsuitable.
>
> Another option I'm considering is a negative boost to documents that
> contain
> repeated terms, but this is too general, since such a document may be very
> relevant to searches about different terms. I really only want to change
> the
> tf of the offending repeated term.
>
> Thanks for all your suggestions, and I'd appreciate any other ideas.
>
> Israel
>
Re: Any way to ignore repeated terms in TF calculation?
Posted by Israel Tsadok <it...@gmail.com>.
>
> you can solve your problem at search time by passing a custom Similarity
> class that looks something like this:
>
> private Similarity similarity = new DefaultSimilarity() {
>> public float tf(float v) {
>> return 1f;
>> }
>> public float tf(int i) {
>> return 1f;
>> }
>> };
>>
>
Thanks, but it seems that this solution would make all words completely
equal without regard to their frequency. This is more extreme than what I
had in mind. Chris Hostetter's suggestion of SweetSpotSimilarity makes the
situation a little better, but still doesn't make the distinction between
repeated words and words that appear in different location in the text.
For example, an encyclopedic article about badgers would probably have the
word "badger" many times throughout its text. I would like to make such
article score much higher than an unrelated article that simply used the
word badger three times in 800 words. Term Frequency works well in this
regard, but fails to make the encyclopedic article rank higher than
documents that simply contain the word "badger" and not much else (
http://tinyurl.com/8p5jsj).
Paul Libberecht's comment has a point - if I eliminate duplicates in the
tokenizer both at indexing time and in the query parser, I should be able to
make search work with reduced effect for repeated terms. However, that
approach has two downsides:
1. It will be impossible to find articles with (specifically) "badger badger
badger" in them.
2. Sometimes two words are repeated (barack obama barack obama barack obama)
which makes the tokenizer approach unsuitable.
Another option I'm considering is a negative boost to documents that contain
repeated terms, but this is too general, since such a document may be very
relevant to searches about different terms. I really only want to change the
tf of the offending repeated term.
Thanks for all your suggestions, and I'd appreciate any other ideas.
Israel
Re: Any way to ignore repeated terms in TF calculation?
Posted by Karl Wettin <ka...@gmail.com>.
Hi Israel,
you can solve your problem at search time by passing a custom
Similarity class that looks something like this:
> private Similarity similarity = new DefaultSimilarity() {
> public float tf(float v) {
> return 1f;
> }
> public float tf(int i) {
> return 1f;
> }
> };
See javadocs for details.
karl
25 dec 2008 kl. 14.20 skrev Israel Tsadok:
> A recurring problem I have with Lucene results is when a document
> contains
> the same word over and over again. If for some reason I have a
> document
> containing "badger badger badger badger badger badger badger
> badger", it
> would appear high on the search results for "badger", even though it's
> usually a useless document.
> What I would like to do is ignore repeating words when counting the
> term
> frequency. At first, I thought I could achieve this by indexing with a
> TokenFilter that would skip repeated tokens, but then a search for
> e.g.
> "Rochelle Rochelle" would return no results.
>
> What I would like is to index all 8 "badger"s, but have the
> frequency of
> "badger" saved as 1. Is that even possible?
>
> Digging around in Lucene code, I found term frequency calculations
> in FreqProxTermsWriterPerField.addTerm() - is that where I need to
> look?
>
> Any help would be appreciated.
> Israel
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org