aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/FlattenExpression.java
blob: 95cbaf3150d56a862208e381bae177f980dab8dd (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage.expressions;

import com.yahoo.document.DataType;
import com.yahoo.document.annotation.Annotation;
import com.yahoo.document.annotation.AnnotationTypes;
import com.yahoo.document.annotation.SpanNode;
import com.yahoo.document.annotation.SpanTree;
import com.yahoo.document.annotation.SpanTrees;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.StringFieldValue;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author Simon Thoresen Hult
 */
public final class FlattenExpression extends Expression {

    public FlattenExpression() {
        super(DataType.STRING);
    }

    @Override
    protected void doExecute(ExecutionContext context) {
        StringFieldValue input = (StringFieldValue) context.getValue();
        SpanTree tree = input.getSpanTree(SpanTrees.LINGUISTICS);
        Map<Integer, List<String>> map = new HashMap<>();
        for (Annotation anno : tree) {
            SpanNode span = anno.getSpanNode();
            if (span == null) {
                continue;
            }
            if (anno.getType() != AnnotationTypes.TERM) {
                continue;
            }
            FieldValue val = anno.getFieldValue();
            String str;
            if (val instanceof StringFieldValue) {
                str = ((StringFieldValue)val).getString();
            } else {
                str = input.getString().substring(span.getFrom(), span.getTo());
            }
            Integer pos = span.getTo();
            List<String> entry = map.computeIfAbsent(pos, k -> new LinkedList<>());
            entry.add(str);
        }
        String inputVal = String.valueOf(input);
        StringBuilder output = new StringBuilder();
        for (int i = 0, len = inputVal.length(); i <= len; ++i) {
            List<String> entry = map.get(i);
            if (entry != null) {
                Collections.sort(entry);
                output.append(entry);
            }
            if (i < len) {
                output.append(inputVal.charAt(i));
            }
        }
        context.setValue(new StringFieldValue(output.toString()));
    }

    @Override
    protected void doVerify(VerificationContext context) {
        context.setValueType(createdOutputType());
    }

    @Override
    public DataType createdOutputType() {
        return DataType.STRING;
    }

    @Override
    public String toString() {
        return "flatten";
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof FlattenExpression;
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }

}