Skip to content

Commit

Permalink
fixed korpling#221
Browse files Browse the repository at this point in the history
Precedence optimization fails when applied to spans which cover more than one token

Take e.g. this query on pcc2
NP & NP & NP &  #1 . #2 & #2 . #3

In ANNIS 2 this gave us 2 results, but since ANNIS 3 incorrectly applies the precedence optimization the query gets translated to

NP & NP & NP &  #1 . #2 & #2 . #3 & #1 . #3

and has only 1 match. The correct optimization would be

NP & NP & NP &  #1 . #2 & #2 . #3 & #1 .* #3

This commit adds proper test cases for this situation and gives a fix
  • Loading branch information
thomaskrause committed Sep 29, 2013
1 parent c63560c commit b06e782
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,15 @@ private void propagateNodePrecedence(QueryNode initialNode,
else
{
// calculate the new range depending on old one
if((range.getMin() == 0 && range.getMax() == 0)
if(
currentNode.isToken() == false
|| (range.getMin() == 0 && range.getMax() == 0)
|| (p.getMinDistance() == 0 && p.getMaxDistance() == 0))
{
// unlimited range, nothing to calculate
// use unlimited range since
// a) the node could also be a
// span covering more than one token,
// b) the original constraint is an unlimited range
newRange = new Range(0, 0);
}
else
Expand Down
5 changes: 5 additions & 0 deletions annis-service/src/test/java/annis/CountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ public void testAQLTestSuitePcc2()
assertEquals(3, countPcc2("Inf-Stat=\"new\" & PP & #1 _o_ #2"));
assertEquals(1, countPcc2("np_form=\"defnp\" & np_form=\"pper\" & #2 ->anaphor_antecedent #1 & cat=\"NP\" & node & #4 >[func=\"OA\"] #3 & #3 _i_ #2"));


assertEquals(5, countPcc2("cat=\"NP\" & #1:arity=3 & node & #1 > #2 & #2:arity=3"));

// test if precendence optimizatin is applied correctly when it is used with spans
// that cover more than one token
assertEquals(2, countPcc2("NP & NP & NP & #1 . #2 & #2 . #3"));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import annis.model.QueryNode;
import annis.ql.node.Start;
import annis.sqlgen.model.Join;
import annis.sqlgen.model.Precedence;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -80,7 +81,7 @@ public void testAddTransitivePrecedenceOperatorsWithBound()
System.out.println("addTransitivePrecedenceOperatorsWithBound");

// query to extend
String aql = "node & node & node & node "
String aql = "tok & tok & tok & tok "
+ "& #1 .3 #2 "
+ "& #2 .5,10 #3 "
+ "& #3 .* #4 "
Expand Down Expand Up @@ -185,7 +186,7 @@ public void testAddTransitivePrecedenceOperatorsWithoutBound()
System.out.println("addTransitivePrecedenceOperatorsWithoutBound");

// query to extend
String aql = "node & node & node & node "
String aql = "tok & tok & tok & tok "
+ "& #1 .3 #2 "
+ "& #2 .5,10 #3 "
+ "& #3 .* #4 "
Expand Down Expand Up @@ -322,7 +323,7 @@ public void testDontFollowSegmentationFromTok()
System.out.println("dontFollowSegmentationFromTok");

// query to extend
String aql = "node & node & node & #1 . #2 & #2 .abc #3";
String aql = "tok & tok & tok & #1 . #2 & #2 .abc #3";

// perform the initial parsing
Start start = parser.parse(aql);
Expand All @@ -334,4 +335,38 @@ public void testDontFollowSegmentationFromTok()

assertEquals(1, nodes.get(0).getJoins().size());
}

@Test
public void testDontUseRangedPrecendenceOnSpans()
{
System.out.println("dontUseRangedPrecendenceOnSpans");

// query to extend
String aql = "node & node & node & #1 . #2 & #2 . #3";

// perform the initial parsing
Start start = parser.parse(aql);
// optimizer is applied on the fly by the query anaylsis (as injected by Spring)
QueryData data = queryAnalysis.analyzeQuery(start, new LinkedList<Long>());

assertEquals(1, data.getAlternatives().size());
List<QueryNode> nodes = data.getAlternatives().get(0);

assertEquals(2, nodes.get(0).getJoins().size());
Join j0 = nodes.get(0).getJoins().get(0);
Join j1 = nodes.get(0).getJoins().get(1);

assertTrue(j0 instanceof Precedence);
assertTrue(j1 instanceof Precedence);

Precedence p0 = (Precedence) j0;
Precedence p1 = (Precedence) j1;

assertEquals(1, p0.getMinDistance());
assertEquals(1, p0.getMaxDistance());

assertEquals(0, p1.getMinDistance());
assertEquals(0, p1.getMaxDistance());

}
}

0 comments on commit b06e782

Please sign in to comment.