Skip to content

Commit b039360

Browse files
committed
Merge pull request #16130 from cbuescher/refactor-smoothingModel
Enable Writeable serialization and parsing from xContent for SmoothingModels
2 parents f5e89f7 + aefdee1 commit b039360

File tree

10 files changed

+660
-24
lines changed

10 files changed

+660
-24
lines changed

core/src/main/java/org/elasticsearch/search/suggest/phrase/LaplaceScorer.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,26 @@
2727
import java.io.IOException;
2828
//TODO public for tests
2929
public final class LaplaceScorer extends WordScorer {
30-
30+
3131
public static final WordScorerFactory FACTORY = new WordScorer.WordScorerFactory() {
3232
@Override
3333
public WordScorer newScorer(IndexReader reader, Terms terms, String field, double realWordLikelyhood, BytesRef separator) throws IOException {
3434
return new LaplaceScorer(reader, terms, field, realWordLikelyhood, separator, 0.5);
3535
}
3636
};
37-
37+
3838
private double alpha;
3939

4040
public LaplaceScorer(IndexReader reader, Terms terms, String field,
4141
double realWordLikelyhood, BytesRef separator, double alpha) throws IOException {
4242
super(reader, terms, field, realWordLikelyhood, separator);
4343
this.alpha = alpha;
4444
}
45-
45+
46+
double alpha() {
47+
return this.alpha;
48+
}
49+
4650
@Override
4751
protected double scoreBigram(Candidate word, Candidate w_1) throws IOException {
4852
SuggestUtils.join(separator, spare, w_1.term, word.term);

core/src/main/java/org/elasticsearch/search/suggest/phrase/LinearInterpoatingScorer.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,19 @@ public LinearInterpoatingScorer(IndexReader reader, Terms terms, String field,
4141
this.bigramLambda = bigramLambda / sum;
4242
this.trigramLambda = trigramLambda / sum;
4343
}
44-
44+
45+
double trigramLambda() {
46+
return this.trigramLambda;
47+
}
48+
49+
double bigramLambda() {
50+
return this.bigramLambda;
51+
}
52+
53+
double unigramLambda() {
54+
return this.unigramLambda;
55+
}
56+
4557
@Override
4658
protected double scoreBigram(Candidate word, Candidate w_1) throws IOException {
4759
SuggestUtils.join(separator, spare, w_1.term, word.term);

core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestParser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import org.elasticsearch.search.suggest.SuggestContextParser;
3636
import org.elasticsearch.search.suggest.SuggestUtils;
3737
import org.elasticsearch.search.suggest.SuggestionSearchContext;
38+
import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder.Laplace;
39+
import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder.StupidBackoff;
3840
import org.elasticsearch.search.suggest.phrase.PhraseSuggestionContext.DirectCandidateGenerator;
3941

4042
import java.io.IOException;
@@ -262,7 +264,7 @@ public WordScorer newScorer(IndexReader reader, Terms terms, String field, doubl
262264
});
263265
} else if ("laplace".equals(fieldName)) {
264266
ensureNoSmoothing(suggestion);
265-
double theAlpha = 0.5;
267+
double theAlpha = Laplace.DEFAULT_LAPLACE_ALPHA;
266268

267269
while ((token = parser.nextToken()) != Token.END_OBJECT) {
268270
if (token == XContentParser.Token.FIELD_NAME) {
@@ -283,7 +285,7 @@ public WordScorer newScorer(IndexReader reader, Terms terms, String field, doubl
283285

284286
} else if ("stupid_backoff".equals(fieldName) || "stupidBackoff".equals(fieldName)) {
285287
ensureNoSmoothing(suggestion);
286-
double theDiscount = 0.4;
288+
double theDiscount = StupidBackoff.DEFAULT_BACKOFF_DISCOUNT;
287289
while ((token = parser.nextToken()) != Token.END_OBJECT) {
288290
if (token == XContentParser.Token.FIELD_NAME) {
289291
fieldName = parser.currentName();

0 commit comments

Comments
 (0)