| Nils Diewald | ee4a6b7 | 2014-06-30 18:23:12 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.query.wrap; |
| 2 | |
| 3 | import org.apache.lucene.search.spans.SpanQuery; |
| 4 | |
| 5 | import de.ids_mannheim.korap.query.SpanRepetitionQuery; |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 6 | import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper; |
| Nils Diewald | be5943e | 2014-10-21 19:35:34 +0000 | [diff] [blame] | 7 | import de.ids_mannheim.korap.util.QueryException; |
| Nils Diewald | ee4a6b7 | 2014-06-30 18:23:12 +0000 | [diff] [blame] | 8 | |
| Nils Diewald | be5943e | 2014-10-21 19:35:34 +0000 | [diff] [blame] | 9 | import org.slf4j.Logger; |
| 10 | import org.slf4j.LoggerFactory; |
| 11 | |
| 12 | // DEAL WITH NEGATIVITY |
| Nils Diewald | ee4a6b7 | 2014-06-30 18:23:12 +0000 | [diff] [blame] | 13 | |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 14 | public class SpanRepetitionQueryWrapper extends SpanQueryWrapper { |
| 15 | private SpanQueryWrapper subquery; |
| Nils Diewald | ee4a6b7 | 2014-06-30 18:23:12 +0000 | [diff] [blame] | 16 | |
| Nils Diewald | be5943e | 2014-10-21 19:35:34 +0000 | [diff] [blame] | 17 | // Logger |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 18 | private final static Logger log = LoggerFactory |
| 19 | .getLogger(SpanSequenceQueryWrapper.class); |
| 20 | |
| Nils Diewald | be5943e | 2014-10-21 19:35:34 +0000 | [diff] [blame] | 21 | |
| 22 | public SpanRepetitionQueryWrapper () { |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 23 | this.isEmpty = true; |
| 24 | this.isNull = false; |
| Nils Diewald | be5943e | 2014-10-21 19:35:34 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 27 | |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 28 | // This is for exact enumbered repetition, like in a{3} |
| 29 | public SpanRepetitionQueryWrapper (SpanQueryWrapper subquery, int exact) { |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 30 | if (!subquery.isEmpty()) { |
| 31 | this.subquery = subquery; |
| 32 | if (subquery.maybeUnsorted()) |
| 33 | this.maybeUnsorted = true; |
| 34 | } |
| 35 | else |
| 36 | this.isEmpty = true; |
| Nils Diewald | 6b33281 | 2014-07-22 18:51:05 +0000 | [diff] [blame] | 37 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 38 | if (exact < 1 || this.subquery.isNull()) { |
| 39 | this.isNull = true; |
| 40 | this.isOptional = true; |
| 41 | this.min = 0; |
| 42 | this.max = 0; |
| 43 | return; |
| 44 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 45 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 46 | this.min = exact; |
| 47 | this.max = exact; |
| Nils Diewald | ee4a6b7 | 2014-06-30 18:23:12 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 50 | |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 51 | // This is for a range of repetitions, like in a{2,3}, a{,4}, a{3,}, a+, a*, a? |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 52 | public SpanRepetitionQueryWrapper (SpanQueryWrapper subquery, int min, |
| 53 | int max) { |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 54 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 55 | if (!subquery.isEmpty()) { |
| 56 | this.subquery = subquery; |
| Nils Diewald | 6b33281 | 2014-07-22 18:51:05 +0000 | [diff] [blame] | 57 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 58 | if (subquery.maybeUnsorted()) |
| 59 | this.maybeUnsorted = true; |
| 60 | } |
| 61 | else |
| 62 | this.isEmpty = true; |
| 63 | |
| 64 | // Subquery may be an empty token |
| Akron | 747986e | 2016-02-18 17:07:12 +0100 | [diff] [blame^] | 65 | if (subquery.isNull()) { |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 66 | this.isNull = true; |
| 67 | return; |
| 68 | } |
| 69 | else { |
| 70 | this.isNull = false; |
| 71 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 72 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 73 | if (min == 0) { |
| 74 | this.isOptional = true; |
| 75 | min = 1; |
| 76 | if (max == 0) |
| 77 | this.isNull = true; |
| 78 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 79 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 80 | this.min = min; |
| 81 | this.max = max; |
| Nils Diewald | ee4a6b7 | 2014-06-30 18:23:12 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 84 | |
| 85 | // Serialize to Lucene SpanQuery |
| Nils Diewald | be5943e | 2014-10-21 19:35:34 +0000 | [diff] [blame] | 86 | public SpanQuery toQuery () throws QueryException { |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 87 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 88 | // The query is null |
| 89 | if (this.isNull) |
| 90 | return (SpanQuery) null; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 91 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 92 | if (this.isEmpty) { |
| 93 | log.error("You can't queryize an empty query"); |
| 94 | return (SpanQuery) null; |
| 95 | }; |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 96 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 97 | // The query is not a repetition query at all, but may be optional |
| 98 | if (this.min == 1 && this.max == 1) |
| Nils Diewald | b9dd413 | 2015-02-16 16:32:41 +0000 | [diff] [blame] | 99 | return this.subquery.retrieveNode(this.retrieveNode).toQuery(); |
| Nils Diewald | be5943e | 2014-10-21 19:35:34 +0000 | [diff] [blame] | 100 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 101 | // That's a fine repetition query |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 102 | return new SpanRepetitionQuery(this.subquery.retrieveNode( |
| 103 | this.retrieveNode).toQuery(), this.min, this.max, true); |
| Nils Diewald | 6b33281 | 2014-07-22 18:51:05 +0000 | [diff] [blame] | 104 | }; |
| Nils Diewald | cc7c0b3 | 2014-07-31 19:58:22 +0000 | [diff] [blame] | 105 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 106 | |
| Nils Diewald | cc7c0b3 | 2014-07-31 19:58:22 +0000 | [diff] [blame] | 107 | public boolean isNegative () { |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 108 | if (this.subquery == null) |
| 109 | return false; |
| 110 | return this.subquery.isNegative(); |
| Nils Diewald | cc7c0b3 | 2014-07-31 19:58:22 +0000 | [diff] [blame] | 111 | }; |
| Nils Diewald | ee4a6b7 | 2014-06-30 18:23:12 +0000 | [diff] [blame] | 112 | }; |