use of de.ids_mannheim.korap.query.wrap.SpanRepetitionQueryWrapper in project Krill by KorAP.
the class KrillQuery method _termFromJson.
// Deserialize koral:term
// TODO: Not optimal as it does not respect non-term
private SpanQueryWrapper _termFromJson(JsonNode json, boolean isSpan, RelationDirection direction) throws QueryException {
if (!json.has("@type")) {
throw new QueryException(701, "JSON-LD group has no @type attribute");
}
;
String termType = json.get("@type").asText();
Boolean isTerm = termType.equals("koral:term") ? true : false;
Boolean isCaseInsensitive = false;
if (!json.has("key") || json.get("key").asText().length() < 1) {
// why must it have an attr?
if (!json.has("attr")) {
// return new SpanRepetitionQueryWrapper();
throw new QueryException(740, "Key definition is missing in term or span");
}
}
;
// Empty koral:span hack
if (isSpan) {
isTerm = false;
}
;
// <legacy>
if (json.has("caseInsensitive") && json.get("caseInsensitive").asBoolean()) {
isCaseInsensitive = true;
} else // Flags
if (json.has("flags") && json.get("flags").isArray()) {
Iterator<JsonNode> flags = json.get("flags").elements();
while (flags.hasNext()) {
String flag = flags.next().asText();
if (flag.equals("flags:caseInsensitive")) {
isCaseInsensitive = true;
} else {
this.addWarning(748, "Flag is unknown", flag);
}
;
}
;
}
;
StringBuilder value = new StringBuilder();
if (direction != null)
value.append(direction.value());
if (json.has("foundry") && json.get("foundry").asText().length() > 0) {
value.append(json.get("foundry").asText()).append('/');
}
;
// No default foundry defined
if (json.has("layer") && json.get("layer").asText().length() > 0) {
String layer = json.get("layer").asText();
switch(layer) {
case "lemma":
layer = "l";
break;
case "pos":
layer = "p";
break;
case "orth":
// TODO: THIS IS AN UGLY HACK! AND SHOULD BE NAMED "SURFACE" or . OR *
layer = ".";
break;
case "struct":
layer = "s";
break;
case "const":
layer = "c";
break;
}
;
if (isCaseInsensitive && isTerm) {
if (layer.equals("."))
layer = "i";
else {
this.addWarning(767, "Case insensitivity is currently not supported for this layer");
}
;
}
;
// Ignore foundry for orth layer
if (layer.equals(".")) {
layer = "s";
value.setLength(0);
} else if (layer.equals("i")) {
value.setLength(0);
}
;
value.append(layer).append(':');
}
;
if (json.has("key") && json.get("key").asText().length() > 0) {
String key = json.get("key").asText();
value.append(isCaseInsensitive ? key.toLowerCase() : key);
}
;
if (json.has("value") && json.get("value").asText().length() > 0)
value.append(':').append(json.get("value").asText());
// Regular expression or wildcard
if (isTerm) {
String match = "match:eq";
if (json.has("match")) {
match = json.get("match").asText();
}
;
if (json.has("type")) {
QueryBuilder qb = this.builder();
// Branch on type
switch(json.get("type").asText()) {
case "type:regex":
{
// The regex can be rewritten to an any token
if (value.toString().matches("^[si]:\\.[\\+\\*]\\??$")) {
return new SpanRepetitionQueryWrapper();
}
;
SpanRegexQueryWrapper srqw = qb.re(value.toString(), isCaseInsensitive);
if (match.equals("match:ne")) {
if (DEBUG)
log.trace("Term is negated");
// ssqw.makeNegative();
return this.builder().seg().without(srqw);
} else if (match.equals("match:eq")) {
return srqw;
}
throw new QueryException(741, "Match relation unknown");
}
case "type:wildcard":
{
SpanWildcardQueryWrapper swcqw = qb.wc(value.toString(), isCaseInsensitive);
if (match.equals("match:ne")) {
if (DEBUG)
log.trace("Term is negated");
// ssqw.makeNegative();
return this.builder().seg().without(swcqw);
} else if (match.equals("match:eq")) {
return swcqw;
}
;
throw new QueryException(741, "Match relation unknown");
}
case "type:string":
break;
default:
this.addWarning(746, "Term type is not supported - treated as a string");
}
;
}
;
SpanSegmentQueryWrapper ssqw = this.builder().seg(value.toString());
if (match.equals("match:ne")) {
if (DEBUG)
log.trace("Term is negated");
ssqw.makeNegative();
return this.builder().seg().without(ssqw);
} else if (match.equals("match:eq")) {
return ssqw;
} else {
throw new QueryException(741, "Match relation unknown");
}
}
;
if (json.has("attr")) {
JsonNode attrNode = json.get("attr");
if (!attrNode.has("@type")) {
throw new QueryException(701, "JSON-LD group has no @type attribute");
}
if (value.toString().isEmpty()) {
return _createElementAttrFromJson(null, json, attrNode);
// this.addWarning(771,
// "Arbitraty elements with attributes are currently not supported.");
} else {
SpanQueryWrapper elementWithIdWrapper = this.builder().tag(value.toString());
if (elementWithIdWrapper == null) {
return null;
}
return _createElementAttrFromJson(elementWithIdWrapper, json, attrNode);
}
}
;
return this.builder().tag(value.toString());
}
use of de.ids_mannheim.korap.query.wrap.SpanRepetitionQueryWrapper in project Krill by KorAP.
the class KrillQuery method _operationRepetitionFromJson.
// Deserialize operation:repetition
private SpanQueryWrapper _operationRepetitionFromJson(JsonNode json, JsonNode operands) throws QueryException {
if (operands.size() != 1)
throw new QueryException(705, "Number of operands is not acceptable");
int min = 0, max = 100;
if (json.has("boundary")) {
Boundary b = new Boundary(json.get("boundary"), 0, 100);
min = b.min;
max = b.max;
} else // <legacyCode>
{
this.addMessage(0, "Setting boundary by min and max is deprecated");
// Set minimum value
if (json.has("min"))
min = json.get("min").asInt(0);
// Set maximum value
if (json.has("max"))
max = json.get("max").asInt(100);
}
;
// Sanitize max
if (max < 0)
max = 100;
else if (max > 100)
max = 100;
// Sanitize min
if (min < 0)
min = 0;
else if (min > 100)
min = 100;
// Check relation between min and max
if (min > max)
max = max;
SpanQueryWrapper sqw = this._fromKoral(operands.get(0));
if (sqw.maybeExtension())
return sqw.setMin(min).setMax(max);
return new SpanRepetitionQueryWrapper(sqw, min, max);
}
use of de.ids_mannheim.korap.query.wrap.SpanRepetitionQueryWrapper in project Krill by KorAP.
the class TestSpanSequenceQuery method spanSequenceQueryWrapper.
@Test
public void spanSequenceQueryWrapper() throws QueryException {
SpanSequenceQueryWrapper ssqw, ssqw2;
SpanRepetitionQueryWrapper srqw;
SpanClassQueryWrapper scqw;
// Synopsis 1
ssqw = new SpanSequenceQueryWrapper("tokens", "der", "Baum");
assertEquals("spanNext(tokens:der, tokens:Baum)", ssqw.toQuery().toString());
// Synopsis 2
ssqw = new SpanSequenceQueryWrapper("tokens");
ssqw.append("der").append("Baum");
assertEquals("spanNext(tokens:der, tokens:Baum)", ssqw.toQuery().toString());
// Append a sequence
ssqw = new SpanSequenceQueryWrapper("tokens");
ssqw2 = new SpanSequenceQueryWrapper("tokens");
ssqw.append("der").append("Baum");
ssqw2.append("fiel").append("still");
ssqw.append(ssqw2);
// This may not be final
assertEquals("spanNext(spanNext(spanNext(tokens:der, tokens:Baum), tokens:fiel), tokens:still)", ssqw.toQuery().toString());
// Synopsis 3
ssqw = new SpanSequenceQueryWrapper("tokens", "Baum");
ssqw.prepend("der");
assertEquals("spanNext(tokens:der, tokens:Baum)", ssqw.toQuery().toString());
// Prepend a sequence
ssqw = new SpanSequenceQueryWrapper("tokens");
ssqw2 = new SpanSequenceQueryWrapper("tokens");
ssqw.append("fiel").append("still");
ssqw2.append("der").append("Baum");
ssqw.prepend(ssqw2);
// This may change
assertEquals("spanNext(spanNext(spanNext(tokens:der, tokens:Baum), tokens:fiel), tokens:still)", ssqw.toQuery().toString());
// Add constraint
ssqw.withConstraint(2, 4);
// This may change
assertEquals("spanDistance(spanDistance(spanDistance(tokens:der, " + "tokens:Baum, [(w[2:4], ordered, notExcluded)]), " + "tokens:fiel, [(w[2:4], ordered, notExcluded)]), " + "tokens:still, [(w[2:4], ordered, notExcluded)])", ssqw.toQuery().toString());
ssqw = new SpanSequenceQueryWrapper("tokens", "der", "Baum").withConstraint(1, 1);
assertEquals("spanNext(tokens:der, tokens:Baum)", ssqw.toQuery().toString());
ssqw = new SpanSequenceQueryWrapper("tokens", "der", "Baum").withConstraint(1, 2, "s");
assertEquals("spanElementDistance(tokens:der, tokens:Baum, [(s[1:2], ordered, notExcluded)])", ssqw.toQuery().toString());
ssqw = new SpanSequenceQueryWrapper("tokens", "der", "Baum").withConstraint(1, 2, "s").withConstraint(2, 3, "x");
assertEquals("spanMultipleDistance(tokens:der, tokens:Baum, " + "[(s[1:2], ordered, notExcluded), " + "(x[2:3], ordered, notExcluded)])", ssqw.toQuery().toString());
ssqw = new SpanSequenceQueryWrapper("tokens").append("Baum").prepend("der").withConstraint(1, 2, "s", true).withConstraint(2, 3, "x");
assertEquals("spanMultipleDistance(tokens:der, " + "tokens:Baum, [(s[1:2], ordered, excluded), " + "(x[2:3], ordered, notExcluded)])", ssqw.toQuery().toString());
// Support empty class ins sequence
ssqw = new SpanSequenceQueryWrapper("field", "Der");
srqw = new SpanRepetitionQueryWrapper();
scqw = new SpanClassQueryWrapper(srqw, (short) 3);
ssqw.append(scqw);
assertEquals("focus(254: spanContain(<field:base/s:t />, {254: spanExpansion(field:Der, []{1, 1}, right, class:3)}))", ssqw.toQuery().toString());
// Support empty class ins sequence
ssqw = new SpanSequenceQueryWrapper("field");
srqw = new SpanRepetitionQueryWrapper();
ssqw.append(srqw);
scqw = new SpanClassQueryWrapper(ssqw, (short) 2);
try {
scqw.toQuery();
} catch (Exception e) {
fail(e.getMessage() + " (Known issue)");
}
;
}
use of de.ids_mannheim.korap.query.wrap.SpanRepetitionQueryWrapper 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