blob: 092cd67ca3bb1350980365e3d41ad37135f4c547 [file] [log] [blame]
Nils Diewald65449ff2015-02-27 17:57:29 +00001package de.ids_mannheim.korap.response;
Nils Diewaldbb33da22015-03-04 16:24:25 +00002
Nils Diewaldf5ab4b22015-02-25 20:55:16 +00003import com.fasterxml.jackson.databind.ObjectMapper;
4import com.fasterxml.jackson.databind.SerializationFeature;
5import com.fasterxml.jackson.databind.JsonNode;
6import com.fasterxml.jackson.databind.node.*;
7import com.fasterxml.jackson.annotation.*;
8
9
10public 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 Diewaldbb33da22015-03-04 16:24:25 +000022 left = new SearchContextSide();
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000023 right = new SearchContextSide();
24 };
25
Nils Diewaldbb33da22015-03-04 16:24:25 +000026
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000027 public SearchContext () {};
28
Nils Diewaldbb33da22015-03-04 16:24:25 +000029
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000030 public SearchContext (String spanContext) {
31 this.spanType = true;
32 this.spanContext = spanContext;
33 };
34
Nils Diewaldbb33da22015-03-04 16:24:25 +000035
36 public SearchContext (boolean leftTokenContext, short leftContext,
37 boolean rightTokenContext, short rightContext) {
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000038 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 Diewaldbb33da22015-03-04 16:24:25 +000045
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000046 public boolean isSpanDefined () {
47 return this.spanType;
48 };
49
Nils Diewaldbb33da22015-03-04 16:24:25 +000050
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000051 public String getSpanContext () {
52 return this.spanContext;
53 };
54
Nils Diewaldbb33da22015-03-04 16:24:25 +000055
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000056 public SearchContext setSpanContext (String spanContext) {
57 this.spanType = true;
Nils Diewaldbb33da22015-03-04 16:24:25 +000058
Akron48937e92015-06-26 01:49:02 +020059 // <LEGACY>
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000060 if (spanContext.equals("sentence")) {
Akron43cea662016-02-15 23:43:59 +010061 spanContext = "base/s:s";
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000062 }
63 else if (spanContext.equals("paragraph")) {
Akron43cea662016-02-15 23:43:59 +010064 spanContext = "base/s:p";
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000065 };
Akron48937e92015-06-26 01:49:02 +020066 // </LEGACY>
Nils Diewaldbb33da22015-03-04 16:24:25 +000067
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000068 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 Diewaldbb33da22015-03-04 16:24:25 +000076
77
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000078 public boolean isToken () {
79 return this.type;
80 };
Nils Diewaldbb33da22015-03-04 16:24:25 +000081
82
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000083 public boolean isCharacter () {
84 return !(this.type);
85 };
86
Nils Diewaldbb33da22015-03-04 16:24:25 +000087
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000088 public SearchContextSide setToken (boolean value) {
89 this.type = value;
90 return this;
91 };
92
Nils Diewaldbb33da22015-03-04 16:24:25 +000093
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000094 public SearchContextSide setCharacter (boolean value) {
95 this.type = !(value);
96 return this;
97 };
98
Nils Diewaldbb33da22015-03-04 16:24:25 +000099
100 public short getLength () {
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000101 return this.length;
102 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000103
104
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000105 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 Diewaldbb33da22015-03-04 16:24:25 +0000116
117
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000118 public SearchContextSide setLength (int value) {
119 return this.setLength((short) value);
120 };
121
Nils Diewaldbb33da22015-03-04 16:24:25 +0000122
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000123 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 Diewaldbb33da22015-03-04 16:24:25 +0000140
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000141 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 Diewaldbb33da22015-03-04 16:24:25 +0000149
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000150 public JsonNode toJsonNode () {
Nils Diewaldbb33da22015-03-04 16:24:25 +0000151
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000152 if (this.isSpanDefined())
153 return new TextNode(this.spanContext);
Nils Diewaldbb33da22015-03-04 16:24:25 +0000154
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000155 ArrayNode leftContext = mapper.createArrayNode();
156 leftContext.add(this.left.isToken() ? "token" : "char");
157 leftContext.add(this.left.getLength());
Nils Diewaldbb33da22015-03-04 16:24:25 +0000158
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000159 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 Diewaldbb33da22015-03-04 16:24:25 +0000166
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000167 return context;
168 };
169};