added '@' for context, rewriting query X:sa as #BEG(X , sa), updated examples
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/CosmasTree.java b/src/main/java/de/ids_mannheim/korap/query/serialize/CosmasTree.java
index f97cc77..777d06a 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/CosmasTree.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/CosmasTree.java
@@ -6,6 +6,8 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.RecognitionException;
@@ -85,7 +87,7 @@
/**
* A list of node categories that can be sequenced (i.e. which can be in a sequence with any number of other nodes in this list)
*/
- private final List<String> sequentiableCats = Arrays.asList(new String[] {"OPWF", "OPLEM", "OPMORPH", "OPBEG", "OPEND", "OPIN"});
+ private final List<String> sequentiableCats = Arrays.asList(new String[] {"OPWF", "OPLEM", "OPMORPH", "OPBEG", "OPEND", "OPIN", "OPBED"});
/**
* Keeps track of sequenced nodes, i.e. nodes that implicitly govern a sequence, as in (C2PQ (OPWF der) (OPWF Mann)).
* This is necessary in order to know when to take the sequence off the object stack, as the sequence is introduced by the
@@ -133,7 +135,7 @@
}
System.out.println("Processing Cosmas");
- requestMap.put("context", "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld");
+ requestMap.put("@context", "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld");
// QueryUtils.prepareContext(requestMap);
processNode(tree);
}
@@ -164,6 +166,7 @@
// Check for potential implicit sequences as in (C2PQ (OPWF der) (OPWF Mann)). The sequence is introduced
// by the first child if it (and its siblings) is sequentiable.
if (sequentiableCats.contains(nodeCat)) {
+ System.err.println(nodeCat);
// for each node, check if parent has more than one child (-> could be implicit sequence)
Tree parent = node.getParent();
if (parent.getChildCount()>1) {
@@ -173,13 +176,14 @@
for (int i=1; i<parent.getChildCount() ;i++) {
if (sequentiableCats.contains(QueryUtils.getNodeCat(parent.getChild(i)))) {
hasSequentiableSiblings = true;
+ continue;
}
}
if (hasSequentiableSiblings) {
// Step I: create sequence
LinkedHashMap<String, Object> sequence = new LinkedHashMap<String, Object>();
sequence.put("@type", "korap:group");
- sequence.put("operation", "operation:"+ "sequence");
+ sequence.put("operation", "operation:sequence");
sequence.put("operands", new ArrayList<Object>());
// push sequence on object stack but don't increment stackedObjects counter since
// we've got to wait until the parent node is processed - therefore, add the parent
@@ -223,7 +227,12 @@
fieldMap.put("match", "match:eq");
}
//Step II: decide where to put
- putIntoSuperObject(token, 1);
+ if (! QueryUtils.hasChild(node, "TPOS")) {
+ putIntoSuperObject(token, 1);
+ } else {
+
+ }
+
}
if (nodeCat.equals("OPMORPH")) {
@@ -505,7 +514,7 @@
submatchgroup.put("classRef", spanRef);
ArrayList<Object> submatchoperands = new ArrayList<Object>();
submatchgroup.put("operands", submatchoperands);
- putIntoSuperObject(submatchgroup, 1);
+ putIntoSuperObject(submatchgroup, 0);
// Distinguish two cases. Normal case: query has just one condition, like #BED(X, sa) ...
if (conditions.getChildCount()==1) {
@@ -767,9 +776,27 @@
}
- private static Tree parseCosmasQuery(String p) throws RecognitionException {
+ private static Tree parseCosmasQuery(String q) throws RecognitionException {
+ Pattern p = Pattern.compile("(\\w+):((\\+|-)?(sa|se|pa|pe|ta|te),?)+");
+ Matcher m = p.matcher(q);
+
+ String rewrittenQuery = q;
+ while (m.find()) {
+ String match = m.group();
+ String conditionsString = match.split(":")[1];
+ Pattern conditionPattern = Pattern.compile("(\\+|-)?(sa|se|pa|pe|ta|te)");
+ Matcher conditionMatcher = conditionPattern.matcher(conditionsString);
+ String replacement = "#BED("+m.group(1)+" , ";
+ while (conditionMatcher.find()) {
+ replacement = replacement+conditionMatcher.group()+",";
+ }
+ replacement = replacement.substring(0, replacement.length()-1)+")"; //remove trailing comma and close parenthesis
+ System.out.println(replacement);
+ rewrittenQuery = rewrittenQuery.replace(match, replacement);
+ }
+ q = rewrittenQuery;
Tree tree = null;
- ANTLRStringStream ss = new ANTLRStringStream(p);
+ ANTLRStringStream ss = new ANTLRStringStream(q);
c2psLexer lex = new c2psLexer(ss);
org.antlr.runtime.CommonTokenStream tokens = new org.antlr.runtime.CommonTokenStream(lex); //v3
cosmasParser = new c2psParser(tokens);
@@ -800,9 +827,11 @@
// "(Mann oder Frau) #IN <s>",
// "#BEG(der /w3:5 Mann) /+w10 kommt",
// "&würde /w0 MORPH(V)",
- "#NHIT(gehen /w1:10 voran)",
- "#BED(der Mann , sa,-pa)",
- "Mann /t0 Frau"
+// "#NHIT(gehen /w1:10 voran)",
+// "#BED(der Mann , sa,-pa)",
+// "Mann /t0 Frau",
+ "sagt der:sa Bundeskanzler",
+// "Der:sa,-pe,+te ",
};
// CosmasTree.debug=true;
for (String q : queries) {
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusTree.java b/src/main/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusTree.java
index ec62578..161aa18 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusTree.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusTree.java
@@ -179,7 +179,7 @@
}
}
System.out.println("Processing PoliqarpPlus");
- requestMap.put("context", "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld");
+ requestMap.put("@context", "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld");
// QueryUtils.prepareContext(requestMap);
processNode(tree);
}
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/QueryUtils.java b/src/main/java/de/ids_mannheim/korap/query/serialize/QueryUtils.java
index 4495c01..e0f0c0d 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/QueryUtils.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/QueryUtils.java
@@ -58,6 +58,22 @@
* @param childCat The category of the potential child.
* @return true iff one or more children belong to the specified category
*/
+ public static boolean hasChild(Tree node, String childCat) {
+ for (int i = 0; i < node.getChildCount(); i++) {
+ if (getNodeCat(node.getChild(i)).equals(childCat)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Tests whether a certain node has a child by a certain name
+ *
+ * @param node The parent node.
+ * @param childCat The category of the potential child.
+ * @return true iff one or more children belong to the specified category
+ */
public static boolean hasChild(ParseTree node, String childCat) {
for (int i = 0; i < node.getChildCount(); i++) {
if (getNodeCat(node.getChild(i)).equals(childCat)) {
@@ -335,4 +351,5 @@
}
+
}
diff --git a/src/test/java/CosmasTreeTest.java b/src/test/java/CosmasTreeTest.java
index 1cfb8bb..b084bb4 100644
--- a/src/test/java/CosmasTreeTest.java
+++ b/src/test/java/CosmasTreeTest.java
@@ -24,24 +24,9 @@
return res.equals(queryMap);
}
-// @Test
+ @Test
public void testContext() throws QueryException {
- String contextString = "{korap = http://korap.ids-mannheim.de/ns/KorAP/json-ld/v0.1/, " +
- "boundary = korap:boundary/,"+
- "group = korap:group/,"+
- "operation = {@id = group:operation/, @type = @id},"+
- "class = {@id = group:class, @type = xsd:integer},"+
- "operands = {@id = group:operands, @container = @list},"+
- "frame = {@id = group:frame/, @type = @id},"+
- "classRef = {@id = group:classRef, @type = xsd:integer},"+
- "spanRef = {@id = group:spanRef, @type = xsd:integer},"+
- "classRefOp = {@id = group:classRefOp, @type = @id},"+
- "min = {@id = boundary:min, @type = xsd:integer},"+
- "max = {@id = boundary:max, @type = xsd:integer},"+
- "exclude = {@id = group:exclude, @type = xsd:boolean},"+
- "distances = {@id = group:distances, @container = @list},"+
- "inOrder = {@id = group:inOrder, @type = xsd:boolean}"+
- "}";
+ String contextString = "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld";
ct = new CosmasTree("Test");
assertEquals(contextString.replaceAll(" ", ""), ct.getRequestMap().get("@context").toString().replaceAll(" ", ""));
}
@@ -599,5 +584,52 @@
assertEquals(bed3.replaceAll(" ", ""), map.replaceAll(" ", ""));
}
+ @Test
+ public void testColonSeparatedConditions() throws QueryException {
+
+ query = "Der:sa";
+ String col1 =
+ "{@type=korap:group, operation=operation:submatch, classRef=[1], operands=[" +
+ "{@type=korap:group, operation=operation:position, frame=frame:startswith, operands=[" +
+ "{@type=korap:span, key=s}," +
+ "{@type=korap:group, operation=operation:class, class=1, operands=[" +
+ "{@type=korap:token, wrap={@type=korap:term, key=Der, layer=orth, match=match:eq}}" +
+ "]}" +
+ "]}" +
+ "]}";
+ ct = new CosmasTree(query);
+ map = ct.getRequestMap().get("query").toString();
+ assertEquals(col1.replaceAll(" ", ""), map.replaceAll(" ", ""));
+
+ query = "Mann:sa,-pa,+te)";
+ String col2 =
+ "{@type=korap:group, operation=operation:submatch, classRef=[1], operands=[" +
+ "{@type=korap:group, operation=operation:sequence, distances=[" +
+ "{@type=korap:distance, key=w, min=0, max=0}" +
+ "], operands=[" +
+ "{@type=korap:group, operation=operation:position, frame=frame:startswith, operands=[" +
+ "{@type=korap:span, key=s}," +
+ "{@type=korap:group, operation=operation:class, class=1, operands=[" +
+ "{@type=korap:token, wrap={@type=korap:term, key=Mann, layer=orth, match=match:eq}}" +
+ "]}" +
+ "]}," +
+ "{@type=korap:group, operation=operation:position, frame=frame:startswith, exclude=true, operands=[" +
+ "{@type=korap:span, key=p}," +
+ "{@type=korap:group, operation=operation:class, class=1, operands=[" +
+ "{@type=korap:token, wrap={@type=korap:term, key=Mann, layer=orth, match=match:eq}}" +
+ "]}" +
+ "]}," +
+ "{@type=korap:group, operation=operation:position, frame=frame:endswith, operands=[" +
+ "{@type=korap:span, key=t}," +
+ "{@type=korap:group, operation=operation:class, class=1, operands=[" +
+ "{@type=korap:token, wrap={@type=korap:term, key=Mann, layer=orth, match=match:eq}}" +
+ "]}" +
+ "]}" +
+ "]}" +
+ "]}";
+ ct = new CosmasTree(query);
+ map = ct.getRequestMap().get("query").toString();
+ assertEquals(col2.replaceAll(" ", ""), map.replaceAll(" ", ""));
+ }
}
diff --git a/src/test/java/PoliqarpPlusTreeTest.java b/src/test/java/PoliqarpPlusTreeTest.java
index b7c48be..ad293eb 100644
--- a/src/test/java/PoliqarpPlusTreeTest.java
+++ b/src/test/java/PoliqarpPlusTreeTest.java
@@ -24,24 +24,9 @@
return res.equals(queryMap);
}
-// @Test
+ @Test
public void testContext() throws QueryException {
- String contextString = "{korap = http://korap.ids-mannheim.de/ns/KorAP/json-ld/v0.1/, " +
- "boundary = korap:boundary/,"+
- "group = korap:group/,"+
- "operation = {@id = group:operation/, @type = @id},"+
- "class = {@id = group:class, @type = xsd:integer},"+
- "operands = {@id = group:operands, @container = @list},"+
- "frame = {@id = group:frame/, @type = @id},"+
- "classRef = {@id = group:classRef, @type = xsd:integer},"+
- "spanRef = {@id = group:spanRef, @type = xsd:integer},"+
- "classRefOp = {@id = group:classRefOp, @type = @id},"+
- "min = {@id = boundary:min, @type = xsd:integer},"+
- "max = {@id = boundary:max, @type = xsd:integer},"+
- "exclude = {@id = group:exclude, @type = xsd:boolean},"+
- "distances = {@id = group:distances, @container = @list},"+
- "inOrder = {@id = group:inOrder, @type = xsd:boolean}"+
- "}";
+ String contextString = "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld";
ppt = new PoliqarpPlusTree("Test");
assertEquals(contextString.replaceAll(" ", ""), ppt.getRequestMap().get("@context").toString().replaceAll(" ", ""));
}