| 1 | |
|
| 2 | |
package org.galagosearch.core.scoring; |
| 3 | |
|
| 4 | |
import java.io.IOException; |
| 5 | |
import java.util.HashMap; |
| 6 | |
import org.galagosearch.core.types.DocumentLengthWordCount; |
| 7 | |
import org.galagosearch.core.types.DocumentWordProbability; |
| 8 | |
import org.galagosearch.tupleflow.Parameters; |
| 9 | |
import org.galagosearch.tupleflow.StandardStep; |
| 10 | |
import org.galagosearch.tupleflow.Utility; |
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | 0 | public class LinearSmoother extends StandardStep<DocumentLengthWordCount, DocumentWordProbability> |
| 17 | |
implements DistributionSmoother { |
| 18 | |
double lambda; |
| 19 | |
HashMap<String, Double> backgrounds; |
| 20 | |
|
| 21 | 0 | public LinearSmoother(Parameters.Value value, HashMap<String, Double> backgrounds) { |
| 22 | 0 | double lm = 0.4; |
| 23 | |
|
| 24 | 0 | if (value.containsKey("lambda")) { |
| 25 | 0 | lm = Double.parseDouble(value.get("lambda")); |
| 26 | |
} |
| 27 | |
|
| 28 | 0 | this.lambda = lm; |
| 29 | 0 | this.backgrounds = backgrounds; |
| 30 | 0 | } |
| 31 | |
|
| 32 | 0 | public LinearSmoother(double lambda, HashMap<String, Double> backgrounds) { |
| 33 | 0 | this.lambda = lambda; |
| 34 | 0 | this.backgrounds = backgrounds; |
| 35 | 0 | } |
| 36 | |
|
| 37 | |
public void process(DocumentLengthWordCount object) throws IOException { |
| 38 | 0 | double background = backgrounds.get(object.word); |
| 39 | 0 | double foreground = 0; |
| 40 | |
|
| 41 | 0 | if (object.length > 0) { |
| 42 | 0 | foreground = (double) object.count / (double) object.length; |
| 43 | |
} |
| 44 | 0 | double probability = lambda * foreground + (1 - lambda) * background; |
| 45 | 0 | processor.process(new DocumentWordProbability(object.document, |
| 46 | |
Utility.makeBytes(object.word), probability)); |
| 47 | 0 | } |
| 48 | |
|
| 49 | |
public double smooth(double background, int count, int length) { |
| 50 | 0 | return (1 - lambda) * (double) count / (double) length + lambda * background; |
| 51 | |
} |
| 52 | |
|
| 53 | |
public double smooth(String word, int count, int length) { |
| 54 | 0 | return smooth(backgrounds.get(word), count, length); |
| 55 | |
} |
| 56 | |
|
| 57 | |
public Class<DocumentLengthWordCount> getInputClass() { |
| 58 | 0 | return DocumentLengthWordCount.class; |
| 59 | |
} |
| 60 | |
|
| 61 | |
public Class<DocumentWordProbability> getOutputClass() { |
| 62 | 0 | return DocumentWordProbability.class; |
| 63 | |
} |
| 64 | |
} |