| 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) { |
| Akron | 35c2d0d | 2017-02-15 11:16:22 +0100 | [diff] [blame] | 30 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 31 | if (!subquery.isEmpty()) { |
| 32 | this.subquery = subquery; |
| Akron | a26184e | 2018-12-05 15:37:34 +0100 | [diff] [blame] | 33 | this.maybeUnsorted = subquery.maybeUnsorted(); |
| Akron | 35c2d0d | 2017-02-15 11:16:22 +0100 | [diff] [blame] | 34 | this.isEmpty = false; |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 35 | } |
| 36 | else |
| 37 | this.isEmpty = true; |
| Nils Diewald | 6b33281 | 2014-07-22 18:51:05 +0000 | [diff] [blame] | 38 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 39 | if (exact < 1 || this.subquery.isNull()) { |
| 40 | this.isNull = true; |
| 41 | this.isOptional = true; |
| 42 | this.min = 0; |
| 43 | this.max = 0; |
| 44 | return; |
| 45 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 46 | |
| Akron | 35c2d0d | 2017-02-15 11:16:22 +0100 | [diff] [blame] | 47 | this.isNull = false; |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 48 | this.min = exact; |
| 49 | this.max = exact; |
| Nils Diewald | ee4a6b7 | 2014-06-30 18:23:12 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 52 | |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 53 | // 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] | 54 | public SpanRepetitionQueryWrapper (SpanQueryWrapper subquery, int min, |
| 55 | int max) { |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 56 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 57 | if (!subquery.isEmpty()) { |
| 58 | this.subquery = subquery; |
| Nils Diewald | 6b33281 | 2014-07-22 18:51:05 +0000 | [diff] [blame] | 59 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 60 | if (subquery.maybeUnsorted()) |
| 61 | this.maybeUnsorted = true; |
| 62 | } |
| 63 | else |
| 64 | this.isEmpty = true; |
| 65 | |
| 66 | // Subquery may be an empty token |
| Akron | 747986e | 2016-02-18 17:07:12 +0100 | [diff] [blame] | 67 | if (subquery.isNull()) { |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 68 | this.isNull = true; |
| 69 | return; |
| 70 | } |
| 71 | else { |
| 72 | this.isNull = false; |
| 73 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 74 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 75 | if (min == 0) { |
| 76 | this.isOptional = true; |
| 77 | min = 1; |
| 78 | if (max == 0) |
| 79 | this.isNull = true; |
| 80 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 81 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 82 | this.min = min; |
| 83 | this.max = max; |
| Nils Diewald | ee4a6b7 | 2014-06-30 18:23:12 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 86 | |
| 87 | // Serialize to Lucene SpanQuery |
| Akron | a7b936d | 2016-03-04 13:40:54 +0100 | [diff] [blame] | 88 | @Override |
| 89 | public SpanQuery toFragmentQuery () throws QueryException { |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 90 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 91 | // The query is null |
| 92 | if (this.isNull) |
| 93 | return (SpanQuery) null; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 94 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 95 | if (this.isEmpty) { |
| 96 | log.error("You can't queryize an empty query"); |
| 97 | return (SpanQuery) null; |
| 98 | }; |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 99 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 100 | // The query is not a repetition query at all, but may be optional |
| 101 | if (this.min == 1 && this.max == 1) |
| Akron | 6759b04 | 2016-04-28 01:25:00 +0200 | [diff] [blame] | 102 | return this.subquery.retrieveNode(this.retrieveNode) |
| 103 | .toFragmentQuery(); |
| Nils Diewald | be5943e | 2014-10-21 19:35:34 +0000 | [diff] [blame] | 104 | |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 105 | // That's a fine repetition query |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 106 | return new SpanRepetitionQuery( |
| 107 | this.subquery.retrieveNode(this.retrieveNode).toFragmentQuery(), |
| 108 | this.min, this.max, true); |
| Nils Diewald | 6b33281 | 2014-07-22 18:51:05 +0000 | [diff] [blame] | 109 | }; |
| Nils Diewald | cc7c0b3 | 2014-07-31 19:58:22 +0000 | [diff] [blame] | 110 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 111 | |
| Nils Diewald | cc7c0b3 | 2014-07-31 19:58:22 +0000 | [diff] [blame] | 112 | public boolean isNegative () { |
| Nils Diewald | 0981c21 | 2015-02-13 20:47:10 +0000 | [diff] [blame] | 113 | if (this.subquery == null) |
| 114 | return false; |
| 115 | return this.subquery.isNegative(); |
| Nils Diewald | cc7c0b3 | 2014-07-31 19:58:22 +0000 | [diff] [blame] | 116 | }; |
| Nils Diewald | ee4a6b7 | 2014-06-30 18:23:12 +0000 | [diff] [blame] | 117 | }; |