Skip to content

Commit

Permalink
half done work on adding scores
Browse files Browse the repository at this point in the history
  • Loading branch information
John Wang committed Nov 23, 2010
1 parent 38564a3 commit e9818b4
Show file tree
Hide file tree
Showing 23 changed files with 312 additions and 43 deletions.
20 changes: 17 additions & 3 deletions bobo-browse/src/com/browseengine/bobo/api/BrowseFacet.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@ public class BrowseFacet implements Serializable {

private String _value;
private int _hitcount;
private int _score;

public BrowseFacet()
{
public BrowseFacet(){
}

public BrowseFacet(String value,int hitcount)
{
_value=value;
_hitcount=hitcount;
_score = hitcount;
}

public void setFacetValueScore(int score){
_score = score;
}

public int getFacetValueScore(){
return _score;
}

/**
Expand Down Expand Up @@ -92,7 +101,9 @@ public boolean equals(Object obj) {
if (obj instanceof BrowseFacet){
BrowseFacet c2=(BrowseFacet)obj;
if (_hitcount==c2._hitcount && _value.equals(c2._value)){
equals=true;
if (_score==c2._score){
equals=true;
}
}
}
return equals;
Expand All @@ -106,6 +117,9 @@ public List<BrowseFacet> merge(List<BrowseFacet> v,Comparator<BrowseFacet> compa
if (val == 0)
{
facet._hitcount +=this._hitcount;

// TODO: need to do better
facet._score += facet._score;
return v;
}
if (val>0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface ComparatorFactory{
* @param counts hit counts
* @return Comparator instance
*/
IntComparator newComparator(FieldValueAccessor fieldValueAccessor,int[] counts);
IntComparator newComparator(FieldValueAccessor fieldValueAccessor,int[] counts,int[] scores);

/**
* Providers a Comparator. This is called when doing a merge across browses.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public abstract class FacetIterator implements Iterator<Comparable>{

public int count;
public int score;
public Comparable facet;

/**
Expand Down
5 changes: 5 additions & 0 deletions bobo-browse/src/com/browseengine/bobo/api/FacetSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public static enum FacetSortSpec
*/
OrderHitsDesc,

/**
* Order by facet score
*/
OrderScoreDesc,

/**
* custom order, must have a comparator
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public List<BrowseFacet> getFacets()
while((facet = iter.next(minHits)) != null)
{
// find the next facet whose combined hit count obeys minHits
list.add(new BrowseFacet(String.valueOf(facet), iter.count));
BrowseFacet bf = new BrowseFacet(String.valueOf(facet), iter.count);
bf.setFacetValueScore(iter.score);
list.add(bf);
if(++cnt >= maxCnt) break;
}
}
Expand All @@ -96,7 +98,7 @@ else if(FacetSortSpec.OrderHitsDesc.equals(_fspec.getOrderBy()))
{
public int compare(BrowseFacet f1, BrowseFacet f2)
{
int val=f2.getHitCount() - f1.getHitCount();
int val=f2.getFacetValueHitCount() - f1.getFacetValueHitCount();
if (val==0)
{
val = (f1.getValue().compareTo(f2.getValue()));
Expand All @@ -112,20 +114,23 @@ public int compare(BrowseFacet f1, BrowseFacet f2)
int qsize = 0;
while( (qsize < maxCnt) && ((facet = iter.next(minHits)) != null) )
{
queue.add(new BrowseFacet(String.valueOf(facet), iter.count));
BrowseFacet bf = new BrowseFacet(String.valueOf(facet), iter.count);
bf.setFacetValueScore(iter.score);
queue.add(bf);
qsize++;
}
if(facet != null)
{
BrowseFacet rootFacet = (BrowseFacet)queue.top();
minHits = rootFacet.getHitCount() + 1;
minHits = rootFacet.getFacetValueHitCount() + 1;
// facet count less than top of min heap, it will never be added
while(((facet = iter.next(minHits)) != null))
{
rootFacet.setValue(String.valueOf(facet));
rootFacet.setHitCount(iter.count);
rootFacet.setFacetValueHitCount(iter.count);
rootFacet.setFacetValueScore(iter.score);
rootFacet = (BrowseFacet) queue.updateTop();
minHits = rootFacet.getHitCount() + 1;
minHits = rootFacet.getFacetValueHitCount() + 1;
}
}
// at this point, queue contains top maxCnt facets that have hitcount >= minHits
Expand All @@ -138,8 +143,70 @@ public int compare(BrowseFacet f1, BrowseFacet f2)
else
{
// no maxCnt specified. So fetch all facets according to minHits and sort them later
while((facet = iter.next(minHits)) != null)
list.add(new BrowseFacet(String.valueOf(facet), iter.count));
while((facet = iter.next(minHits)) != null){
BrowseFacet bf = new BrowseFacet(String.valueOf(facet), iter.count);
bf.setFacetValueScore(iter.score);
list.add(bf);
}
Collections.sort(list, comparator);
}
}
else if(FacetSortSpec.OrderScoreDesc.equals(_fspec.getOrderBy()))
{
comparator = new Comparator<BrowseFacet>()
{
public int compare(BrowseFacet f1, BrowseFacet f2)
{
int val=f2.getFacetValueScore() - f1.getFacetValueScore();
if (val==0)
{
val = (f1.getValue().compareTo(f2.getValue()));
}
return val;
}
};
if(maxCnt != Integer.MAX_VALUE)
{
// we will maintain a min heap of size maxCnt
// Order by hits in descending order and max count is supplied
PriorityQueue queue = createPQ(maxCnt, comparator);
int qsize = 0;
while( (qsize < maxCnt) && ((facet = iter.next(minHits)) != null) )
{
BrowseFacet bf = new BrowseFacet(String.valueOf(facet), iter.count);
bf.setFacetValueScore(iter.score);
queue.add(bf);
qsize++;
}
if(facet != null)
{
BrowseFacet rootFacet = (BrowseFacet)queue.top();
minHits = rootFacet.getFacetValueHitCount() + 1;
// facet count less than top of min heap, it will never be added
while(((facet = iter.next(minHits)) != null))
{
rootFacet.setValue(String.valueOf(facet));
rootFacet.setFacetValueHitCount(iter.count);
rootFacet.setFacetValueScore(iter.score);
rootFacet = (BrowseFacet) queue.updateTop();
minHits = rootFacet.getFacetValueHitCount() + 1;
}
}
// at this point, queue contains top maxCnt facets that have hitcount >= minHits
while(qsize-- > 0)
{
// append each entry to the beginning of the facet list to order facets by hits descending
list.addFirst((BrowseFacet) queue.pop());
}
}
else
{
// no maxCnt specified. So fetch all facets according to minHits and sort them later
while((facet = iter.next(minHits)) != null){
BrowseFacet bf = new BrowseFacet(String.valueOf(facet), iter.count);
bf.setFacetValueScore(iter.score);
list.add(bf);
}
Collections.sort(list, comparator);
}
}
Expand All @@ -153,16 +220,19 @@ public int compare(BrowseFacet f1, BrowseFacet f2)
int qsize = 0;
while( (qsize < maxCnt) && ((facet = iter.next(minHits)) != null) )
{
queue.add(new BrowseFacet(String.valueOf(facet), iter.count));
BrowseFacet bf = new BrowseFacet(String.valueOf(facet), iter.count);
bf.setFacetValueScore(iter.score);
queue.add(bf);
qsize++;
}
if(facet != null)
{
while((facet = iter.next(minHits)) != null)
{
// check with the top of min heap
browseFacet.setHitCount(iter.count);
browseFacet.setFacetValueHitCount(iter.count);
browseFacet.setValue(String.valueOf(facet));
browseFacet.setFacetValueScore(iter.score);
browseFacet = (BrowseFacet)queue.insertWithOverflow(browseFacet);
}
}
Expand All @@ -173,8 +243,11 @@ public int compare(BrowseFacet f1, BrowseFacet f2)
else
{
// order by custom but no max count supplied
while((facet = iter.next(minHits)) != null)
list.add(new BrowseFacet(String.valueOf(facet), iter.count));
while((facet = iter.next(minHits)) != null){
BrowseFacet bf = new BrowseFacet(String.valueOf(facet), iter.count);
bf.setFacetValueScore(iter.score);
list.add(bf);
}
Collections.sort(list, comparator);
}
}
Expand Down
14 changes: 14 additions & 0 deletions bobo-browse/src/com/browseengine/bobo/facets/FacetHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ private FacetDataNone(){}
private final Set<String> _dependsOn;
private final Map<String,FacetHandler<?>> _dependedFacetHandlers;
private TermCountSize _termCountSize;
private HashMap<String,String> _facetScoringParamMap;

public static enum TermCountSize{
small,
Expand All @@ -61,6 +62,19 @@ public FacetHandler(String name,Set<String> dependsOn)
}
_dependedFacetHandlers = new HashMap<String,FacetHandler<?>>();
_termCountSize = TermCountSize.large;
_facetScoringParamMap = new HashMap<String,String>();
}

public void setFacetScoringParam(String name,String value){
_facetScoringParamMap.put(name, value);
}

public String getFacetScoringParam(String name){
return _facetScoringParamMap.get(name);
}

public Map<String,String> getFacetScoringParamMap(){
return _facetScoringParamMap;
}

public void setTermCountSize(String termCountSize){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ public FacetCountCollectorSource getFacetCountCollectorSource(final BrowseSelect
public FacetCountCollector getFacetCountCollector(BoboIndexReader reader,
int docBase) {
final FacetDataCache dataCache = CompactMultiValueFacetHandler.this.getFacetData(reader);
return new CompactMultiValueFacetCountCollector(_name,sel,dataCache,docBase,ospec);
CompactMultiValueFacetCountCollector collector = new CompactMultiValueFacetCountCollector(_name,sel,dataCache,docBase, ospec);
collector.setFacetScoringParams(CompactMultiValueFacetHandler.this.getFacetScoringParamMap());
return collector;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@ public class DefaultDoubleFacetIterator extends DoubleFacetIterator

public TermDoubleList _valList;
private int[] _count;
private int[] _scores;
private int _countlength;
private int _countLengthMinusOne;
private int _index;

public DefaultDoubleFacetIterator(TermDoubleList valList, int[] countarray, int countlength,
public DefaultDoubleFacetIterator(TermDoubleList valList, int[] countarray, int[] scores, int countlength,
boolean zeroBased)
{
_valList = valList;
_countlength = countlength;
_count = countarray;
_scores = scores;
_countLengthMinusOne = _countlength - 1;
_index = -1;
if (!zeroBased)
_index++;
facet = -1;
count = 0;
score = 0;
}

/*
Expand Down Expand Up @@ -85,6 +88,8 @@ public String next()
_index++;
facet = _valList.getPrimitiveValue(_index);
count = _count[_index];
score = _scores[_index];

return _valList.get(_index);
}

Expand All @@ -98,6 +103,8 @@ public double nextDouble()
_index++;
facet = _valList.getPrimitiveValue(_index);
count = _count[_index];
score = _scores[_index];

return facet;
}

Expand Down Expand Up @@ -125,11 +132,14 @@ public String next(int minHits)
{
facet = _valList.getPrimitiveValue(_index);
count = _count[_index];
score = _scores[_index];

return _valList.format(facet);
}
}
facet = -1;
count = 0;
score =0;
return null;
}

Expand All @@ -144,11 +154,14 @@ public double nextDouble(int minHits)
{
facet = _valList.getPrimitiveValue(_index);
count = _count[_index];
score = _scores[_index];

return facet;
}
}
facet = -1;
count = 0;
score = 0;
return facet;
}
}
Loading

0 comments on commit e9818b4

Please sign in to comment.