blob: 22963a577a59be7480ba2e82e3e13c74e0688dc2 [file] [log] [blame]
Michael Hanled5658f2014-02-07 22:24:46 +00001package de.ids_mannheim.korap.query.serialize;
2
Michael Hanlc1c3b5d2014-08-04 16:57:46 +00003import lombok.Data;
4
Michael Hanl034be0d2014-02-14 10:17:34 +00005import java.util.LinkedHashMap;
Michael Hanled5658f2014-02-07 22:24:46 +00006import java.util.LinkedList;
7import java.util.List;
8import java.util.Map;
9
10/**
11 * @author hanl
12 * @date 07/02/2014
13 */
Michael Hanldf206ab2014-05-13 10:22:27 +000014public class MetaQueryBuilder {
Michael Hanled5658f2014-02-07 22:24:46 +000015
Michael Hanled5658f2014-02-07 22:24:46 +000016 private Map meta;
Michael Hanlc1c3b5d2014-08-04 16:57:46 +000017 private SpanContext spanContext;
Michael Hanled5658f2014-02-07 22:24:46 +000018
Michael Hanlf33f7062015-06-24 21:14:26 +020019 public MetaQueryBuilder() {
Michael Hanl034be0d2014-02-14 10:17:34 +000020 this.meta = new LinkedHashMap();
Michael Hanlf33f7062015-06-24 21:14:26 +020021 this.meta.put("fields", new LinkedList<>());
Michael Hanled5658f2014-02-07 22:24:46 +000022 }
23
Michael Hanlb9f3fd12014-06-03 11:38:27 +000024 /**
25 * context segment if context is either of type char or token.
26 * size can differ for left and right span
Michael Hanlf33f7062015-06-24 21:14:26 +020027 *
Michael Hanlb9f3fd12014-06-03 11:38:27 +000028 * @param left
29 * @param leftType
30 * @param right
31 * @param rightType
32 * @return
33 */
Michael Hanlf33f7062015-06-24 21:14:26 +020034 public MetaQueryBuilder setSpanContext(Integer left, String leftType,
Joachim Bingel20e06ac2015-01-15 10:31:33 +000035 Integer right, String rightType) {
Michael Hanlc1c3b5d2014-08-04 16:57:46 +000036 this.spanContext = new SpanContext(left, leftType, right, rightType);
Michael Hanled5658f2014-02-07 22:24:46 +000037 return this;
38 }
39
Michael Hanlf33f7062015-06-24 21:14:26 +020040 public SpanContext getSpanContext() {
Michael Hanlc1c3b5d2014-08-04 16:57:46 +000041 return this.spanContext;
42 }
43
Michael Hanlb9f3fd12014-06-03 11:38:27 +000044 /**
Joachim Bingel20e06ac2015-01-15 10:31:33 +000045 * context if of type paragraph or sentence where left and right
46 * size delimiters are irrelevant; or 2-token, 2-char p/paragraph,
Michael Hanl93518e42015-06-26 16:18:06 +020047 * s/sentence or token, char.
48 * Distinguish
Michael Hanlf33f7062015-06-24 21:14:26 +020049 *
Michael Hanl1b7f54a2014-07-25 17:35:55 +000050 * @param context
Michael Hanlb9f3fd12014-06-03 11:38:27 +000051 * @return
52 */
Michael Hanlf33f7062015-06-24 21:14:26 +020053 public MetaQueryBuilder setSpanContext(String context) {
Michael Hanl93518e42015-06-26 16:18:06 +020054 if (context != null) {
55 if (!context.contains("-") || context.contains(":"))
56 this.spanContext = new SpanContext(context);
57 else {
58 String[] ct = context.replaceAll("\\s+", "").split(",");
59 String[] lc = ct[0].split("-");
60 String[] rc = ct[1].split("-");
61 this.spanContext = new SpanContext(Integer.valueOf(lc[0]),
62 lc[1], Integer.valueOf(rc[0]), rc[1]);
63 }
Michael Hanl1b7f54a2014-07-25 17:35:55 +000064 }
Michael Hanlb9f3fd12014-06-03 11:38:27 +000065 return this;
66 }
67
Michael Hanlf33f7062015-06-24 21:14:26 +020068 public MetaQueryBuilder addEntry(String name, Object value) {
Michael Hanl93518e42015-06-26 16:18:06 +020069 if (value != null)
70 meta.put(name, value);
Michael Hanled5658f2014-02-07 22:24:46 +000071 return this;
72 }
73
Michael Hanlf33f7062015-06-24 21:14:26 +020074 public Map raw() {
Michael Hanldd5c9652014-09-02 18:51:08 +000075 if (this.spanContext != null)
76 meta.putAll(this.spanContext.raw());
Michael Hanl034be0d2014-02-14 10:17:34 +000077 return meta;
78 }
Michael Hanlc1c3b5d2014-08-04 16:57:46 +000079
Michael Hanl93518e42015-06-26 16:18:06 +020080
81
Michael Hanlc1c3b5d2014-08-04 16:57:46 +000082 @Data
83 public class SpanContext {
84 private String left_type;
85 private String right_type;
86 private int left_size;
87 private int right_size;
88 private String context = null;
89
Michael Hanlc1c3b5d2014-08-04 16:57:46 +000090 /**
91 * context segment if context is either of type char or token.
92 * size can differ for left and right span
Michael Hanlf33f7062015-06-24 21:14:26 +020093 *
Michael Hanlc1c3b5d2014-08-04 16:57:46 +000094 * @param ls
95 * @param lt
96 * @param rs
97 * @param rt
98 * @return
99 */
Michael Hanlf33f7062015-06-24 21:14:26 +0200100 public SpanContext(int ls, String lt, int rs, String rt) {
Michael Hanlc1c3b5d2014-08-04 16:57:46 +0000101 this.left_type = lt;
102 this.left_size = ls;
103 this.right_type = rt;
104 this.right_size = rs;
105 }
106
Michael Hanlf33f7062015-06-24 21:14:26 +0200107 public SpanContext(String context) {
Michael Hanlc1c3b5d2014-08-04 16:57:46 +0000108 this.context = context;
109 }
110
Michael Hanlf33f7062015-06-24 21:14:26 +0200111 public Map raw() {
Michael Hanlc1c3b5d2014-08-04 16:57:46 +0000112 Map meta = new LinkedHashMap();
113 if (this.context == null) {
114 Map map = new LinkedHashMap();
115 List l = new LinkedList();
116 List r = new LinkedList();
117 l.add(this.left_type);
118 l.add(this.left_size);
119 map.put("left", l);
120 r.add(this.right_type);
121 r.add(this.right_size);
122 map.put("right", r);
123 meta.put("context", map);
Michael Hanlf33f7062015-06-24 21:14:26 +0200124 }else
Michael Hanlc1c3b5d2014-08-04 16:57:46 +0000125 meta.put("context", this.context);
126 return meta;
127 }
128 }
Michael Hanled5658f2014-02-07 22:24:46 +0000129}