|
34 | 34 | import org.apache.lucene.search.Query; |
35 | 35 | import org.apache.lucene.search.SortField; |
36 | 36 | import org.apache.lucene.search.similarities.BM25Similarity; |
| 37 | +import org.apache.lucene.search.similarities.Similarity; |
37 | 38 | import org.apache.lucene.search.similarities.Similarity.SimScorer; |
38 | 39 |
|
39 | 40 | /** |
@@ -262,9 +263,27 @@ static final class LinearFunction extends FeatureFunction { |
262 | 263 | @Override |
263 | 264 | SimScorer scorer(float w) { |
264 | 265 | return new SimScorer() { |
| 266 | + private float doScore(float f) { |
| 267 | + return (w * f); |
| 268 | + } |
| 269 | + |
265 | 270 | @Override |
266 | 271 | public float score(float freq, long norm) { |
267 | | - return (w * decodeFeatureValue(freq)); |
| 272 | + float f = decodeFeatureValue(freq); |
| 273 | + return doScore(f); |
| 274 | + } |
| 275 | + |
| 276 | + @Override |
| 277 | + public Similarity.BulkSimScorer asBulkSimScorer() { |
| 278 | + return new Similarity.BulkSimScorer() { |
| 279 | + @Override |
| 280 | + public void score(int size, float[] freqs, long[] norms, float[] scores) { |
| 281 | + for (int i = 0; i < size; ++i) { |
| 282 | + float f = decodeFeatureValue(freqs[i]); |
| 283 | + scores[i] = doScore(f); |
| 284 | + } |
| 285 | + } |
| 286 | + }; |
268 | 287 | } |
269 | 288 | }; |
270 | 289 | } |
@@ -333,9 +352,27 @@ public String toString() { |
333 | 352 | @Override |
334 | 353 | SimScorer scorer(float weight) { |
335 | 354 | return new SimScorer() { |
| 355 | + private float doScore(float f) { |
| 356 | + return (float) (weight * Math.log(scalingFactor + f)); |
| 357 | + } |
| 358 | + |
336 | 359 | @Override |
337 | 360 | public float score(float freq, long norm) { |
338 | | - return (float) (weight * Math.log(scalingFactor + decodeFeatureValue(freq))); |
| 361 | + float f = decodeFeatureValue(freq); |
| 362 | + return doScore(f); |
| 363 | + } |
| 364 | + |
| 365 | + @Override |
| 366 | + public Similarity.BulkSimScorer asBulkSimScorer() { |
| 367 | + return new Similarity.BulkSimScorer() { |
| 368 | + @Override |
| 369 | + public void score(int size, float[] freqs, long[] norms, float[] scores) { |
| 370 | + for (int i = 0; i < size; ++i) { |
| 371 | + float f = decodeFeatureValue(freqs[i]); |
| 372 | + scores[i] = doScore(f); |
| 373 | + } |
| 374 | + } |
| 375 | + }; |
339 | 376 | } |
340 | 377 | }; |
341 | 378 | } |
@@ -405,14 +442,32 @@ SimScorer scorer(float weight) { |
405 | 442 | } |
406 | 443 | final float pivot = this.pivot; // unbox |
407 | 444 | return new SimScorer() { |
408 | | - @Override |
409 | | - public float score(float freq, long norm) { |
410 | | - float f = decodeFeatureValue(freq); |
| 445 | + |
| 446 | + private float doScore(float f) { |
411 | 447 | // should be f / (f + k) but we rewrite it to |
412 | 448 | // 1 - k / (f + k) to make sure it doesn't decrease |
413 | 449 | // with f in spite of rounding |
414 | 450 | return weight * (1 - pivot / (f + pivot)); |
415 | 451 | } |
| 452 | + |
| 453 | + @Override |
| 454 | + public float score(float freq, long norm) { |
| 455 | + float f = decodeFeatureValue(freq); |
| 456 | + return doScore(f); |
| 457 | + } |
| 458 | + |
| 459 | + @Override |
| 460 | + public Similarity.BulkSimScorer asBulkSimScorer() { |
| 461 | + return new Similarity.BulkSimScorer() { |
| 462 | + @Override |
| 463 | + public void score(int size, float[] freqs, long[] norms, float[] scores) { |
| 464 | + for (int i = 0; i < size; ++i) { |
| 465 | + float f = decodeFeatureValue(freqs[i]); |
| 466 | + scores[i] = doScore(f); |
| 467 | + } |
| 468 | + } |
| 469 | + }; |
| 470 | + } |
416 | 471 | }; |
417 | 472 | } |
418 | 473 |
|
@@ -469,14 +524,31 @@ public String toString() { |
469 | 524 | @Override |
470 | 525 | SimScorer scorer(float weight) { |
471 | 526 | return new SimScorer() { |
472 | | - @Override |
473 | | - public float score(float freq, long norm) { |
474 | | - float f = decodeFeatureValue(freq); |
| 527 | + private float doScore(float f) { |
475 | 528 | // should be f^a / (f^a + k^a) but we rewrite it to |
476 | 529 | // 1 - k^a / (f + k^a) to make sure it doesn't decrease |
477 | 530 | // with f in spite of rounding |
478 | 531 | return (float) (weight * (1 - pivotPa / (Math.pow(f, a) + pivotPa))); |
479 | 532 | } |
| 533 | + |
| 534 | + @Override |
| 535 | + public float score(float freq, long norm) { |
| 536 | + float f = decodeFeatureValue(freq); |
| 537 | + return doScore(f); |
| 538 | + } |
| 539 | + |
| 540 | + @Override |
| 541 | + public Similarity.BulkSimScorer asBulkSimScorer() { |
| 542 | + return new Similarity.BulkSimScorer() { |
| 543 | + @Override |
| 544 | + public void score(int size, float[] freqs, long[] norms, float[] scores) { |
| 545 | + for (int i = 0; i < size; ++i) { |
| 546 | + float f = decodeFeatureValue(freqs[i]); |
| 547 | + scores[i] = doScore(f); |
| 548 | + } |
| 549 | + } |
| 550 | + }; |
| 551 | + } |
480 | 552 | }; |
481 | 553 | } |
482 | 554 |
|
|
0 commit comments