aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/query/PhraseItem.java
blob: 130eafe49ef74c3c520fb0d8faf96276cf670934 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright 2016 Yahoo Inc. 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;

/**
 * A term which contains a phrase - a collection of word terms
 *
 * @author bratseth
 * @author havardpe
 */
public class PhraseItem extends CompositeIndexedItem {

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

    /** Creates an empty phrase */
    public PhraseItem() {}

    /** Creates an empty phrase which will search the given index */
    public PhraseItem(String indexName) {
        setIndexName(indexName);
    }

    /** Creates a phrase containing the given words */
    public PhraseItem(String[] words) {
        for (int i = 0; i < words.length; i++) {
            addIndexedItem(new WordItem(words[i]));
        }
    }

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

    public String getName() {
        return "PHRASE";
    }

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

    /**
     * Sets whether this was explicitly written as a phrase using quotes by the
     * user
     */
    public void setExplicit(boolean explicit) {
        this.explicit = explicit;
    }

    /**
     * Returns whether this was explicitly written as a phrase using quotes by
     * the user Default is false
     */
    public boolean isExplicit() {
        return explicit;
    }

    private IndexedItem convertIntToWord(Item orig) {
        IntItem o = (IntItem) orig;
        return new WordItem(o.stringValue(), o.getIndexName(), o.isFromQuery());
    }

    /**
     * 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
     */
    public void addItem(Item item) {
        if (item instanceof WordItem || item instanceof PhraseSegmentItem || item instanceof WordAlternativesItem) {
            addIndexedItem((IndexedItem) item);
        } else if (item instanceof IntItem) {
            addIndexedItem(convertIntToWord(item));
        } else if (item instanceof PhraseItem) {
            PhraseItem phrase = (PhraseItem) item;

            for (Iterator<Item> i = phrase.getItemIterator(); i.hasNext();) {
                addIndexedItem((IndexedItem) i.next());
            }
        } else {
            throw new IllegalArgumentException("Can not add " + item
                    + " to a phrase");
        }
    }

    @Override
    public void addItem(int index, Item item) {
        if (item instanceof WordItem || item instanceof PhraseSegmentItem) {
            addIndexedItem(index, (IndexedItem) item);
        } else if (item instanceof IntItem) {
            addIndexedItem(index, convertIntToWord(item));
        } else if (item instanceof PhraseItem) {
            PhraseItem phrase = (PhraseItem) item;

            for (Iterator<Item> i = phrase.getItemIterator(); i.hasNext();) {
                addIndexedItem(index++, (WordItem) i.next());
            }
        } else {
            throw new IllegalArgumentException("Can not add " + item
                    + " to a phrase");
        }
    }

    @Override
    public Item setItem(int index, Item item) {
        if (item instanceof WordItem || item instanceof PhraseSegmentItem) {
            return setIndexedItem(index, (IndexedItem) item);
        } else if (item instanceof IntItem) {
            return setIndexedItem(index, convertIntToWord(item));
        } else if (item instanceof PhraseItem) {
            PhraseItem phrase = (PhraseItem) item;
            Iterator<Item> i = phrase.getItemIterator();
            // we assume we don't try to add empty phrases
            IndexedItem firstItem = (IndexedItem) i.next();
            Item toReturn = setIndexedItem(index++, firstItem);

            while (i.hasNext()) {
                addIndexedItem(index++, (IndexedItem) i.next());
            }
            return toReturn;
        } else {
            throw new IllegalArgumentException("Can not add " + item
                    + " to a phrase");
        }
    }

    private void addIndexedItem(IndexedItem word) {
        word.setIndexName(this.getIndexName());
        super.addItem((Item) word);
    }

    private void addIndexedItem(int index, IndexedItem word) {
        word.setIndexName(this.getIndexName());
        super.addItem(index, (Item) word);
    }

    private Item setIndexedItem(int index, IndexedItem word) {
        word.setIndexName(this.getIndexName());
        return super.setItem(index, (Item) word);
    }

    /**
     * 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);
    }

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

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

    public int encode(ByteBuffer buffer) {
        encodeThis(buffer);
        int itemCount = 1;

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

            if (subitem instanceof PhraseSegmentItem) {
                PhraseSegmentItem seg = (PhraseSegmentItem) subitem;

                // "What encode does, minus what encodeThis does"
                itemCount += seg.encodeContent(buffer);
            } else {
                itemCount += subitem.encode(buffer);
            }
        }
        return itemCount;
    }

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

    /** Phrase items uses a empty heading instead of "PHRASE " */
    protected void appendHeadingString(StringBuilder buffer) {
    }

    protected void appendBodyString(StringBuilder buffer) {
        appendIndexString(buffer);

        buffer.append("\"");
        for (Iterator<Item> i = getItemIterator(); i.hasNext();) {
            Item item = i.next();

            if (item instanceof WordItem) {
                WordItem wordItem = (WordItem) item;

                buffer.append(wordItem.getWord());
            } else {
                PhraseSegmentItem seg = (PhraseSegmentItem) item;

                seg.appendContentsString(buffer);
            }
            if (i.hasNext()) {
                buffer.append(" ");
            }
        }
        buffer.append("\"");
    }

    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();
    }

    protected int encodingArity() {
        return getNumWords();
    }

    public int getNumWords() {
        int numWords = 0;

        for (Iterator<Item> j = getItemIterator(); j.hasNext();) {
            numWords += ((IndexedItem) j.next()).getNumWords();
        }
        return numWords;
    }

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