forked from javasoze/bobo
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added scorer building and missing files
- Loading branch information
John Wang
committed
Jun 8, 2010
1 parent
c50cf7e
commit 2df385e
Showing
3 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
bobo-browse/src/com/browseengine/bobo/query/RecencyBoostScorerBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.browseengine.bobo.query; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.apache.lucene.index.IndexReader; | ||
import org.apache.lucene.search.Explanation; | ||
import org.apache.lucene.search.Scorer; | ||
|
||
import com.browseengine.bobo.api.BoboIndexReader; | ||
import com.browseengine.bobo.facets.data.FacetDataCache; | ||
import com.browseengine.bobo.facets.data.TermLongList; | ||
import com.browseengine.bobo.util.BigSegmentedArray; | ||
|
||
public class RecencyBoostScorerBuilder implements ScorerBuilder { | ||
|
||
private final float _maxFactor; | ||
private final TimeUnit _timeunit; | ||
private final float _min; | ||
private final float _max; | ||
private final long _cutoffInMillis; | ||
private final float _A; | ||
private final String _timeFacetName; | ||
private final long _now; | ||
|
||
public RecencyBoostScorerBuilder(String timeFacetName,float maxFactor,long cutoff,TimeUnit timeunit) { | ||
_timeFacetName = timeFacetName; | ||
_maxFactor = maxFactor; | ||
_min = 1.0f; | ||
_max = _maxFactor + _min; | ||
_timeunit = timeunit; | ||
_cutoffInMillis = _timeunit.toMillis(cutoff); | ||
_A = (_min - _max) / (((float)_cutoffInMillis)*((float)_cutoffInMillis)); | ||
_now = System.currentTimeMillis(); | ||
} | ||
|
||
public Explanation explain(IndexReader reader, int doc, | ||
Explanation innerExplaination) throws IOException { | ||
if (reader instanceof BoboIndexReader){ | ||
BoboIndexReader boboReader = (BoboIndexReader)reader; | ||
Object dataObj = boboReader.getFacetData(_timeFacetName); | ||
if (dataObj instanceof FacetDataCache<?>){ | ||
FacetDataCache<Long> facetDataCache = (FacetDataCache<Long>)(boboReader.getFacetData(_timeFacetName)); | ||
final BigSegmentedArray orderArray = facetDataCache.orderArray; | ||
final TermLongList termList = (TermLongList)facetDataCache.valArray; | ||
final long now = System.currentTimeMillis(); | ||
Explanation finalExpl = new Explanation(); | ||
finalExpl.addDetail(innerExplaination); | ||
float rawScore = innerExplaination.getValue(); | ||
long timeVal = termList.getRawValue(orderArray.get(doc)); | ||
float timeScore = computeTimeFactor(timeVal); | ||
float finalScore = combineScores(timeScore,rawScore); | ||
finalExpl.setValue(finalScore); | ||
finalExpl.setDescription("final score = (time score: "+timeScore+") * (raw score: "+rawScore+"), timeVal: "+timeVal); | ||
return finalExpl; | ||
} | ||
else{ | ||
throw new IllegalStateException("underlying facet data must be of type FacetDataCache<Long>"); | ||
} | ||
} | ||
else{ | ||
throw new IllegalStateException("reader not instance of "+BoboIndexReader.class); | ||
} | ||
} | ||
|
||
public Scorer createScorer(final Scorer innerScorer, IndexReader reader, | ||
boolean scoreDocsInOrder, boolean topScorer) throws IOException { | ||
if (reader instanceof BoboIndexReader){ | ||
BoboIndexReader boboReader = (BoboIndexReader)reader; | ||
Object dataObj = boboReader.getFacetData(_timeFacetName); | ||
if (dataObj instanceof FacetDataCache<?>){ | ||
FacetDataCache<Long> facetDataCache = (FacetDataCache<Long>)(boboReader.getFacetData(_timeFacetName)); | ||
final BigSegmentedArray orderArray = facetDataCache.orderArray; | ||
final TermLongList termList = (TermLongList)facetDataCache.valArray; | ||
return new Scorer(innerScorer.getSimilarity()){ | ||
|
||
|
||
@Override | ||
public float score() throws IOException { | ||
float rawScore = innerScorer.score(); | ||
long timeVal = termList.getRawValue(orderArray.get(innerScorer.docID())); | ||
float timeScore = computeTimeFactor(timeVal); | ||
return combineScores(timeScore,rawScore); | ||
} | ||
|
||
}; | ||
} | ||
else{ | ||
throw new IllegalStateException("underlying facet data must be of type FacetDataCache<Long>"); | ||
} | ||
} | ||
else{ | ||
throw new IllegalStateException("reader not instance of "+BoboIndexReader.class); | ||
} | ||
} | ||
|
||
private float computeTimeFactor(long timeVal){ | ||
long xVal = _now - timeVal; | ||
if (xVal > _cutoffInMillis){ | ||
return _min; | ||
} | ||
else{ | ||
float xValFloat = (float)xVal; | ||
return _A*xValFloat*xValFloat+_max; | ||
} | ||
} | ||
|
||
private static float combineScores(float timeScore,float rawScore){ | ||
return timeScore * rawScore; | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
bobo-browse/src/com/browseengine/bobo/query/ScoreAdjusterQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.browseengine.bobo.query; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.lucene.index.IndexReader; | ||
import org.apache.lucene.search.Explanation; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.Scorer; | ||
import org.apache.lucene.search.Searcher; | ||
import org.apache.lucene.search.Weight; | ||
|
||
public abstract class ScoreAdjusterQuery extends Query | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
private class ScoreAdjusterWeight extends Weight | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
Weight _innerWeight; | ||
|
||
public ScoreAdjusterWeight(Weight innerWeight) throws IOException | ||
{ | ||
_innerWeight = innerWeight; | ||
} | ||
|
||
public String toString() | ||
{ | ||
return "weight(" + ScoreAdjusterQuery.this + ")"; | ||
} | ||
|
||
public Query getQuery() | ||
{ | ||
return _innerWeight.getQuery(); | ||
} | ||
|
||
public float getValue() | ||
{ | ||
return _innerWeight.getValue(); | ||
} | ||
|
||
public float sumOfSquaredWeights() throws IOException | ||
{ | ||
return _innerWeight.sumOfSquaredWeights(); | ||
} | ||
|
||
public void normalize(float queryNorm) | ||
{ | ||
_innerWeight.normalize(queryNorm); | ||
} | ||
|
||
@Override | ||
public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException | ||
{ | ||
Scorer innerScorer = _innerWeight.scorer(reader, scoreDocsInOrder, topScorer); | ||
return _scorerBuilder.createScorer(innerScorer, reader, scoreDocsInOrder, topScorer); | ||
} | ||
|
||
public Explanation explain(IndexReader reader, int doc) throws IOException | ||
{ | ||
Explanation innerExplain = _innerWeight.explain(reader, doc); | ||
return _scorerBuilder.explain(reader, doc,innerExplain); | ||
} | ||
} | ||
|
||
protected final Query _query; | ||
protected final ScorerBuilder _scorerBuilder; | ||
public ScoreAdjusterQuery(Query query,ScorerBuilder scorerBuilder) | ||
{ | ||
_query = query; | ||
_scorerBuilder = scorerBuilder; | ||
} | ||
|
||
@Override | ||
public Weight createWeight(Searcher searcher) throws IOException | ||
{ | ||
return new ScoreAdjusterWeight(_query.createWeight(searcher)); | ||
} | ||
|
||
@Override | ||
public Query rewrite(IndexReader reader) throws IOException | ||
{ | ||
_query.rewrite(reader); | ||
return this; | ||
} | ||
|
||
@Override | ||
public String toString(String field) | ||
{ | ||
return _query.toString(field); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
bobo-browse/src/com/browseengine/bobo/query/ScorerBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.browseengine.bobo.query; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.lucene.index.IndexReader; | ||
import org.apache.lucene.search.Explanation; | ||
import org.apache.lucene.search.Scorer; | ||
|
||
public interface ScorerBuilder { | ||
Scorer createScorer(Scorer innerScorer, IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException; | ||
Explanation explain(IndexReader reader,int doc,Explanation innerExplaination) throws IOException; | ||
} |