You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2016/01/18 22:32:14 UTC
svn commit: r1725353 - in /lucene/dev/branches/branch_5x: ./ lucene/
lucene/core/
lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.java
lucene/core/src/test/org/apache/lucene/search/similarities/TestBM25Similarity.java
Author: rmuir
Date: Mon Jan 18 21:32:14 2016
New Revision: 1725353
URL: http://svn.apache.org/viewvc?rev=1725353&view=rev
Log:
LUCENE-6982: throw IAE on illegal bm25 parameter values
Modified:
lucene/dev/branches/branch_5x/ (props changed)
lucene/dev/branches/branch_5x/lucene/ (props changed)
lucene/dev/branches/branch_5x/lucene/core/ (props changed)
lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.java
lucene/dev/branches/branch_5x/lucene/core/src/test/org/apache/lucene/search/similarities/TestBM25Similarity.java
Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.java?rev=1725353&r1=1725352&r2=1725353&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.java Mon Jan 18 21:32:14 2016
@@ -46,8 +46,16 @@ public class BM25Similarity extends Simi
* 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.isNaN(k1) || Float.isInfinite(k1) || 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;
}
@@ -59,8 +67,7 @@ public class BM25Similarity extends Simi
* </ul>
*/
public BM25Similarity() {
- this.k1 = 1.2f;
- this.b = 0.75f;
+ this(1.2f, 0.75f);
}
/** Implemented as <code>log(1 + (numDocs - docFreq + 0.5)/(docFreq + 0.5))</code>. */
Modified: lucene/dev/branches/branch_5x/lucene/core/src/test/org/apache/lucene/search/similarities/TestBM25Similarity.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/test/org/apache/lucene/search/similarities/TestBM25Similarity.java?rev=1725353&r1=1725352&r2=1725353&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/test/org/apache/lucene/search/similarities/TestBM25Similarity.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/test/org/apache/lucene/search/similarities/TestBM25Similarity.java Mon Jan 18 21:32:14 2016
@@ -33,4 +33,50 @@ public class TestBM25Similarity extends
}
}
}
+
+ 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"));
+ }
+ }
}