blob: c1356562add5302c12a4adee055bca856be434a2 [file] [log] [blame]
Eliza Margaretha7ee76da2014-08-12 15:32:33 +00001package de.ids_mannheim.korap.query;
2
3import java.io.IOException;
4import java.util.Map;
5
6import org.apache.lucene.index.AtomicReaderContext;
7import org.apache.lucene.index.Term;
8import org.apache.lucene.index.TermContext;
9import org.apache.lucene.search.spans.SpanQuery;
10import org.apache.lucene.search.spans.Spans;
11import org.apache.lucene.util.Bits;
12
Nils Diewald5380aa62014-09-01 13:21:07 +000013// Temporary:
Eliza Margaretha99c72c22014-09-17 08:38:25 +000014import de.ids_mannheim.korap.query.spans.ExpandedExclusionSpans;
Eliza Margaretha7ee76da2014-08-12 15:32:33 +000015import de.ids_mannheim.korap.query.spans.ExpandedSpans;
16
Eliza Margaretha7788a982014-08-29 16:10:52 +000017/** Query to make a span longer by stretching out the start or the end
18 * position of the span. The constraints of the expansion, such as how
19 * large the expansion should be (min and max position) and the
20 * direction of the expansion with respect to the "main" span, are
21 * specified in ExpansionConstraint.
22 *
23 * The expansion can be specified to not contain any direct/immediate
24 * /adjacent occurrence(s) of another span. Examples:
25 * [orth=der][orth!=Baum] "der" cannot be followed by "Baum"
26 * [pos!=ADJ]{1,2}[orth=Baum] one or two adjectives cannot precedes
27 * "Baum"
28 *
29 * The offsets of the expansion parts can be collected by using a class
30 * number.
Eliza Margaretha7ee76da2014-08-12 15:32:33 +000031 *
32 * @author margaretha
33 * */
34public class SpanExpansionQuery extends SimpleSpanQuery{
Eliza Margaretha7788a982014-08-29 16:10:52 +000035
36 private int min, max; // min, max expansion position
Eliza Margaretha7ee76da2014-08-12 15:32:33 +000037
Eliza Margaretha7788a982014-08-29 16:10:52 +000038 // if > 0, collect expansion offsets using this label
39 private byte classNumber;
40
41 // expansion direction with regard to the main span:
42 // < 0 to the left of main span
43 // >= 0 to the right of main span
44 private int direction;
45
46 // if true, no occurrence of another span
47 final boolean isExclusion;
48
49 /** Simple expansion for any/empty token. Use
50 * {@link #SpanExpansionQuery(SpanQuery, SpanQuery, ExpansionConstraint,
51 * boolean)} for expansion with exclusions of a specific spanquery.
52 * */
53 public SpanExpansionQuery(SpanQuery firstClause, int min, int max, int direction,
54 boolean collectPayloads) {
Eliza Margaretha7ee76da2014-08-12 15:32:33 +000055 super(firstClause, collectPayloads);
Eliza Margaretha3eaafc62014-09-17 12:34:26 +000056 if (max < min){
57 throw new IllegalArgumentException("The max position has to be " +
58 "bigger than or the same as min position.");
59 }
Eliza Margaretha7ee76da2014-08-12 15:32:33 +000060 this.min = min;
61 this.max = max;
Eliza Margaretha7788a982014-08-29 16:10:52 +000062 this.direction = direction;
63 this.isExclusion = false;
Eliza Margaretha7ee76da2014-08-12 15:32:33 +000064 }
Eliza Margaretha656cb312014-08-14 12:42:26 +000065
Eliza Margaretha7788a982014-08-29 16:10:52 +000066 public SpanExpansionQuery(SpanQuery firstClause, int min, int max, int direction,
67 byte classNumber, boolean collectPayloads) {
68 this(firstClause, min, max, direction, collectPayloads);
Eliza Margaretha656cb312014-08-14 12:42:26 +000069 this.classNumber = classNumber;
70 }
Eliza Margaretha7788a982014-08-29 16:10:52 +000071
72 /** Expansion with exclusions of the spanquery specified as the second
73 * parameter.
74 * */
75 public SpanExpansionQuery(SpanQuery firstClause, SpanQuery notClause, int min,
76 int max, int direction, boolean collectPayloads) {
77 super(firstClause, notClause, collectPayloads);
Eliza Margaretha3eaafc62014-09-17 12:34:26 +000078 if (max < min){
79 throw new IllegalArgumentException("The max position has to be " +
80 "bigger than or the same as min position.");
81 }
Eliza Margaretha7788a982014-08-29 16:10:52 +000082 this.min = min;
83 this.max = max;
84 this.direction = direction;
85 this.isExclusion = true;
86 }
Eliza Margaretha99c72c22014-09-17 08:38:25 +000087
88 public SpanExpansionQuery(SpanQuery firstClause, SpanQuery notClause, int min,
89 int max, int direction, byte classNumber, boolean collectPayloads) {
90 this(firstClause, notClause, min, max, direction, collectPayloads);
91 this.classNumber = classNumber;
92 }
93
Eliza Margaretha7788a982014-08-29 16:10:52 +000094
Eliza Margaretha7ee76da2014-08-12 15:32:33 +000095 @Override
96 public SimpleSpanQuery clone() {
Eliza Margaretha7788a982014-08-29 16:10:52 +000097 SpanExpansionQuery sq = null;
98 if (isExclusion){
Eliza Margaretha99c72c22014-09-17 08:38:25 +000099 sq = new SpanExpansionQuery(firstClause, secondClause, min, max, direction,
100 classNumber, collectPayloads);
Eliza Margaretha7788a982014-08-29 16:10:52 +0000101 }
102 else{
103 sq = new SpanExpansionQuery(firstClause, min, max, direction, classNumber,
104 collectPayloads);
105 }
Eliza Margaretha7ee76da2014-08-12 15:32:33 +0000106 //sq.setBoost(sq.getBoost());
107 return sq;
108 }
109
110 @Override
111 public Spans getSpans(AtomicReaderContext context, Bits acceptDocs,
Eliza Margaretha7788a982014-08-29 16:10:52 +0000112 Map<Term, TermContext> termContexts) throws IOException {
Eliza Margaretha99c72c22014-09-17 08:38:25 +0000113
114// Temporary:
Eliza Margaretha7788a982014-08-29 16:10:52 +0000115 if (isExclusion)
116 return new ExpandedExclusionSpans(this, context, acceptDocs, termContexts);
117 else
Eliza Margaretha99c72c22014-09-17 08:38:25 +0000118
Eliza Margaretha7788a982014-08-29 16:10:52 +0000119 return new ExpandedSpans(this, context, acceptDocs, termContexts);
Eliza Margaretha7ee76da2014-08-12 15:32:33 +0000120 }
121
122 @Override
123 public String toString(String field) {
124 StringBuilder sb = new StringBuilder();
125 sb.append("spanExpansion(");
126 sb.append(firstClause.toString());
Eliza Margaretha7788a982014-08-29 16:10:52 +0000127 if (isExclusion && secondClause != null){
128 sb.append(", !");
129 sb.append(secondClause.toString());
130 }
131 else{
132 sb.append(", []");
133 }
134 sb.append("{");
Eliza Margaretha7ee76da2014-08-12 15:32:33 +0000135 sb.append(min);
Eliza Margaretha7788a982014-08-29 16:10:52 +0000136 sb.append(", ");
Eliza Margaretha7ee76da2014-08-12 15:32:33 +0000137 sb.append(max);
Eliza Margaretha7788a982014-08-29 16:10:52 +0000138 sb.append("}, ");
139 if (direction < 0)
140 sb.append("left");
141 else sb.append("right");
142 if (classNumber > 0){
143 sb.append(", class:");
144 sb.append(classNumber);
145 }
146 sb.append(")");
Eliza Margaretha7ee76da2014-08-12 15:32:33 +0000147 return sb.toString();
148 }
149
150 public int getMin() {
151 return min;
152 }
153
154 public void setMin(int min) {
155 this.min = min;
156 }
157
158 public int getMax() {
159 return max;
160 }
161
162 public void setMax(int max) {
163 this.max = max;
164 }
165
Eliza Margaretha656cb312014-08-14 12:42:26 +0000166 public byte getClassNumber() {
167 return classNumber;
168 }
169
170 public void setClassNumber(byte classNumber) {
171 this.classNumber = classNumber;
172 }
Eliza Margaretha7788a982014-08-29 16:10:52 +0000173
174 public int getDirection() {
175 return direction;
176 }
177
178 public void setDirection(int direction) {
179 this.direction = direction;
180 }
Eliza Margaretha7ee76da2014-08-12 15:32:33 +0000181}