Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2005-2008 Whirlwind Match Limited. All rights reserved. This is open source software; you can use, redistribute and/or modify it under the terms of the Open Software Licence v 3.0 as published by the Open Source Initiative. You should have received a copy of the Open Software Licence along with this application. if not, contact the Open Source Initiative (www.opensource.org) /
 
 package org.fuzzydb.attrs.byteencoding;
 
A scorer to score 100% if two float values are identical, and less if not. TODO: Make less crude!

Author(s):
Neale
 
 public class CompactSimilarFloatValueScorer extends TwoAttrScorer {
 
     private static final long serialVersionUID = 1L;
 
     private static Logger log = LogFactory.getLogger(CompactSimilarFloatValueScorer.class);
 	
 	private float expectedRange;
 	
 	public CompactSimilarFloatValueScorerint scoreAttrIdint otherAttrIdfloat expectedRange ) {
 		super(scoreAttrIdotherAttrId);
 		assertexpectedRange > 0f);
 		this. = expectedRange;
 	}


Calculate the score
 
 	private float scoreFloatsfloat scoreValfloat otherVal ) {
 		// this may be crude: score as the ratio of the smallest vs the highest -> 0.0-1.0 range
 		
 		float diff = Math.absscoreVal - otherVal );
 		
 		if ( diff >=  ) return 0f;
 		
 		float result = 1.0f - (diff / );
 		assertresult <= 1.0f );
 		return result;
 	}
 
     // FIXME Extract relevant bits from score()
     @Override
 	public void scoreSearchToNode(Score scoreScore.Direction dIConstraintMap cIAttributeMap<? extends IAttributescoreAttrs) {
         assertd == . ); // Always the case
         IAttribute attr = scoreAttrs.findAttr();
 		if (attr == null) {
 			return// If we do not have the scorer attr present in the search direction, we do not score - it wasn't 'wanted'
 		}
        	score(scoredattrc);
     }
     
     @Override
         assertd == . ); // Always the case
     	IAttributeConstraint constraint = c.findAttr();
 		if (constraint == null) {
 			return// If we do not have the scorer attr present in the search direction, we do not score - it wasn't 'wanted'
 		}
 
 		if (constraint.isIncludesNotSpecified()) {
 			return;
     	}
 
 		// This is the same attribute in the other container
 		FloatValue other = (FloatValue)searchAttrs.findAttr );
 		if (other == nullreturn// If no matching have, we score 1.0f
 	
 		FloatConstraint fc = (FloatConstraint)constraint;
 		scoreConstraint(scoredotherfc);
     }
    
Implementation that avoids creating any objects. Directly inspects the byte array
 
     @Override
     public void scoreItemToItem(Score scoreDirection dIAttributeMap<IAttributeotherAttrsIAttributeMap<IAttributescoreAttrs) {
    	// We assume that both are CompactAttrMap
    	ByteArray scoreBytes = ((CompactAttrMap<?>)scoreAttrs).getByteArray();
    	ByteArray otherBytes = ((CompactAttrMap<?>)otherAttrs).getByteArray();
    	
    	int scoreIndex = CompactAttrCodec.findAttrInBuf(scoreBytes);
		if (scoreIndex == .) {
			return// If we do not have the scorer attr present in the search direction, we do not score - it wasn't 'wanted'
		}
    	int otherIndex = CompactAttrCodec.findAttrInBuf(otherBytes);
        if (otherIndex == .) {
            score.addNull(thisd);
            return;
        }
        score.add(thiscalcScore(scoreBytesscoreIndexotherBytesotherIndex), d);
    }
	private float calcScore(ByteArray scoreBytesint scoreIndexByteArray otherBytesint otherIndex) {
		float scoreVal = FloatCodec.getValue(scoreBytesscoreIndex);
		float otherVal = FloatCodec.getValue(otherBytesotherIndex);
		return scoreFloatsscoreValotherVal );
	}


    
    
For a node, give the highest possible score. If we're within the bounds of the constraint for the node, then the max poss score is 1.0 else, it's the ration of the distance to the nearest value of the range.

See also:
org.fuzzydb.attrs.Scorer#score(org.fuzzydb.core.marker.internal.IAttribute , likemynds.db.indextree.NodeAttributeContainer )
	private void score(Score scoreScore.Direction dIAttribute scoreAttrIConstraintMap c) {
    	
    	
		if (na == nullreturn;
		if (na.isIncludesNotSpecified()) { // na.hasValue() && 
			score.add(this, 1.0f, d);
			return;
		}
//		if (!na.hasValue()) {
//			return;
//		}
    	
		assertscoreAttr.getAttrId() ==  );
		FloatValue scoreVal = (FloatValuescoreAttr;
		scoreConstraint(scoredscoreValbc);
    }
	private void scoreConstraint(Score scoreScore.Direction dFloatValue scoreValFloatConstraint bc) {
		float s;
		if (bc == null) {
			return;
		}
		if ( bc.consistent(scoreVal) ) {
			s = 1.0f;
		}
		else {
			float branch_min = bc.getMin();
			float branch_max = bc.getMax();
			float myVal = scoreVal.getValue();
			if (myVal < branch_min ) {
				s = scoreFloats(myValbranch_min);
			else if (myVal > branch_max ) {
				s = scoreFloats(myValbranch_max);
			}
			else {
				s = 1.0f;
			}
		}
		score.add(thissd);
	}
	public float getExpectedRange() {
	}
	public void setExpectedRange(float expectedRange) {
		this. = expectedRange;
	}
New to GrepCode? Check out our FAQ X