aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/ranking/features/ElementCompleteness.java
blob: e5566135e86f0271bcb9ba10e4eb95db946c26e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.ranking.features;

import com.yahoo.api.annotations.Beta;
import com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue;
import com.yahoo.searchlib.rankingexpression.evaluation.Value;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Calculates the elementCompleteness features
 *
 * @author bratseth
 */
public class ElementCompleteness {

    /** Hardcoded to default for now */
    private static final double fieldCompletenessImportance	= 0.05;

    /**
     * Computes the following elementCompleteness features:
     * <ul>
     *     <li><code>completeness</code>
     *     <li><code>fieldCompleteness</code>
     *     <li><code>queryCompleteness</code>
     *     <li><code>elementWeight</code>
     * </ul>
     *
     * @param queryTerms the query terms with associated weights to compute over
     * @param field a set of weighted field values, where each is taken to be a space-separated string of tokens
     * @return a features object containing the values listed above
     */
    public static Features compute(Map<String, Integer> queryTerms, Item[] field) {
        double completeness = 0;
        double fieldCompleteness = 0;
        double queryCompleteness = 0;
        double elementWeight = 0;

        double queryTermWeightSum = sum(queryTerms.values());

        for (Item item : field) {
            String[] itemTokens =item.value().split(" ");
            int matchCount = 0;
            int matchWeightSum = 0;
            for (String token : itemTokens) {
                Integer weight = queryTerms.get(token);
                if (weight == null) continue;
                matchCount++;
                matchWeightSum += weight;
            }
            double itemFieldCompleteness = (double)matchCount / itemTokens.length;
            double itemQueryCompleteness = matchWeightSum / queryTermWeightSum;
            double itemCompleteness =
                    fieldCompletenessImportance * itemFieldCompleteness +
                    (1 - fieldCompletenessImportance) * itemQueryCompleteness;
            if (itemCompleteness > completeness) {
                completeness = itemCompleteness;
                fieldCompleteness = itemFieldCompleteness;
                queryCompleteness = itemQueryCompleteness;
                elementWeight = item.weight();
            }
        }

        Map<String, Value> features = new HashMap<>();
        features.put("completeness", new DoubleValue(completeness));
        features.put("fieldCompleteness", new DoubleValue(fieldCompleteness));
        features.put("queryCompleteness", new DoubleValue(queryCompleteness));
        features.put("elementWeight", new DoubleValue(elementWeight));
        return new Features(features);
    }

    private static int sum(Collection<Integer> integers) {
        int sum = 0;
        for (int integer : integers)
            sum += integer;
        return sum;
    }

    public static class Item {

        private final String value;
        private final double weight;

        public Item(String value, double weight) {
            this.value = value;
            this.weight = weight;
        }

        public String value() { return value; }
        public double weight() { return weight; }

    }

}