Fixed handling of non-necessary brackets in tokens

Change-Id: Ibb2550d308c49176f62b45a01e008bd851e84605
diff --git a/Changes b/Changes
index 19d0443..f74c7a7 100644
--- a/Changes
+++ b/Changes
@@ -1,7 +1,8 @@
-0.26 2017-05-30
+0.26 2017-06-28
 	- Updated collection query grammar (margaretha)
 	- Updated maven dependency phase (margaretha)
-	
+  - Fixed handling of non-necessary brackets in tokens (diewald)
+
 0.25 2017-05-10
 	- Altered dominance and relation serialization (margaretha)
 	 
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusQueryProcessor.java b/src/main/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusQueryProcessor.java
index 792ba7d..3a2a594 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusQueryProcessor.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusQueryProcessor.java
@@ -867,7 +867,7 @@
         else if (nodeCat.equals("termGroup")) {
 
             // TermGroup is defined recursive with non-necessary brackets
-            if (getNodeCat(node.getChild(0)).equals("(")) {
+            if (getNodeCat(node.getChild(0)).equals("(") && node.getChildCount() == 3) {
                 return parseTermOrTermGroup(node.getChild(1), negatedGlobal,
                         mode);
             };
@@ -878,6 +878,8 @@
             Map<String, Object> termGroup = null;
             ParseTree leftOp = null;
             ParseTree rightOp = null;
+
+
             // check for leading/trailing parantheses
             if (!getNodeCat(node.getChild(0)).equals("("))
                 leftOp = node.getChild(0);
@@ -890,6 +892,8 @@
                 rightOp = node.getChild(node.getChildCount() - 2);
             // establish boolean relation
             ParseTree boolOp = getFirstChildWithCat(node, "boolOp");
+
+			// Create group
             if (boolOp.getText().equals("&")) {
                 termGroup = KoralObjectGenerator
                         .makeTermGroup(KoralTermGroupRelation.AND);
diff --git a/src/test/java/de/ids_mannheim/korap/query/test/poliqarpplus/PoliqarpPlusQueryProcessorTest.java b/src/test/java/de/ids_mannheim/korap/query/test/poliqarpplus/PoliqarpPlusQueryProcessorTest.java
index 6205444..0af2c18 100644
--- a/src/test/java/de/ids_mannheim/korap/query/test/poliqarpplus/PoliqarpPlusQueryProcessorTest.java
+++ b/src/test/java/de/ids_mannheim/korap/query/test/poliqarpplus/PoliqarpPlusQueryProcessorTest.java
@@ -634,8 +634,29 @@
                 .asText());
         assertEquals("gen", res.at("/query/wrap/operands/1/operands/1/layer")
                 .asText());
-    }
 
+		query = "[(cas=N|cas=A)&base=Mann]";
+        qs.setQuery(query, "poliqarpplus");
+        res = mapper.readTree(qs.toJSON());		
+        assertEquals("koral:token", res.at("/query/@type").asText());
+        assertEquals("relation:and", res.at("/query/wrap/relation").asText());
+        assertEquals("koral:termGroup", res.at("/query/wrap/operands/0/@type")
+                .asText());
+        assertEquals("relation:or", res.at("/query/wrap/operands/0/relation")
+                .asText());
+        assertEquals("N", res.at("/query/wrap/operands/0/operands/0/key")
+                .asText());
+        assertEquals("cas", res.at("/query/wrap/operands/0/operands/0/layer")
+                .asText());
+        assertEquals("A", res.at("/query/wrap/operands/0/operands/1/key")
+                .asText());
+        assertEquals("cas", res.at("/query/wrap/operands/0/operands/1/layer")
+                .asText());
+		assertEquals("Mann", res.at("/query/wrap/operands/1/key").asText());
+        assertEquals("lemma", res.at("/query/wrap/operands/1/layer").asText());
+	}
+
+	
     @Test
     public void testUnnecessaryParentheses () throws JsonProcessingException,
             IOException {
@@ -708,6 +729,25 @@
         assertEquals("cas", res.at("/query/wrap/operands/1/layer")
                      .asText());
 
+		query = "[((cas=N|cas=A))&base=Mann]";
+        qs.setQuery(query, "poliqarpplus");
+        res = mapper.readTree(qs.toJSON());		
+        assertEquals("koral:token", res.at("/query/@type").asText());
+        assertEquals("relation:and", res.at("/query/wrap/relation").asText());
+        assertEquals("koral:termGroup", res.at("/query/wrap/operands/0/@type")
+                .asText());
+        assertEquals("relation:or", res.at("/query/wrap/operands/0/relation")
+                .asText());
+        assertEquals("N", res.at("/query/wrap/operands/0/operands/0/key")
+                .asText());
+        assertEquals("cas", res.at("/query/wrap/operands/0/operands/0/layer")
+                .asText());
+        assertEquals("A", res.at("/query/wrap/operands/0/operands/1/key")
+                .asText());
+        assertEquals("cas", res.at("/query/wrap/operands/0/operands/1/layer")
+                .asText());
+		assertEquals("Mann", res.at("/query/wrap/operands/1/key").asText());
+        assertEquals("lemma", res.at("/query/wrap/operands/1/layer").asText());
     };
 
     @Test