| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.query.wrap; |
| 2 | |
| 3 | import java.util.*; |
| Nils Diewald | 164f8be | 2014-02-13 02:43:16 +0000 | [diff] [blame] | 4 | import de.ids_mannheim.korap.query.DistanceConstraint; |
| 5 | import de.ids_mannheim.korap.query.SpanElementQuery; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 6 | import de.ids_mannheim.korap.query.SpanNextQuery; |
| Nils Diewald | d7cb0eb | 2014-02-12 23:06:10 +0000 | [diff] [blame] | 7 | import de.ids_mannheim.korap.query.SpanDistanceQuery; |
| Nils Diewald | 164f8be | 2014-02-13 02:43:16 +0000 | [diff] [blame] | 8 | import de.ids_mannheim.korap.query.SpanMultipleDistanceQuery; |
| 9 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 10 | import de.ids_mannheim.korap.query.wrap.SpanSegmentQueryWrapper; |
| 11 | import de.ids_mannheim.korap.query.wrap.SpanRegexQueryWrapper; |
| 12 | |
| 13 | import org.apache.lucene.index.Term; |
| 14 | import org.apache.lucene.search.spans.SpanQuery; |
| 15 | import org.apache.lucene.search.spans.SpanTermQuery; |
| 16 | import de.ids_mannheim.korap.query.wrap.SpanQueryWrapperInterface; |
| 17 | |
| 18 | /** |
| 19 | * @author Nils Diewald |
| 20 | */ |
| 21 | public class SpanSequenceQueryWrapper implements SpanQueryWrapperInterface { |
| 22 | private String field; |
| Nils Diewald | d7cb0eb | 2014-02-12 23:06:10 +0000 | [diff] [blame] | 23 | private ArrayList<SpanQuery> segments; |
| 24 | private ArrayList<DistanceConstraint> constraints; |
| 25 | private boolean isInOrder = true; |
| 26 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 27 | public SpanSequenceQueryWrapper (String field) { |
| 28 | this.field = field; |
| Nils Diewald | d7cb0eb | 2014-02-12 23:06:10 +0000 | [diff] [blame] | 29 | this.segments = new ArrayList<SpanQuery>(2); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | public SpanSequenceQueryWrapper (String field, String ... terms) { |
| 33 | this(field); |
| 34 | for (int i = 0; i < terms.length; i++) { |
| 35 | this.segments.add((SpanQuery) new SpanTermQuery(new Term(field, terms[i]))); |
| 36 | }; |
| 37 | }; |
| 38 | |
| 39 | public SpanSequenceQueryWrapper (String field, SpanQuery sq) { |
| 40 | this(field); |
| 41 | this.segments.add((SpanQuery) sq); |
| 42 | }; |
| 43 | |
| 44 | public SpanSequenceQueryWrapper (String field, SpanQueryWrapperInterface sswq) { |
| 45 | this(field); |
| 46 | this.segments.add((SpanQuery) sswq.toQuery()); |
| 47 | }; |
| 48 | |
| 49 | public SpanSequenceQueryWrapper (String field, SpanRegexQueryWrapper re) { |
| 50 | this(field); |
| 51 | this.segments.add((SpanQuery) re.toQuery()); |
| 52 | }; |
| 53 | |
| 54 | public SpanQuery get (int index) { |
| 55 | return this.segments.get(index); |
| 56 | }; |
| 57 | |
| 58 | public void set (int index, SpanQuery sq) { |
| 59 | this.segments.set(index, sq); |
| 60 | }; |
| 61 | |
| 62 | public SpanSequenceQueryWrapper append (String term) { |
| 63 | this.segments.add((SpanQuery) new SpanTermQuery(new Term(field, term))); |
| 64 | return this; |
| 65 | }; |
| 66 | |
| 67 | public SpanSequenceQueryWrapper append (SpanQueryWrapperInterface ssq) { |
| 68 | this.segments.add((SpanQuery) ssq.toQuery()); |
| 69 | return this; |
| 70 | }; |
| 71 | |
| 72 | public SpanSequenceQueryWrapper append (SpanRegexQueryWrapper srqw) { |
| 73 | this.segments.add((SpanQuery) srqw.toQuery()); |
| 74 | return this; |
| 75 | }; |
| 76 | |
| 77 | public SpanSequenceQueryWrapper prepend (String term) { |
| 78 | this.segments.add(0, (SpanQuery) new SpanTermQuery(new Term(field, term))); |
| 79 | return this; |
| 80 | }; |
| 81 | |
| 82 | public SpanSequenceQueryWrapper prepend (SpanSegmentQueryWrapper ssq) { |
| 83 | this.segments.add(0, (SpanQuery) ssq.toQuery()); |
| 84 | return this; |
| 85 | }; |
| 86 | |
| 87 | public SpanSequenceQueryWrapper prepend (SpanRegexQueryWrapper re) { |
| 88 | this.segments.add(0, (SpanQuery) re.toQuery()); |
| 89 | return this; |
| 90 | }; |
| 91 | |
| Nils Diewald | d7cb0eb | 2014-02-12 23:06:10 +0000 | [diff] [blame] | 92 | public SpanSequenceQueryWrapper withConstraint (int min, int max) { |
| Nils Diewald | 164f8be | 2014-02-13 02:43:16 +0000 | [diff] [blame] | 93 | if (this.constraints == null) |
| 94 | this.constraints = new ArrayList<DistanceConstraint>(1); |
| Eliza Margaretha | 0192918 | 2014-02-19 11:48:59 +0000 | [diff] [blame] | 95 | this.constraints.add(new DistanceConstraint(min, max,false)); |
| Nils Diewald | d7cb0eb | 2014-02-12 23:06:10 +0000 | [diff] [blame] | 96 | return this; |
| 97 | }; |
| 98 | |
| Nils Diewald | 164f8be | 2014-02-13 02:43:16 +0000 | [diff] [blame] | 99 | public SpanSequenceQueryWrapper withConstraint (int min, int max, String unit) { |
| 100 | if (this.constraints == null) |
| 101 | this.constraints = new ArrayList<DistanceConstraint>(1); |
| Nils Diewald | f5f29ff | 2014-02-14 12:24:34 +0000 | [diff] [blame] | 102 | if (unit.equals("w")) |
| Eliza Margaretha | 0192918 | 2014-02-19 11:48:59 +0000 | [diff] [blame] | 103 | this.constraints.add(new DistanceConstraint(min, max,false)); |
| Nils Diewald | f5f29ff | 2014-02-14 12:24:34 +0000 | [diff] [blame] | 104 | else |
| 105 | this.constraints.add( |
| 106 | new DistanceConstraint( |
| Eliza Margaretha | 0192918 | 2014-02-19 11:48:59 +0000 | [diff] [blame] | 107 | new SpanElementQuery(this.field, unit), min, max,false) |
| Nils Diewald | f5f29ff | 2014-02-14 12:24:34 +0000 | [diff] [blame] | 108 | ); |
| Nils Diewald | d7cb0eb | 2014-02-12 23:06:10 +0000 | [diff] [blame] | 109 | return this; |
| 110 | }; |
| 111 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 112 | public SpanQuery toQuery () { |
| 113 | if (this.segments.size() == 0) { |
| 114 | return (SpanQuery) null; |
| 115 | }; |
| 116 | |
| 117 | SpanQuery query = this.segments.get(0); |
| 118 | |
| Nils Diewald | d7cb0eb | 2014-02-12 23:06:10 +0000 | [diff] [blame] | 119 | // NextQueries: |
| Nils Diewald | 164f8be | 2014-02-13 02:43:16 +0000 | [diff] [blame] | 120 | if (this.constraints == null || this.constraints.size() == 0) { |
| Nils Diewald | d7cb0eb | 2014-02-12 23:06:10 +0000 | [diff] [blame] | 121 | for (int i = 1; i < this.segments.size(); i++) { |
| 122 | query = new SpanNextQuery( |
| 123 | query, |
| 124 | this.segments.get(i) // Todo: Maybe payloads are not necessary |
| 125 | ); |
| 126 | }; |
| 127 | return (SpanQuery) query; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 128 | }; |
| Nils Diewald | d7cb0eb | 2014-02-12 23:06:10 +0000 | [diff] [blame] | 129 | |
| 130 | // DistanceQueries |
| 131 | if (this.constraints.size() == 1) { |
| Nils Diewald | 164f8be | 2014-02-13 02:43:16 +0000 | [diff] [blame] | 132 | DistanceConstraint constraint = this.constraints.get(0); |
| 133 | |
| 134 | // Create spanElementDistance query |
| 135 | if (!constraint.getUnit().equals("w")) { |
| 136 | for (int i = 1; i < this.segments.size(); i++) { |
| 137 | query = new SpanDistanceQuery( |
| Eliza Margaretha | 609fcc6 | 2014-02-13 14:10:20 +0000 | [diff] [blame] | 138 | new SpanElementQuery(this.field, constraint.getUnit()), |
| Nils Diewald | 164f8be | 2014-02-13 02:43:16 +0000 | [diff] [blame] | 139 | query, |
| 140 | this.segments.get(i), |
| 141 | constraint.getMinDistance(), |
| 142 | constraint.getMaxDistance(), |
| 143 | this.isInOrder(), |
| 144 | true |
| 145 | ); |
| 146 | }; |
| 147 | } |
| 148 | |
| 149 | // Create spanDistance query |
| 150 | else { |
| 151 | for (int i = 1; i < this.segments.size(); i++) { |
| 152 | query = new SpanDistanceQuery( |
| 153 | query, |
| 154 | this.segments.get(i), |
| 155 | constraint.getMinDistance(), |
| 156 | constraint.getMaxDistance(), |
| 157 | this.isInOrder(), |
| 158 | true |
| 159 | ); |
| 160 | }; |
| 161 | }; |
| 162 | |
| 163 | return (SpanQuery) query; |
| Nils Diewald | d7cb0eb | 2014-02-12 23:06:10 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
| Nils Diewald | 164f8be | 2014-02-13 02:43:16 +0000 | [diff] [blame] | 166 | // MultipleDistanceQueries |
| 167 | for (int i = 1; i < this.segments.size(); i++) { |
| 168 | query = new SpanMultipleDistanceQuery( |
| 169 | query, |
| 170 | this.segments.get(i), |
| 171 | this.constraints, |
| 172 | this.isInOrder(), |
| 173 | true |
| 174 | ); |
| 175 | }; |
| Nils Diewald | d7cb0eb | 2014-02-12 23:06:10 +0000 | [diff] [blame] | 176 | |
| Nils Diewald | 164f8be | 2014-02-13 02:43:16 +0000 | [diff] [blame] | 177 | return (SpanQuery) query; |
| Nils Diewald | d7cb0eb | 2014-02-12 23:06:10 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | public void setInOrder (boolean isInOrder) { |
| 181 | this.isInOrder = isInOrder; |
| 182 | }; |
| 183 | |
| 184 | public boolean isInOrder () { |
| 185 | return this.isInOrder; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 186 | }; |
| Nils Diewald | 164f8be | 2014-02-13 02:43:16 +0000 | [diff] [blame] | 187 | |
| 188 | public boolean hasConstraints () { |
| 189 | if (this.constraints == null) |
| 190 | return false; |
| 191 | if (this.constraints.size() <= 0) |
| 192 | return false; |
| 193 | return true; |
| 194 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 195 | }; |