blob: cad43192720e5937bc263269223f4dd3e4da3be0 [file] [log] [blame]
Michael Hanled5658f2014-02-07 22:24:46 +00001package de.ids_mannheim.korap.query.serialize;
2
Michael Hanl034be0d2014-02-14 10:17:34 +00003import java.util.LinkedHashMap;
Michael Hanled5658f2014-02-07 22:24:46 +00004import java.util.LinkedList;
5import java.util.List;
6import java.util.Map;
Michael Hanlfaae0182015-06-26 16:18:06 +02007import java.util.regex.Pattern;
Michael Hanled5658f2014-02-07 22:24:46 +00008
9/**
10 * @author hanl
11 * @date 07/02/2014
12 */
Michael Hanldf206ab2014-05-13 10:22:27 +000013public class MetaQueryBuilder {
Michael Hanled5658f2014-02-07 22:24:46 +000014
Michael Hanlfaae0182015-06-26 16:18:06 +020015 private static Pattern p = Pattern
16 .compile("\\s*\\d+-(?:c(?:hars?)?|t(?:okens?)?)");
Michael Hanled5658f2014-02-07 22:24:46 +000017 private Map meta;
Michael Hanlc1c3b5d2014-08-04 16:57:46 +000018 private SpanContext spanContext;
Michael Hanled5658f2014-02-07 22:24:46 +000019
Akron3ac85c92016-06-06 16:14:40 +020020
21 public MetaQueryBuilder () {
Michael Hanl034be0d2014-02-14 10:17:34 +000022 this.meta = new LinkedHashMap();
Michael Hanlfaae0182015-06-26 16:18:06 +020023 // this.meta.put("fields", new LinkedList<>());
Michael Hanled5658f2014-02-07 22:24:46 +000024 }
25
Akron3ac85c92016-06-06 16:14:40 +020026
Michael Hanlb9f3fd12014-06-03 11:38:27 +000027 /**
28 * context segment if context is either of type char or token.
29 * size can differ for left and right span
Akron3ac85c92016-06-06 16:14:40 +020030 *
Michael Hanlb9f3fd12014-06-03 11:38:27 +000031 * @param left
32 * @param leftType
33 * @param right
34 * @param rightType
35 * @return
36 */
Akron3ac85c92016-06-06 16:14:40 +020037 public MetaQueryBuilder setSpanContext (Integer left, String leftType,
Joachim Bingel20e06ac2015-01-15 10:31:33 +000038 Integer right, String rightType) {
Michael Hanlc1c3b5d2014-08-04 16:57:46 +000039 this.spanContext = new SpanContext(left, leftType, right, rightType);
Michael Hanled5658f2014-02-07 22:24:46 +000040 return this;
41 }
42
Akron3ac85c92016-06-06 16:14:40 +020043
44 public SpanContext getSpanContext () {
Michael Hanlc1c3b5d2014-08-04 16:57:46 +000045 return this.spanContext;
46 }
47
Akron3ac85c92016-06-06 16:14:40 +020048
Michael Hanlb9f3fd12014-06-03 11:38:27 +000049 /**
Joachim Bingel20e06ac2015-01-15 10:31:33 +000050 * context if of type paragraph or sentence where left and right
51 * size delimiters are irrelevant; or 2-token, 2-char p/paragraph,
Michael Hanl93518e42015-06-26 16:18:06 +020052 * s/sentence or token, char.
53 * Distinguish
Akron3ac85c92016-06-06 16:14:40 +020054 *
Michael Hanl1b7f54a2014-07-25 17:35:55 +000055 * @param context
Michael Hanlb9f3fd12014-06-03 11:38:27 +000056 * @return
57 */
Akron3ac85c92016-06-06 16:14:40 +020058 public MetaQueryBuilder setSpanContext (String context) {
Michael Hanl93518e42015-06-26 16:18:06 +020059 if (context != null) {
Michael Hanlfaae0182015-06-26 16:18:06 +020060 if (!p.matcher(context).find())
Michael Hanl93518e42015-06-26 16:18:06 +020061 this.spanContext = new SpanContext(context);
62 else {
63 String[] ct = context.replaceAll("\\s+", "").split(",");
64 String[] lc = ct[0].split("-");
65 String[] rc = ct[1].split("-");
66 this.spanContext = new SpanContext(Integer.valueOf(lc[0]),
67 lc[1], Integer.valueOf(rc[0]), rc[1]);
68 }
Michael Hanl1b7f54a2014-07-25 17:35:55 +000069 }
Michael Hanlb9f3fd12014-06-03 11:38:27 +000070 return this;
71 }
72
Akron3ac85c92016-06-06 16:14:40 +020073
74 public MetaQueryBuilder addEntry (String name, Object value) {
Michael Hanl93518e42015-06-26 16:18:06 +020075 if (value != null)
76 meta.put(name, value);
Michael Hanled5658f2014-02-07 22:24:46 +000077 return this;
78 }
79
Akron3ac85c92016-06-06 16:14:40 +020080
81 public Map raw () {
Michael Hanldd5c9652014-09-02 18:51:08 +000082 if (this.spanContext != null)
83 meta.putAll(this.spanContext.raw());
Michael Hanl034be0d2014-02-14 10:17:34 +000084 return meta;
85 }
Michael Hanlc1c3b5d2014-08-04 16:57:46 +000086
Michael Hanlc1c3b5d2014-08-04 16:57:46 +000087 public class SpanContext {
88 private String left_type;
89 private String right_type;
90 private int left_size;
91 private int right_size;
92 private String context = null;
93
Akron3ac85c92016-06-06 16:14:40 +020094
Michael Hanlc1c3b5d2014-08-04 16:57:46 +000095 /**
96 * context segment if context is either of type char or token.
97 * size can differ for left and right span
Akron3ac85c92016-06-06 16:14:40 +020098 *
Michael Hanlc1c3b5d2014-08-04 16:57:46 +000099 * @param ls
100 * @param lt
101 * @param rs
102 * @param rt
103 * @return
104 */
Akron3ac85c92016-06-06 16:14:40 +0200105 public SpanContext (int ls, String lt, int rs, String rt) {
Michael Hanlc1c3b5d2014-08-04 16:57:46 +0000106 this.left_type = lt;
107 this.left_size = ls;
108 this.right_type = rt;
109 this.right_size = rs;
110 }
111
Akron3ac85c92016-06-06 16:14:40 +0200112
113 public SpanContext (String context) {
Michael Hanlc1c3b5d2014-08-04 16:57:46 +0000114 this.context = context;
115 }
116
Michael Hanl621180c2016-06-04 09:24:56 +0200117 public String getRightType() {
118 return this.right_type;
119 }
120
121 public String getLeftType() {
122 return this.left_type;
123 }
124
125 public Integer getLeftSize() {
126 return this.left_size;
127 }
128
129 public Integer getRightSize() {
130 return this.right_size;
131 }
132
Michael Hanlf33f7062015-06-24 21:14:26 +0200133 public Map raw() {
Michael Hanlc1c3b5d2014-08-04 16:57:46 +0000134 Map meta = new LinkedHashMap();
135 if (this.context == null) {
136 Map map = new LinkedHashMap();
137 List l = new LinkedList();
138 List r = new LinkedList();
139 l.add(this.left_type);
140 l.add(this.left_size);
141 map.put("left", l);
142 r.add(this.right_type);
143 r.add(this.right_size);
144 map.put("right", r);
145 meta.put("context", map);
Akron3ac85c92016-06-06 16:14:40 +0200146 }
147 else
Michael Hanlc1c3b5d2014-08-04 16:57:46 +0000148 meta.put("context", this.context);
149 return meta;
150 }
151 }
Michael Hanled5658f2014-02-07 22:24:46 +0000152}