aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/query/PhraseSegmentItem.java
blob: 2a97970ac4a89930d1a6c1f492f88efb00ee5938 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.query;

import com.yahoo.prelude.query.textualrepresentation.Discloser;

import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;


/**
 * A term which contains a fixed length phrase, a collection of word terms,
 * resulting from a single segmentation operation.
 *
 * @author Steinar Knutsen
 */
public class PhraseSegmentItem extends IndexedSegmentItem {

    /** Whether this was explicitly written as a phrase using quotes by the user */
    private boolean explicit = false;

    /** Creates a phrase containing the same words and state (as pertinent) as the given SegmentAndItem. */
    public PhraseSegmentItem(AndSegmentItem andSegment) {
        super(andSegment.getRawWord(), andSegment.stringValue(), andSegment.isFromQuery(), andSegment.isStemmed(), andSegment.getOrigin());
        if (andSegment.getItemCount() > 0) {
            WordItem w = (WordItem) andSegment.getItem(0);
            setIndexName(w.getIndexName());
            for (Iterator<Item> i = andSegment.getItemIterator(); i.hasNext();) {
                WordItem word = (WordItem) i.next();
                addWordItem(word);
            }
        }
    }

    public PhraseSegmentItem(String rawWord, boolean isFromQuery, boolean stemmed) {
        super(rawWord, rawWord, isFromQuery, stemmed, null);
    }

    /**
     * Creates a phrase segment from strings
     *
     * @param rawWord the raw text as received in the request
     * @param current the normalized form of the raw text, or the raw text repeated if no normalized form is known
     * @param isFromQuery whether this originates in the request
     * @param stemmed whether this is stemmed
     */
    public PhraseSegmentItem(String rawWord, String current, boolean isFromQuery, boolean stemmed) {
        super(rawWord, current, isFromQuery, stemmed, null);
    }

    public PhraseSegmentItem(String rawWord, String current, boolean isFromQuery,
                             boolean stemmed, Substring substring) {
        super(rawWord, current, isFromQuery, stemmed, substring);
    }

    @Override
    public ItemType getItemType() {
        return ItemType.PHRASE;
    }

    @Override
    public String getName() {
        return "SPHRASE";
    }

    @Override
    public void setIndexName(String index) {
        super.setIndexName(index);
        for (Iterator<Item> i = getItemIterator(); i.hasNext();) {
            WordItem word = (WordItem) i.next();
            word.setIndexName(index);
        }
    }

    @Override
    public void setWeight(int weight) {
        super.setWeight(weight);
        for (Iterator<Item> i = getItemIterator(); i.hasNext();) {
            Item word = i.next();
            word.setWeight(weight);
        }
    }

    /**
     * Adds subitem. The word will have its index name set to the index name
     * of this phrase. If the item is a word, it will simply be added,
     * if the item is a phrase, each of the words of the phrase will be added.
     *
     * @throws IllegalArgumentException if the given item is not a WordItem or PhraseItem
     */
    @Override
    public void addItem(Item item) {
        if (item instanceof WordItem) {
            addWordItem((WordItem) item);
        } else {
            throw new IllegalArgumentException("Can not add " + item + " to a segment phrase");
        }
    }

    @Override
    public Optional<Item> extractSingleChild() {
        Optional<Item> extracted = super.extractSingleChild();
        extracted.ifPresent(e -> e.setWeight(this.getWeight()));
        return extracted;
    }

    private void addWordItem(WordItem word) {
        word.setIndexName(this.getIndexName());
        super.addItem(word);
    }

    // TODO: Override addItem(index,item), setItem(index,item)

    /**
     * Returns a subitem as a word item
     *
     * @param index the (0-base) index of the item to return
     * @throws IndexOutOfBoundsException if there is no subitem at index
     */
    public WordItem getWordItem(int index) {
        return (WordItem) getItem(index);
    }

    @Override
    protected void encodeThis(ByteBuffer buffer) {
        super.encodeThis(buffer); // takes care of index bytes
    }

    @Override
    public int encode(ByteBuffer buffer) {
        encodeThis(buffer);
        return encodeContent(buffer, 1);
    }

    public int encodeContent(ByteBuffer buffer) {
        return encodeContent(buffer, 0);
    }

    private int encodeContent(ByteBuffer buffer, int itemCount) {
        for (Iterator<Item> i = getItemIterator(); i.hasNext();) {
            Item subitem = i.next();
            itemCount += subitem.encode(buffer);
        }
        return itemCount;
    }


    /** Returns false, no parenthezes for phrases */
    @Override
    protected boolean shouldParenthesize() {
        return false;
    }

    /** Segment phrase items uses a empty heading instead of "SPHRASE " */
    @Override
    protected void appendHeadingString(StringBuilder buffer) {}

    @Override
    protected void appendBodyString(StringBuilder buffer) {
        appendIndexString(buffer);
        appendContentsString(buffer);
    }

    void appendContentsString(StringBuilder buffer) {
        buffer.append("'");
        for (Iterator<Item> i = getItemIterator(); i.hasNext();) {
            WordItem wordItem = (WordItem) i.next();

            buffer.append(wordItem.getWord());
            if (i.hasNext()) {
                buffer.append(" ");
            }
        }
        buffer.append("'");
    }

    @Override
    public String getIndexedString() {
        StringBuilder buf = new StringBuilder();

        for (Iterator<Item> i = getItemIterator(); i.hasNext();) {
            IndexedItem indexedItem = (IndexedItem) i.next();

            buf.append(indexedItem.getIndexedString());
            if (i.hasNext()) {
                buf.append(' ');
            }
        }
        return buf.toString();
    }

    public boolean isExplicit() {
        return explicit;
    }

    public void setExplicit(boolean explicit) {
        this.explicit = explicit;
    }

    @Override
    public void disclose(Discloser discloser) {
        super.disclose(discloser);
        discloser.addProperty("explicit", explicit);
    }

    @Override
    public boolean equals(Object other) {
        if ( ! super.equals(other)) return false;
        return this.explicit == ((PhraseSegmentItem)other).explicit;
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), explicit);
    }

}