| 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 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 59 | if (spanContext.equals("sentence")) { |
| 60 | spanContext = "s"; |
| 61 | } |
| 62 | else if (spanContext.equals("paragraph")) { |
| 63 | spanContext = "p"; |
| 64 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 65 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 66 | this.spanContext = spanContext; |
| 67 | return this; |
| 68 | }; |
| 69 | |
| 70 | public class SearchContextSide { |
| 71 | private boolean type = true; |
| 72 | private short length = 6; |
| 73 | private short maxLength = 500; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 74 | |
| 75 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 76 | public boolean isToken () { |
| 77 | return this.type; |
| 78 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 79 | |
| 80 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 81 | public boolean isCharacter () { |
| 82 | return !(this.type); |
| 83 | }; |
| 84 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 85 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 86 | public SearchContextSide setToken (boolean value) { |
| 87 | this.type = value; |
| 88 | return this; |
| 89 | }; |
| 90 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 91 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 92 | public SearchContextSide setCharacter (boolean value) { |
| 93 | this.type = !(value); |
| 94 | return this; |
| 95 | }; |
| 96 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 97 | |
| 98 | public short getLength () { |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 99 | return this.length; |
| 100 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 101 | |
| 102 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 103 | public SearchContextSide setLength (short value) { |
| 104 | if (value >= 0) { |
| 105 | if (value <= maxLength) { |
| 106 | this.length = value; |
| 107 | } |
| 108 | else { |
| 109 | this.length = this.maxLength; |
| 110 | }; |
| 111 | }; |
| 112 | return this; |
| 113 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 114 | |
| 115 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 116 | public SearchContextSide setLength (int value) { |
| 117 | return this.setLength((short) value); |
| 118 | }; |
| 119 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 120 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 121 | public void fromJson (JsonNode json) { |
| 122 | String type = json.get(0).asText(); |
| 123 | if (type.equals("token")) { |
| 124 | this.setToken(true); |
| 125 | } |
| 126 | else if (type.equals("char")) { |
| 127 | this.setCharacter(true); |
| 128 | }; |
| 129 | this.setLength(json.get(1).asInt(this.length)); |
| 130 | }; |
| 131 | }; |
| 132 | |
| 133 | |
| 134 | public void fromJson (JsonNode context) { |
| 135 | if (context.isContainerNode()) { |
| 136 | if (context.has("left")) |
| 137 | this.left.fromJson(context.get("left")); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 138 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 139 | if (context.has("right")) |
| 140 | this.right.fromJson(context.get("right")); |
| 141 | } |
| 142 | else if (context.isValueNode()) { |
| 143 | this.setSpanContext(context.asText()); |
| 144 | }; |
| 145 | }; |
| 146 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 147 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 148 | public JsonNode toJsonNode () { |
| 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 | if (this.isSpanDefined()) |
| 151 | return new TextNode(this.spanContext); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 152 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 153 | ArrayNode leftContext = mapper.createArrayNode(); |
| 154 | leftContext.add(this.left.isToken() ? "token" : "char"); |
| 155 | leftContext.add(this.left.getLength()); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 156 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 157 | ArrayNode rightContext = mapper.createArrayNode(); |
| 158 | rightContext.add(this.right.isToken() ? "token" : "char"); |
| 159 | rightContext.add(this.right.getLength()); |
| 160 | |
| 161 | ObjectNode context = mapper.createObjectNode(); |
| 162 | context.put("left", leftContext); |
| 163 | context.put("right", rightContext); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 164 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 165 | return context; |
| 166 | }; |
| 167 | }; |