aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/ranking/features/FieldTermMatch.java
blob: bbe9e0b850a30508f5489b884e1512d0236e310b (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
// 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.HashMap;
import java.util.Map;

/**
 * Calculates the fieldTermMatch features
 *
 * @author bratseth
 */
@Beta
public class FieldTermMatch {

    /**
     * Computes the fieldTermMatch features:
     * <ul>
     *     <li><code>firstPosition</code> - the position of the first occurrence of this query term in this index field</li>
     *     <li><code>occurrences</code> - the position of the first occurrence of this query term in this index field</li>
     * </ul>
     * @param queryTerm the term to return these features for
     * @param field the field value to compute over, assumed to be a space-separated string of tokens
     * @return a features object containing the two values described above
     */
    public static Features compute(String queryTerm, String field) {
        Map<String, Value> features = new HashMap<>();

        String[] tokens = field.split(" ");

        int occurrences = 0;
        int firstPosition = 1000000;
        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i].equals(queryTerm)) {
                if (occurrences == 0)
                    firstPosition = i;
                occurrences++;
            }
        }
        features.put("firstPosition", new DoubleValue(firstPosition));
        features.put("occurrences", new DoubleValue(occurrences));
        return new Features(features);
    }

}