blob: a6a83dc3c5b2d68b55a29e8d7c9fd48136cfdc78 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.result;
import com.yahoo.text.DoubleFormatter;
/**
* A relevance double value. These values should always be normalized between 0 and 1 (where 1 means perfect),
* however, this is not enforced.
* <p>
* Sources may create subclasses of this to include additional information or functionality.
*
* @author bratseth
*/
public class Relevance implements Comparable<Relevance> {
private static final long serialVersionUID = 4536685722731661704L;
/** The relevancy score. */
private double score;
/**
* Construct a relevancy object with an initial value.
* This initial value should ideally be a normalized value
* between 0 and 1, but that is not enforced.
*
* @param score the inital value (rank score)
*/
public Relevance(double score) {
this.score=score;
}
/**
* Set score value to this value. This should ideally be a
* normalized value between 0 and 1, but that is not enforced.
* NaN is also a legal value, for elements where it makes no sense to assign a particular value.
*/
public void setScore(double score) { this.score = score; }
/**
* Returns the relevancy score of this, preferably a normalized value
* between 0 and 1 but this is not guaranteed by this framework
*/
public double getScore() { return score; }
/**
* Returns the score value as a string
*/
@Override
public String toString() {
return DoubleFormatter.stringValue(score);
}
/** Compares relevancy values with */
public int compareTo(Relevance other) {
double thisScore = getScore();
double otherScore = other.getScore();
if (Double.isNaN(thisScore)) {
if (Double.isNaN(otherScore)) {
return 0;
} else {
return -1;
}
} else if (Double.isNaN(otherScore)) {
return 1;
} else {
return Double.compare(thisScore, otherScore);
}
}
/** Compares relevancy values */
@Override
public boolean equals(Object object) {
if (object==this) return true;
if (!(object instanceof Relevance)) { return false; }
Relevance other = (Relevance) object;
return getScore() == other.getScore();
}
/** Returns a hash from the relevancy value */
@Override
public int hashCode() {
double hash=getScore()*335451367; // A largish prime
if (hash>-1 && hash<1) hash=1/hash;
return (int) hash;
}
}
|