blob: 90745124d0d4d6e905759af9d2570dbe787b3970 [file] [log] [blame]
Nils Diewald1e5d5942014-05-20 13:29:53 +00001package de.ids_mannheim.korap.index;
2import com.fasterxml.jackson.databind.ObjectMapper;
3import com.fasterxml.jackson.databind.SerializationFeature;
4import com.fasterxml.jackson.databind.JsonNode;
5import com.fasterxml.jackson.databind.node.*;
6import com.fasterxml.jackson.annotation.*;
7
8
9public class SearchContext {
10 ObjectMapper mapper = new ObjectMapper();
11
12
13 private boolean spanType = false;
14
15 @JsonIgnore
16 public SearchContextSide left, right;
17
18 @JsonIgnore
19 public String spanContext;
20
21 {
22 left = new SearchContextSide();
23 right = new SearchContextSide();
24 };
25
26 public SearchContext () {};
27
28 public SearchContext (String spanContext) {
29 this.spanType = true;
30 this.spanContext = spanContext;
31 };
32
33 public SearchContext (boolean leftTokenContext,
34 short leftContext,
35 boolean rightTokenContext,
36 short rightContext) {
37 this.spanType = false;
38 this.left.setToken(leftTokenContext);
39 this.left.setLength(leftContext);
40 this.right.setToken(leftTokenContext);
41 this.right.setLength(rightContext);
42 };
43
44 public boolean isSpanDefined () {
45 return this.spanType;
46 };
47
48 public String getSpanContext () {
49 return this.spanContext;
50 };
51
52 public SearchContext setSpanContext (String spanContext) {
53 this.spanType = true;
54
55 if (spanContext.equals("sentence")) {
56 spanContext = "s";
57 }
58 else if (spanContext.equals("paragraph")) {
59 spanContext = "p";
60 };
61
62 this.spanContext = spanContext;
63 return this;
64 };
65
66 public class SearchContextSide {
67 private boolean type = true;
68 private short length = 6;
69 private short maxLength = 500;
70
71 public boolean isToken () {
72 return this.type;
73 };
74
75 public boolean isCharacter () {
76 return !(this.type);
77 };
78
79 public SearchContextSide setToken (boolean value) {
80 this.type = value;
81 return this;
82 };
83
84 public SearchContextSide setCharacter (boolean value) {
85 this.type = !(value);
86 return this;
87 };
88
89 public short getLength() {
90 return this.length;
91 };
92
93 public SearchContextSide setLength (short value) {
94 if (value >= 0) {
95 if (value <= maxLength) {
96 this.length = value;
97 }
98 else {
99 this.length = this.maxLength;
100 };
101 };
102 return this;
103 };
104
105 public SearchContextSide setLength (int value) {
106 return this.setLength((short) value);
107 };
108
109 public void fromJSON (JsonNode json) {
110 String type = json.get(0).asText();
111 if (type.equals("token")) {
112 this.setToken(true);
113 }
114 else if (type.equals("char")) {
115 this.setCharacter(true);
116 };
117 this.setLength(json.get(1).asInt(this.length));
118 };
119 };
120
121
122 public void fromJSON (JsonNode context) {
123 if (context.isContainerNode()) {
124 if (context.has("left"))
125 this.left.fromJSON(context.get("left"));
126
127 if (context.has("right"))
128 this.right.fromJSON(context.get("right"));
129 }
130 else if (context.isValueNode()) {
131 this.setSpanContext(context.asText());
132 };
133 };
134
Nils Diewaldec336122014-05-21 17:09:50 +0000135 public JsonNode toJSON () {
Nils Diewald7cf8c6d2014-05-28 18:37:38 +0000136
137 if (this.isSpanDefined())
Nils Diewaldec336122014-05-21 17:09:50 +0000138 return new TextNode(this.spanContext);
Nils Diewaldec336122014-05-21 17:09:50 +0000139
Nils Diewald1e5d5942014-05-20 13:29:53 +0000140 ArrayNode leftContext = mapper.createArrayNode();
141 leftContext.add(this.left.isToken() ? "token" : "char");
142 leftContext.add(this.left.getLength());
143
144 ArrayNode rightContext = mapper.createArrayNode();
145 rightContext.add(this.right.isToken() ? "token" : "char");
146 rightContext.add(this.right.getLength());
147
148 ObjectNode context = mapper.createObjectNode();
149 context.put("left", leftContext);
150 context.put("right", rightContext);
151
152 return context;
153 };
154
155};