aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/semantics/rule/TermCondition.java
blob: 9baa240fb4322724ce1f1599911cdb418f3b909b (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 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.semantics.rule;

import com.yahoo.prelude.query.TermItem;
import com.yahoo.prelude.semantics.engine.NameSpace;
import com.yahoo.prelude.semantics.engine.RuleEvaluation;

/**
 * A term in a rule
 *
 * @author bratseth
 */
public class TermCondition extends Condition {

    private String term,termPlusS;

    /** Creates an invalid term */
    public TermCondition() { }

    public TermCondition(String term) {
        this(null,term);
    }

    public TermCondition(String label,String term) {
        super(label);
        this.term=term;
        termPlusS=term + "s";
    }

    public String getTerm() { return term; }

    public void setTerm(String term) {
        this.term=term;
        termPlusS=term + "s";
    }

    protected boolean doesMatch(RuleEvaluation e) {
        // TODO: Move this into the respective namespaces when query becomes one */
        if (getNameSpace()!=null) {
            NameSpace nameSpace=e.getEvaluation().getNameSpace(getNameSpace());
            return nameSpace.matches(term,e);
        }
        else {
            if (e.currentItem()==null)
                return false;

            if (!labelMatches(e)) return false;

            String matchedValue=termMatches(e.currentItem().getItem(),e.getEvaluation().getStemming());
            boolean matches=matchedValue!=null && labelMatches(e.currentItem().getItem(),e);
            if ((matches && !e.isInNegation() || (!matches && e.isInNegation()))) {
                e.addMatch(e.currentItem(),matchedValue);
                e.setValue(term);
                e.next();
            }
            return matches;
        }
    }

    /** Returns a non-null replacement term if there is a match, null otherwise */
    private String termMatches(TermItem queryTerm,boolean stemming){
        String queryTermString=queryTerm.stringValue();

        // The terms are the same
        boolean matches=queryTermString.equals(term);
        if (matches) return term;

        if (stemming)
            if (termMatchesWithStemming(queryTermString)) return term;

        return null;
    }

    private boolean termMatchesWithStemming(String queryTermString) {
        if (queryTermString.length()<3) return false; // Don't stem very short terms

        // The query term minus s is the same
        boolean matches=queryTermString.equals(termPlusS);
        if (matches) return true;

        // The query term plus s is the same
        matches=term.equals(queryTermString + "s");
        if (matches) return true;

        return false;
    }

    public String toInnerString() {
        return getLabelString() + term;
    }

}