| Nils Diewald | 65449ff | 2015-02-27 17:57:29 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.response; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 2 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 3 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | import com.fasterxml.jackson.databind.SerializationFeature; |
| 5 | import com.fasterxml.jackson.databind.JsonNode; |
| 6 | import com.fasterxml.jackson.databind.node.*; |
| 7 | import com.fasterxml.jackson.annotation.*; |
| 8 | |
| 9 | |
| 10 | public class SearchContext { |
| 11 | ObjectMapper mapper = new ObjectMapper(); |
| 12 | |
| 13 | private boolean spanType = false; |
| 14 | |
| 15 | @JsonIgnore |
| 16 | public SearchContextSide left, right; |
| 17 | |
| 18 | @JsonIgnore |
| 19 | public String spanContext; |
| 20 | |
| 21 | { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 22 | left = new SearchContextSide(); |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 23 | right = new SearchContextSide(); |
| 24 | }; |
| 25 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 26 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 27 | public SearchContext () {}; |
| 28 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 29 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 30 | public SearchContext (String spanContext) { |
| 31 | this.spanType = true; |
| 32 | this.spanContext = spanContext; |
| 33 | }; |
| 34 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 35 | |
| 36 | public SearchContext (boolean leftTokenContext, short leftContext, |
| 37 | boolean rightTokenContext, short rightContext) { |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 38 | this.spanType = false; |
| 39 | this.left.setToken(leftTokenContext); |
| 40 | this.left.setLength(leftContext); |
| 41 | this.right.setToken(leftTokenContext); |
| 42 | this.right.setLength(rightContext); |
| 43 | }; |
| 44 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 45 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 46 | public boolean isSpanDefined () { |
| 47 | return this.spanType; |
| 48 | }; |
| 49 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 50 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 51 | public String getSpanContext () { |
| 52 | return this.spanContext; |
| 53 | }; |
| 54 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 55 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 56 | public SearchContext setSpanContext (String spanContext) { |
| 57 | this.spanType = true; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 58 | |
| Akron | 48937e9 | 2015-06-26 01:49:02 +0200 | [diff] [blame] | 59 | // <LEGACY> |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 60 | if (spanContext.equals("sentence")) { |
| Akron | 43cea66 | 2016-02-15 23:43:59 +0100 | [diff] [blame] | 61 | spanContext = "base/s:s"; |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 62 | } |
| 63 | else if (spanContext.equals("paragraph")) { |
| Akron | 43cea66 | 2016-02-15 23:43:59 +0100 | [diff] [blame] | 64 | spanContext = "base/s:p"; |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 65 | }; |
| Akron | 48937e9 | 2015-06-26 01:49:02 +0200 | [diff] [blame] | 66 | // </LEGACY> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 67 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 68 | this.spanContext = spanContext; |
| 69 | return this; |
| 70 | }; |
| 71 | |
| 72 | public class SearchContextSide { |
| 73 | private boolean type = true; |
| 74 | private short length = 6; |
| 75 | private short maxLength = 500; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 76 | |
| 77 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 78 | public boolean isToken () { |
| 79 | return this.type; |
| 80 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 81 | |
| 82 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 83 | public boolean isCharacter () { |
| 84 | return !(this.type); |
| 85 | }; |
| 86 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 87 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 88 | public SearchContextSide setToken (boolean value) { |
| 89 | this.type = value; |
| 90 | return this; |
| 91 | }; |
| 92 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 93 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 94 | public SearchContextSide setCharacter (boolean value) { |
| 95 | this.type = !(value); |
| 96 | return this; |
| 97 | }; |
| 98 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 99 | |
| 100 | public short getLength () { |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 101 | return this.length; |
| 102 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 103 | |
| 104 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 105 | public SearchContextSide setLength (short value) { |
| 106 | if (value >= 0) { |
| 107 | if (value <= maxLength) { |
| 108 | this.length = value; |
| 109 | } |
| 110 | else { |
| 111 | this.length = this.maxLength; |
| 112 | }; |
| 113 | }; |
| 114 | return this; |
| 115 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 116 | |
| 117 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 118 | public SearchContextSide setLength (int value) { |
| 119 | return this.setLength((short) value); |
| 120 | }; |
| 121 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 122 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 123 | public void fromJson (JsonNode json) { |
| 124 | String type = json.get(0).asText(); |
| 125 | if (type.equals("token")) { |
| 126 | this.setToken(true); |
| 127 | } |
| 128 | else if (type.equals("char")) { |
| 129 | this.setCharacter(true); |
| 130 | }; |
| 131 | this.setLength(json.get(1).asInt(this.length)); |
| 132 | }; |
| 133 | }; |
| 134 | |
| 135 | |
| 136 | public void fromJson (JsonNode context) { |
| 137 | if (context.isContainerNode()) { |
| 138 | if (context.has("left")) |
| 139 | this.left.fromJson(context.get("left")); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 140 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 141 | if (context.has("right")) |
| 142 | this.right.fromJson(context.get("right")); |
| 143 | } |
| 144 | else if (context.isValueNode()) { |
| 145 | this.setSpanContext(context.asText()); |
| 146 | }; |
| 147 | }; |
| 148 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 149 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 150 | public JsonNode toJsonNode () { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 151 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 152 | if (this.isSpanDefined()) |
| 153 | return new TextNode(this.spanContext); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 154 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 155 | ArrayNode leftContext = mapper.createArrayNode(); |
| 156 | leftContext.add(this.left.isToken() ? "token" : "char"); |
| 157 | leftContext.add(this.left.getLength()); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 158 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 159 | ArrayNode rightContext = mapper.createArrayNode(); |
| 160 | rightContext.add(this.right.isToken() ? "token" : "char"); |
| 161 | rightContext.add(this.right.getLength()); |
| 162 | |
| 163 | ObjectNode context = mapper.createObjectNode(); |
| 164 | context.put("left", leftContext); |
| 165 | context.put("right", rightContext); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 166 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 167 | return context; |
| 168 | }; |
| 169 | }; |