| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.query; |
| 2 | |
| 3 | import java.io.IOException; |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 4 | import java.util.ArrayList; |
| 5 | import java.util.List; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 6 | import java.util.Map; |
| 7 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 8 | import org.apache.lucene.index.AtomicReaderContext; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 9 | import org.apache.lucene.index.IndexReader; |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 10 | import org.apache.lucene.index.Term; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 11 | import org.apache.lucene.index.TermContext; |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 12 | import org.apache.lucene.search.Query; |
| 13 | import org.apache.lucene.search.spans.SpanQuery; |
| 14 | import org.apache.lucene.search.spans.Spans; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 15 | import org.apache.lucene.util.Bits; |
| 16 | import org.apache.lucene.util.ToStringUtils; |
| 17 | |
| Nils Diewald | f075df0 | 2015-03-03 20:34:00 +0000 | [diff] [blame] | 18 | import de.ids_mannheim.korap.query.spans.FocusSpans; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 19 | |
| 20 | /** |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 21 | * Modify the span of a match to the boundaries of a certain class. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 22 | * |
| 23 | * In case multiple classes are found with the very same number, the |
| margaretha | f70addb | 2015-04-27 13:17:18 +0200 | [diff] [blame] | 24 | * span is maximized to start on the first occurrence from the left |
| 25 | * and end on the last occurrence on the right. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 26 | * |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 27 | * In case the class to modify on is not found in the subquery, the |
| margaretha | f70addb | 2015-04-27 13:17:18 +0200 | [diff] [blame] | 28 | * match is ignored. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 29 | * |
| margaretha | f70addb | 2015-04-27 13:17:18 +0200 | [diff] [blame] | 30 | * @author diewald, margaretha |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 31 | * |
| Nils Diewald | f075df0 | 2015-03-03 20:34:00 +0000 | [diff] [blame] | 32 | * @see FocusSpans |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 33 | */ |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 34 | public class SpanFocusQuery extends SimpleSpanQuery { |
| 35 | |
| 36 | private List<Byte> classNumbers = new ArrayList<Byte>(); |
| 37 | private boolean isSorted = true; |
| margaretha | f70addb | 2015-04-27 13:17:18 +0200 | [diff] [blame] | 38 | private boolean matchTemporaryClass = false; |
| 39 | private boolean removeTemporaryClasses = false; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 40 | |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 41 | |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 42 | /** |
| Nils Diewald | cec40f9 | 2015-02-19 22:20:02 +0000 | [diff] [blame] | 43 | * Construct a new SpanFocusQuery. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 44 | * |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 45 | * @param firstClause |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 46 | * The nested {@link SpanQuery}, that contains one or |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 47 | * more |
| 48 | * classed spans. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 49 | * @param number |
| 50 | * The class number to focus on. |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 51 | */ |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 52 | public SpanFocusQuery (SpanQuery sq, byte classNumber) { |
| 53 | super(sq, true); |
| 54 | classNumbers.add(classNumber); |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | public SpanFocusQuery (SpanQuery sq, List<Byte> classNumbers) { |
| 59 | super(sq, true); |
| 60 | this.classNumbers = classNumbers; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 63 | |
| 64 | /** |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 65 | * Construct a new SpanFocusQuery. The class to focus on defaults |
| 66 | * to |
| 67 | * <tt>1</tt>. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 68 | * |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 69 | * @param firstClause |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 70 | * The nested {@link SpanQuery}, that contains one or |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 71 | * more |
| 72 | * classed spans. |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 73 | */ |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 74 | public SpanFocusQuery (SpanQuery sq) { |
| 75 | super(sq, true); |
| 76 | classNumbers.add((byte) 1); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 79 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 80 | @Override |
| 81 | public String toString (String field) { |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 82 | StringBuffer buffer = new StringBuffer(); |
| 83 | buffer.append("focus("); |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 84 | if (matchTemporaryClass) { |
| margaretha | f70addb | 2015-04-27 13:17:18 +0200 | [diff] [blame] | 85 | buffer.append("#"); |
| 86 | } |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 87 | if (classNumbers.size() > 1) { |
| 88 | buffer.append("["); |
| 89 | for (int i = 0; i < classNumbers.size(); i++) { |
| 90 | buffer.append((short) classNumbers.get(i) & 0xFF); |
| 91 | if (i != classNumbers.size() - 1) { |
| 92 | buffer.append(","); |
| 93 | } |
| 94 | } |
| 95 | buffer.append("]"); |
| 96 | } |
| 97 | else { |
| 98 | buffer.append((short) classNumbers.get(0) & 0xFF).append(": "); |
| 99 | } |
| 100 | buffer.append(this.firstClause.toString()); |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 101 | buffer.append(')'); |
| 102 | buffer.append(ToStringUtils.boost(getBoost())); |
| 103 | return buffer.toString(); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 106 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 107 | @Override |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 108 | public Spans getSpans (final AtomicReaderContext context, Bits acceptDocs, |
| 109 | Map<Term, TermContext> termContexts) throws IOException { |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 110 | return new FocusSpans(this, context, acceptDocs, termContexts); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 113 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 114 | @Override |
| 115 | public Query rewrite (IndexReader reader) throws IOException { |
| Nils Diewald | cec40f9 | 2015-02-19 22:20:02 +0000 | [diff] [blame] | 116 | SpanFocusQuery clone = null; |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 117 | SpanQuery query = (SpanQuery) this.firstClause.rewrite(reader); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 118 | |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 119 | if (query != this.firstClause) { |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 120 | if (clone == null) |
| 121 | clone = this.clone(); |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 122 | clone.firstClause = query; |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 123 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 124 | |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 125 | if (clone != null) |
| 126 | return clone; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 127 | |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 128 | return this; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 131 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 132 | @Override |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 133 | public SpanFocusQuery clone () { |
| Nils Diewald | cec40f9 | 2015-02-19 22:20:02 +0000 | [diff] [blame] | 134 | SpanFocusQuery spanFocusQuery = new SpanFocusQuery( |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 135 | (SpanQuery) this.firstClause.clone(), this.getClassNumbers()); |
| Nils Diewald | cec40f9 | 2015-02-19 22:20:02 +0000 | [diff] [blame] | 136 | spanFocusQuery.setBoost(getBoost()); |
| margaretha | f70addb | 2015-04-27 13:17:18 +0200 | [diff] [blame] | 137 | spanFocusQuery.setMatchTemporaryClass(this.matchTemporaryClass); |
| 138 | spanFocusQuery.setSorted(this.isSorted); |
| 139 | spanFocusQuery.setRemoveTemporaryClasses(this.removeTemporaryClasses); |
| Nils Diewald | cec40f9 | 2015-02-19 22:20:02 +0000 | [diff] [blame] | 140 | return spanFocusQuery; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 144 | @Override |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 145 | public boolean equals (Object o) { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 146 | if (this == o) |
| 147 | return true; |
| 148 | if (!(o instanceof SpanFocusQuery)) |
| 149 | return false; |
| 150 | |
| 151 | final SpanFocusQuery spanFocusQuery = (SpanFocusQuery) o; |
| 152 | |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 153 | if (!this.firstClause.equals(spanFocusQuery.firstClause)) |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 154 | return false; |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 155 | if (this.getClassNumbers() != spanFocusQuery.getClassNumbers()) |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 156 | return false; |
| Nils Diewald | cec40f9 | 2015-02-19 22:20:02 +0000 | [diff] [blame] | 157 | |
| 158 | // Probably not necessary |
| 159 | return getBoost() == spanFocusQuery.getBoost(); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 163 | @Override |
| Nils Diewald | 3e3cbf3 | 2015-02-06 21:30:49 +0000 | [diff] [blame] | 164 | public int hashCode () { |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 165 | int result = firstClause.hashCode(); |
| 166 | for (byte number : classNumbers) |
| 167 | result = 31 * result + number; |
| Nils Diewald | 85f9c42 | 2015-02-06 21:09:16 +0000 | [diff] [blame] | 168 | result += Float.floatToRawIntBits(getBoost()); |
| 169 | return result; |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
| 173 | public List<Byte> getClassNumbers () { |
| 174 | return classNumbers; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | public void setClassNumbers (List<Byte> classNumbers) { |
| 179 | this.classNumbers = classNumbers; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | public boolean isSorted () { |
| 184 | return isSorted; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | public void setSorted (boolean isSorted) { |
| 189 | this.isSorted = isSorted; |
| 190 | } |
| 191 | |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 192 | |
| 193 | public boolean matchTemporaryClass () { |
| margaretha | f70addb | 2015-04-27 13:17:18 +0200 | [diff] [blame] | 194 | return matchTemporaryClass; |
| 195 | } |
| 196 | |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 197 | |
| 198 | public void setMatchTemporaryClass (boolean matchTemporaryClass) { |
| margaretha | f70addb | 2015-04-27 13:17:18 +0200 | [diff] [blame] | 199 | this.matchTemporaryClass = matchTemporaryClass; |
| 200 | } |
| 201 | |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 202 | |
| 203 | public boolean removeTemporaryClasses () { |
| margaretha | f70addb | 2015-04-27 13:17:18 +0200 | [diff] [blame] | 204 | return removeTemporaryClasses; |
| 205 | } |
| 206 | |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 207 | |
| 208 | public void setRemoveTemporaryClasses (boolean rem) { |
| margaretha | f70addb | 2015-04-27 13:17:18 +0200 | [diff] [blame] | 209 | this.removeTemporaryClasses = rem; |
| 210 | } |
| 211 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 212 | }; |