This is a long question, and quite complicated, at least to me.
I am trying to make a Java module, which is included in BaseX as a jar-file in `lib/custom`. The module contains one XQuery function:
named-entity-recognition($grammar as item(), $options as map(*)?) as function(item()) as node()*
This is a higher-order XQuery function that returns another (anonymous) XQuery function. The returned function is a parser (for named entities), similar to what the `invisible-xml` function returns.
The relevant part (I think) is the following Java function:
@Requires(Permission.NONE)
@Deterministic
@ContextDependent
publicFuncItem namedEntityRecognition(Object grammar, Map<String, String> options) throwsQueryException {
// Types of the arguments of the generated function.
finalVar[] generatedFunctionParameters= { newVarScope().addNew(newQNm("input"), SeqType.ITEM_O, queryContext, null) };
// Type of the generated function.
finalFuncType generatedFunctionType= FuncType.get(SeqType.NODE_ZM, generatedFunctionParameters[0].declType);
// The generated function.
NamedEntityRecognitionFunction nerf= newNamedEntityRecognitionFunction(grammar, options, generatedFunctionType, queryContext);
// Return a function item.
returnnewFuncItem(null, nerf, generatedFunctionParameters, AnnList.EMPTY, generatedFunctionType, generatedFunctionParameters.length, null);
}
The NamedEntityRecognitionFunction has a public Value value(final QueryContext qc) function that does the actual parsing. It starts by getting the value of its (only) parameter:
Value inputValue = arg(0).value(qc); // Java
The namedEntityRecognition function works fine when I use it like this (XQuery):
let $ner-parse := ner:named-entity-recognition($grammar, map{}) return $input => $ner-parse()
However, is does not work when the parsing function is put into a declared variable, like
declare variable $ner-parse as function(item()) as node()* := ner:named-entity-recognition($grammar,map{}); $input => $ner-parse()
In this case, the parameter value obtained by arg(0).value(qc) is null. When I debug this expression, it turns out that the parameter ($input) is not on the stack. The stack (which is qc.stack) is all nulls, and the begin and end indexes of the stack are both 0, meaning that the stack is empty..
This probably means that the query context of a local (let) variable is different from the query context of a global (declare) variable. Maybe I need to indicate that the namedEntityRecognition function returns a higher order function (but how)?
The code for this can be found at https://github.com/nverwer/basex-ner-xar.
If anyone can shed some light on this, that would be very much appreciated.
Best regards, Nico Verwer