use of de.ids_mannheim.korap.query.wrap.SpanQueryWrapper in project Krill by KorAP.
the class TestClass method queryJSONpoly1.
@Test
public void queryJSONpoly1() throws QueryException, IOException {
String jsonPath = URLDecoder.decode(getClass().getResource("/queries/poly1.json").getFile(), "UTF-8");
String jsonQuery = readFile(jsonPath);
SpanQueryWrapper sqwi = new KrillQuery("tokens").fromKoral(jsonQuery);
SpanNextQuery sq = (SpanNextQuery) sqwi.toQuery();
// System.out.println(sq.toString());
ki = new KrillIndex();
ki.addDoc(getClass().getResourceAsStream("/wiki/JJJ-00785.json.gz"), true);
ki.addDoc(getClass().getResourceAsStream("/wiki/DDD-01402.json.gz"), true);
ki.commit();
kr = ki.search(sq, (short) 10);
assertEquals(61, kr.getMatch(0).getStartPos());
assertEquals(64, kr.getMatch(0).getEndPos());
assertEquals("... Bruckner (Wien) und Mathis Lussy (Paris). [[{1:Inspiriert} " + "{2:durch die}]] additiven Modelle arabischer Rhythmik (er half ...", kr.getMatch(0).getSnippetBrackets());
assertEquals(31, kr.getMatch(1).getStartPos());
assertEquals(34, kr.getMatch(1).getEndPos());
assertEquals("... des Sendens wird ein unhörbarer Unterton [[{1:mitgesendet}, " + "{2:auf den}]] das angesprochene Funkgerät reagiert. Die Abkürzung ...", kr.getMatch(1).getSnippetBrackets());
}
use of de.ids_mannheim.korap.query.wrap.SpanQueryWrapper in project Krill by KorAP.
the class TestClass method queryJSONpoly4.
@Test
public void queryJSONpoly4() throws QueryException, IOException {
String jsonPath = URLDecoder.decode(getClass().getResource("/queries/poly4.json").getFile(), "UTF-8");
String jsonQuery = readFile(jsonPath);
SpanQueryWrapper sqwi = new KrillQuery("tokens").fromKoral(jsonQuery);
SpanQuery sq = sqwi.toQuery();
// System.out.println(sq.toString());
ki = new KrillIndex();
ki.addDoc(getClass().getResourceAsStream("/wiki/SSS-09803.json.gz"), true);
ki.commit();
kr = ki.search(sq, (short) 10);
/*
for (Match km : kr.getMatches()){
System.out.println(km.getStartPos() +","+km.getEndPos()+" "
+km.getSnippetBrackets()
);
}
*/
assertEquals((long) 5315, kr.getTotalResults());
assertEquals(3, kr.getMatch(0).getStartPos());
assertEquals(5, kr.getMatch(0).getEndPos());
// fail("Tests have to be updated");
}
use of de.ids_mannheim.korap.query.wrap.SpanQueryWrapper in project Krill by KorAP.
the class SpanAlterQueryWrapper method toFragmentQuery.
@Override
public SpanQuery toFragmentQuery() throws QueryException {
if (this.isNull || this.alternatives.size() == 0)
return (SpanQuery) null;
if (this.alternatives.size() == 1) {
return (SpanQuery) this.alternatives.get(0).retrieveNode(this.retrieveNode).toFragmentQuery();
}
;
Iterator<SpanQueryWrapper> clause = this.alternatives.iterator();
SpanOrQuery soquery = new SpanOrQuery(clause.next().retrieveNode(this.retrieveNode).toFragmentQuery());
while (clause.hasNext()) {
soquery.addClause(clause.next().retrieveNode(this.retrieveNode).toFragmentQuery());
}
;
return (SpanQuery) soquery;
}
use of de.ids_mannheim.korap.query.wrap.SpanQueryWrapper in project Krill by KorAP.
the class Krill method fromKoral.
/**
* Parse KoralQuery as a {@link JsonNode} object.
*
* @param query
* The KoralQuery {@link JsonNode} object.
* @return The {@link Krill} object for chaining.
* @throws QueryException
*/
public Krill fromKoral(JsonNode json) {
// Parse "query" attribute
if (json.has("query")) {
try {
final KrillQuery kq = new KrillQuery("tokens");
this.setQuery(kq);
final SpanQueryWrapper qw = kq.fromKoral(json.get("query"));
// Koral messages are moved to the Krill object
this.moveNotificationsFrom(kq);
// Throw an error, in case the query matches everywhere
if (qw.isEmpty()) {
this.addError(780, "This query matches everywhere");
} else if (qw.isNull()) {
this.addError(783, "This query can't match anywhere");
} else {
// Serialize a Lucene SpanQuery based on the SpanQueryWrapper
this.spanQuery = qw.toQuery();
// Throw a warning in case the root object is optional
if (qw.isOptional())
this.addWarning(781, "Optionality of query is ignored");
// Throw a warning in case the root object is negative
if (qw.isNegative())
this.addWarning(782, "Exclusivity of query is ignored");
}
;
} catch (QueryException q) {
this.addError(q.getErrorCode(), q.getMessage());
}
;
} else
this.addError(700, "No query given");
// <legacycode>
if (json.has("warning") && json.get("warning").asText().length() > 0) {
this.addWarning(799, json.get("warning").asText());
}
;
// </legacycode>
// Copy notifications from request
this.copyNotificationsFrom(json);
// Parse "collection" or "collections" attribute
try {
if (json.has("collection")) {
final JsonNode collNode = json.get("collection");
// TODO: Temporary
if (collNode.fieldNames().hasNext()) {
this.setCollection(new KrillCollection().fromKoral(collNode));
}
;
} else if (json.has("collections")) {
this.addError(899, "Collections are not supported anymore in favour of a single collection");
}
;
} catch (QueryException q) {
this.addError(q.getErrorCode(), q.getMessage());
}
;
// !this.hasErrors() &&
if (json.has("meta"))
this.setMeta(new KrillMeta(json.get("meta")));
return this;
}
use of de.ids_mannheim.korap.query.wrap.SpanQueryWrapper in project Krill by KorAP.
the class KrillQuery method _fromKoral.
private SpanQueryWrapper _fromKoral(JsonNode json, boolean isOperationRelation) throws QueryException {
int number = 0;
// TODO: Support @context for cosmas:...
if (!json.has("@type"))
throw new QueryException(701, "JSON-LD group has no @type attribute");
// Get @type for branching
String type = json.get("@type").asText();
switch(type) {
case "koral:group":
return this._groupFromJson(json);
case "koral:reference":
if (json.has("operation") && !json.get("operation").asText().equals("operation:focus"))
throw new QueryException(712, "Unknown reference operation");
if (!json.has("operands")) {
throw new QueryException(766, "Peripheral references are currently not supported");
}
JsonNode operands = json.get("operands");
if (!operands.isArray())
throw new QueryException(704, "Operation needs operand list");
if (operands.size() == 0)
throw new QueryException(704, "Operation needs operand list");
if (operands.size() != 1)
throw new QueryException(705, "Number of operands is not acceptable");
// Reference based on classes
if (json.has("classRef")) {
if (json.has("classRefOp")) {
throw new QueryException(761, "Class reference operators are currently not supported");
}
;
number = json.get("classRef").get(0).asInt();
if (number > MAX_CLASS_NUM)
throw new QueryException(709, "Valid class numbers exceeded");
} else // Reference based on spans
if (json.has("spanRef")) {
JsonNode spanRef = json.get("spanRef");
int length = 0;
int startOffset = 0;
if (!spanRef.isArray() || spanRef.size() == 0) {
throw new QueryException(714, "Span references expect a start position" + " and a length parameter");
}
;
if (spanRef.size() > 1)
length = spanRef.get(1).asInt(0);
startOffset = spanRef.get(0).asInt(0);
if (DEBUG)
log.trace("Wrap span reference {},{}", startOffset, length);
SpanQueryWrapper sqw = this._fromKoral(operands.get(0));
SpanSubspanQueryWrapper ssqw = new SpanSubspanQueryWrapper(sqw, startOffset, length);
return ssqw;
}
;
if (DEBUG)
log.trace("Wrap class reference {}", number);
return new SpanFocusQueryWrapper(this._fromKoral(operands.get(0)), number);
case "koral:token":
// The token is empty and should be treated like []
if (!json.has("wrap"))
return new SpanRepetitionQueryWrapper();
// Get wrapped token
return this._segFromJson(json.get("wrap"));
case "koral:span":
// it is allowed only in relation queries
if (isOperationRelation && !json.has("key") && !json.has("wrap") && !json.has("attr")) {
return new SpanRepetitionQueryWrapper();
}
if (!json.has("wrap"))
return this._termFromJson(json);
// This is an ugly hack
return this._termFromJson(json.get("wrap"), true);
}
;
// Unknown query type
throw new QueryException(713, "Query type is not supported");
}
Aggregations