mirror of https://github.com/apache/lucene.git
LUCENE-6982: throw IAE on illegal bm25 parameter values
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1725350 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
eafdf74bdd
commit
fb0aa2dc49
|
@ -44,8 +44,16 @@ public class BM25Similarity extends Similarity {
|
|||
* BM25 with the supplied parameter values.
|
||||
* @param k1 Controls non-linear term frequency normalization (saturation).
|
||||
* @param b Controls to what degree document length normalizes tf values.
|
||||
* @throws IllegalArgumentException if {@code k1} is infinite or negative, or if {@code b} is
|
||||
* not within the range {@code [0..1]}
|
||||
*/
|
||||
public BM25Similarity(float k1, float b) {
|
||||
if (Float.isFinite(k1) == false || k1 < 0) {
|
||||
throw new IllegalArgumentException("illegal k1 value: " + k1 + ", must be a non-negative finite value");
|
||||
}
|
||||
if (Float.isNaN(b) || b < 0 || b > 1) {
|
||||
throw new IllegalArgumentException("illegal b value: " + b + ", must be between 0 and 1");
|
||||
}
|
||||
this.k1 = k1;
|
||||
this.b = b;
|
||||
}
|
||||
|
@ -57,8 +65,7 @@ public class BM25Similarity extends Similarity {
|
|||
* </ul>
|
||||
*/
|
||||
public BM25Similarity() {
|
||||
this.k1 = 1.2f;
|
||||
this.b = 0.75f;
|
||||
this(1.2f, 0.75f);
|
||||
}
|
||||
|
||||
/** Implemented as <code>log(1 + (docCount - docFreq + 0.5)/(docFreq + 0.5))</code>. */
|
||||
|
|
|
@ -33,4 +33,50 @@ public class TestBM25Similarity extends LuceneTestCase {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testIllegalK1() {
|
||||
try {
|
||||
new BM25Similarity(Float.POSITIVE_INFINITY, 0.75f);
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(expected.getMessage().contains("illegal k1 value"));
|
||||
}
|
||||
|
||||
try {
|
||||
new BM25Similarity(-1, 0.75f);
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(expected.getMessage().contains("illegal k1 value"));
|
||||
}
|
||||
|
||||
try {
|
||||
new BM25Similarity(Float.NaN, 0.75f);
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(expected.getMessage().contains("illegal k1 value"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testIllegalB() {
|
||||
try {
|
||||
new BM25Similarity(1.2f, 2f);
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(expected.getMessage().contains("illegal b value"));
|
||||
}
|
||||
|
||||
try {
|
||||
new BM25Similarity(1.2f, -1f);
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(expected.getMessage().contains("illegal b value"));
|
||||
}
|
||||
|
||||
try {
|
||||
new BM25Similarity(1.2f, Float.POSITIVE_INFINITY);
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(expected.getMessage().contains("illegal b value"));
|
||||
}
|
||||
|
||||
try {
|
||||
new BM25Similarity(1.2f, Float.NaN);
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(expected.getMessage().contains("illegal b value"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue