Search in sources :

Example 6 with SpanSequenceQueryWrapper

use of de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper in project Krill by KorAP.

the class TestSpanSegmentSequenceQuery method spanSegmentSequenceQuery.

@Test
public void spanSegmentSequenceQuery() throws QueryException {
    SpanSequenceQueryWrapper sssq = new SpanSequenceQueryWrapper("field");
    assertNull(sssq.toQuery());
    sssq.append("a").append("b");
    assertEquals("spanNext(field:a, field:b)", sssq.toQuery().toString());
    sssq.append("c");
    assertEquals("spanNext(spanNext(field:a, field:b), field:c)", sssq.toQuery().toString());
}
Also used : SpanSequenceQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper) Test(org.junit.Test)

Example 7 with SpanSequenceQueryWrapper

use of de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper in project Krill by KorAP.

the class TestSpanSegmentSequenceQuery method spanSegmentSequenceQuery2.

@Test
public void spanSegmentSequenceQuery2() throws QueryException {
    SpanSegmentQueryWrapper ssq = new SpanSegmentQueryWrapper("field", "-c", "-d", "-e");
    SpanSequenceQueryWrapper sssq = new SpanSequenceQueryWrapper("field", "a", "b");
    sssq.append(ssq);
    assertEquals("spanNext(spanNext(field:a, field:b), spanSegment(spanSegment(field:-c, field:-d), field:-e))", sssq.toQuery().toString());
}
Also used : SpanSegmentQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanSegmentQueryWrapper) SpanSequenceQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper) Test(org.junit.Test)

Example 8 with SpanSequenceQueryWrapper

use of de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper in project Krill by KorAP.

the class KrillQuery method _operationSequenceFromJson.

// Deserialize operation:sequence
private SpanQueryWrapper _operationSequenceFromJson(JsonNode json, JsonNode operands) throws QueryException {
    // Sequence with only one operand
    if (operands.size() == 1)
        return this._fromKoral(operands.get(0));
    SpanSequenceQueryWrapper sseqqw = this.builder().seq();
    // Say if the operand order is important
    if (json.has("inOrder"))
        sseqqw.setInOrder(json.get("inOrder").asBoolean());
    // ATTENTION: Distances have to be set before segments are added
    if (json.has("distances")) {
        // THIS IS NO LONGER NECESSARY, AS IT IS COVERED BY FRAMES
        if (json.has("exclude") && json.get("exclude").asBoolean()) {
            throw new QueryException(763, "Excluding distance constraints are currently not supported");
        }
        ;
        if (!json.get("distances").isArray()) {
            throw new QueryException(707, "Distance Constraints have to be defined as arrays");
        }
        ;
        // TEMPORARY: Workaround for group distances
        JsonNode firstDistance = json.get("distances").get(0);
        if (!firstDistance.has("@type")) {
            throw new QueryException(701, "JSON-LD group has no @type attribute");
        }
        ;
        JsonNode distances;
        if (firstDistance.get("@type").asText().equals("koral:group")) {
            if (!firstDistance.has("operands") || !firstDistance.get("operands").isArray())
                throw new QueryException(704, "Operation needs operand list");
            distances = firstDistance.get("operands");
        } else // TODO: Support cosmas distances
        if (firstDistance.get("@type").asText().equals("koral:distance") || firstDistance.get("@type").asText().equals("cosmas:distance")) {
            distances = json.get("distances");
        } else
            throw new QueryException(708, "No valid distances defined");
        // Add all distance constraint to query
        for (JsonNode constraint : distances) {
            String unit = "w";
            if (constraint.has("key"))
                unit = constraint.get("key").asText();
            // There is a maximum of 100 fix
            int min = 0, max = 100;
            if (constraint.has("boundary")) {
                Boundary b = new Boundary(constraint.get("boundary"), 0, 100);
                min = b.min;
                max = b.max;
            } else // <legacy>
            {
                if (constraint.has("min"))
                    min = constraint.get("min").asInt(0);
                if (constraint.has("max"))
                    max = constraint.get("max").asInt(100);
            }
            ;
            // Add foundry and layer to the unit for new indices
            if (constraint.has("foundry") && constraint.has("layer") && constraint.get("foundry").asText().length() > 0 && constraint.get("layer").asText().length() > 0) {
                StringBuilder value = new StringBuilder();
                value.append(constraint.get("foundry").asText());
                value.append('/');
                value.append(constraint.get("layer").asText());
                value.append(':').append(unit);
                unit = value.toString();
            } else // Use default foundry and layer - currently only base is supported!
            if (unit.equals("s") || unit.equals("p") || unit.equals("t")) {
                StringBuilder value = new StringBuilder();
                unit = value.append("base/s:").append(unit).toString();
            }
            ;
            // Workaround for koral:distance vs cosmas:distance
            if (constraint.get("@type").asText().equals("koral:distance")) {
                min++;
                max++;
            }
            ;
            // Set distance exclusion
            Boolean exclusion = false;
            if (constraint.has("exclude"))
                exclusion = constraint.get("exclude").asBoolean();
            // Sanitize boundary
            if (max < min)
                max = min;
            if (DEBUG)
                log.trace("Add distance constraint of '{}': {}-{}", unit, min, max);
            sseqqw.withConstraint(min, max, unit, exclusion);
        }
        ;
    }
    ;
    // Add segments to sequence
    for (JsonNode operand : operands) {
        sseqqw.append(this._fromKoral(operand));
    }
    ;
    // inOrder was set to false without a distance constraint
    if (!sseqqw.isInOrder() && !sseqqw.hasConstraints()) {
        if (DEBUG)
            log.trace("Add distance constraint - for the normal inorder case");
        sseqqw.withConstraint(1, 1, "w");
    }
    ;
    return sseqqw;
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) JsonNode(com.fasterxml.jackson.databind.JsonNode) SpanSequenceQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper)

Example 9 with SpanSequenceQueryWrapper

use of de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper in project Krill by KorAP.

the class TestNextIndex method indexExample8Distances.

@Test
public void indexExample8Distances() throws Exception {
    KrillIndex ki = new KrillIndex();
    ki.addDoc(createFieldDoc1());
    ki.addDoc(createFieldDoc2());
    ki.addDoc(createFieldDoc3());
    ki.addDoc(createFieldDoc4());
    ki.commit();
    SpanSequenceQueryWrapper sq = new SpanSequenceQueryWrapper("base");
    sq.append("i:a").append("i:b").withConstraint(0, 3, "e");
    Result kr = ki.search(sq.toQuery(), (short) 10);
    assertEquals("totalResults", kr.getTotalResults(), 3);
    assertEquals("doc-number", "match-doc-0-p3-6", kr.getMatch(0).getID());
    assertEquals("doc-number", "match-doc-1-p1-3", kr.getMatch(1).getID());
    assertEquals("doc-number", "match-doc-3-p3-6", kr.getMatch(2).getID());
}
Also used : KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanSequenceQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper) Result(de.ids_mannheim.korap.response.Result) Test(org.junit.Test)

Example 10 with SpanSequenceQueryWrapper

use of de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper in project Krill by KorAP.

the class TestSpanSequenceQuery method spanSequenceQuery.

@Test
public void spanSequenceQuery() throws QueryException {
    SpanSequenceQueryWrapper sssq = new SpanSequenceQueryWrapper("field");
    assertNull(sssq.toQuery());
    assertFalse(sssq.hasConstraints());
    sssq.append("a").append("b");
    assertEquals("spanNext(field:a, field:b)", sssq.toQuery().toString());
    assertFalse(sssq.hasConstraints());
    sssq.append("c");
    assertEquals("spanNext(spanNext(field:a, field:b), field:c)", sssq.toQuery().toString());
    assertFalse(sssq.hasConstraints());
    sssq = new SpanSequenceQueryWrapper("field");
    sssq.append("a");
    assertEquals("field:a", sssq.toQuery().toString());
    assertFalse(sssq.hasConstraints());
    sssq.append("b");
    assertEquals("spanNext(field:a, field:b)", sssq.toQuery().toString());
    assertFalse(sssq.hasConstraints());
    sssq.withConstraint(2, 3);
    assertTrue(sssq.hasConstraints());
    assertEquals("spanDistance(field:a, field:b, [(w[2:3], ordered, notExcluded)])", sssq.toQuery().toString());
    sssq.append("c");
    assertEquals("spanDistance(spanDistance(field:a, field:b, [(w[2:3], ordered, notExcluded)]), field:c, [(w[2:3], ordered, notExcluded)])", sssq.toQuery().toString());
    sssq.withConstraint(6, 8, "s");
    assertTrue(sssq.hasConstraints());
    assertEquals("spanMultipleDistance(spanMultipleDistance(field:a, field:b, [(w[2:3], ordered, notExcluded), (s[6:8], ordered, notExcluded)]), field:c, [(w[2:3], ordered, notExcluded), (s[6:8], ordered, notExcluded)])", sssq.toQuery().toString());
}
Also used : SpanSequenceQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper) Test(org.junit.Test)

Aggregations

SpanSequenceQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper)14 Test (org.junit.Test)13 KrillIndex (de.ids_mannheim.korap.KrillIndex)3 SpanSegmentQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanSegmentQueryWrapper)3 SpanWithinQuery (de.ids_mannheim.korap.query.SpanWithinQuery)2 SpanRegexQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanRegexQueryWrapper)2 Result (de.ids_mannheim.korap.response.Result)2 QueryException (de.ids_mannheim.korap.util.QueryException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 SpanElementQuery (de.ids_mannheim.korap.query.SpanElementQuery)1 SpanClassQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanClassQueryWrapper)1 SpanRepetitionQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanRepetitionQueryWrapper)1