blob: 91ebaa63df060ec4c59bbba064169cbac07b8eee [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
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000059 if (spanContext.equals("sentence")) {
60 spanContext = "s";
61 }
62 else if (spanContext.equals("paragraph")) {
63 spanContext = "p";
64 };
Nils Diewaldbb33da22015-03-04 16:24:25 +000065
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000066 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 Diewaldbb33da22015-03-04 16:24:25 +000074
75
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000076 public boolean isToken () {
77 return this.type;
78 };
Nils Diewaldbb33da22015-03-04 16:24:25 +000079
80
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000081 public boolean isCharacter () {
82 return !(this.type);
83 };
84
Nils Diewaldbb33da22015-03-04 16:24:25 +000085
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000086 public SearchContextSide setToken (boolean value) {
87 this.type = value;
88 return this;
89 };
90
Nils Diewaldbb33da22015-03-04 16:24:25 +000091
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000092 public SearchContextSide setCharacter (boolean value) {
93 this.type = !(value);
94 return this;
95 };
96
Nils Diewaldbb33da22015-03-04 16:24:25 +000097
98 public short getLength () {
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000099 return this.length;
100 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000101
102
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000103 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 Diewaldbb33da22015-03-04 16:24:25 +0000114
115
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000116 public SearchContextSide setLength (int value) {
117 return this.setLength((short) value);
118 };
119
Nils Diewaldbb33da22015-03-04 16:24:25 +0000120
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000121 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 Diewaldbb33da22015-03-04 16:24:25 +0000138
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000139 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 Diewaldbb33da22015-03-04 16:24:25 +0000147
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000148 public JsonNode toJsonNode () {
Nils Diewaldbb33da22015-03-04 16:24:25 +0000149
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000150 if (this.isSpanDefined())
151 return new TextNode(this.spanContext);
Nils Diewaldbb33da22015-03-04 16:24:25 +0000152
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000153 ArrayNode leftContext = mapper.createArrayNode();
154 leftContext.add(this.left.isToken() ? "token" : "char");
155 leftContext.add(this.left.getLength());
Nils Diewaldbb33da22015-03-04 16:24:25 +0000156
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000157 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 Diewaldbb33da22015-03-04 16:24:25 +0000164
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000165 return context;
166 };
167};