arbitrary id writer for korap:token element for collocation analysis function
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/IdWriter.java b/src/main/java/de/ids_mannheim/korap/query/serialize/IdWriter.java
new file mode 100644
index 0000000..7249df7
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/IdWriter.java
@@ -0,0 +1,73 @@
+package de.ids_mannheim.korap.query.serialize;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import de.ids_mannheim.korap.utils.JsonUtils;
+
+import java.util.Iterator;
+
+/**
+ * @author hanl
+ * @date 04/06/2014
+ * <p/>
+ * create idn for korap:token
+ */
+public class IdWriter {
+
+ private JsonNode node;
+ private int counter;
+
+ public IdWriter(String json) {
+ node = JsonUtils.readTree(json);
+ counter = 0;
+ }
+
+ public IdWriter process() {
+ process(node.path("query"));
+ return this;
+ }
+
+ private void process(JsonNode node) {
+ if (node.path("@type").asText().equals("korap:group")) {
+ Iterator<JsonNode> operands = node.path("operands").elements();
+ while (operands.hasNext())
+ process(operands.next());
+ } else if (node.path("@type").asText().equals("korap:token"))
+ addId(node);
+ }
+
+ private JsonNode addId(JsonNode node) {
+ if (node.isObject()) {
+ ObjectNode o = (ObjectNode) node;
+ String s = extractToken(node);
+ if (s != null && !s.isEmpty())
+ o.put("idn", s + "_" + counter++);
+ }
+ return node;
+ }
+
+ private String extractToken(JsonNode token) {
+ if (!token.path("@type").equals("korap:term")) {
+ JsonNode wrap = token.path("wrap");
+ JsonNode op = token.path("operands");
+ if (!wrap.isMissingNode())
+ return extractToken(wrap);
+ if (!op.isMissingNode()) {
+ Iterator<JsonNode> operands = op.elements();
+ while (operands.hasNext())
+ return extractToken(operands.next());
+ }
+ }
+ return token.path("key").asText();
+
+ }
+
+ public JsonNode getFinalNode() {
+ return this.node;
+ }
+
+ public String toJSON() {
+ return JsonUtils.toJSON(node);
+ }
+
+}
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java b/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
index cde7b8b..a4f0a0b 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
@@ -124,16 +124,16 @@
public QuerySerializer setQuery(String query, String ql, String version)
throws QueryException {
try {
- if (ql.toLowerCase().equals("poliqarp")) {
+ if (ql.equalsIgnoreCase("poliqarp")) {
ast = new PoliqarpPlusTree(query);
- } else if (ql.toLowerCase().equals("cosmas2")) {
+ } else if (ql.equalsIgnoreCase("cosmas2")) {
ast = new CosmasTree(query);
- } else if (ql.toLowerCase().equals("poliqarpplus")) {
+ } else if (ql.equalsIgnoreCase("poliqarpplus")) {
ast = new PoliqarpPlusTree(query);
- } else if (ql.toLowerCase().equals("cql")) {
+ } else if (ql.equalsIgnoreCase("cql")) {
// queryLanguageVersion = "1.2"; // set me
ast = new CQLTree(query, version);
- } else if (ql.toLowerCase().equals("annis")) {
+ } else if (ql.equalsIgnoreCase("annis")) {
ast = new AqlTree(query);
} else {
throw new QueryException(ql + " is not a supported query language!");
@@ -150,10 +150,13 @@
return setQuery(query, ql, "");
}
- public final String build() {
+ public final String toJSON() {
return JsonUtils.toJSON(raw());
}
+ public final Map build() {
+ return raw();
+ }
private Map raw() {
Map<String, Object> requestMap = ast.getRequestMap();