This one is good - it tells that method get
has been called and only failed because we tried to access 0th element of 0-length array.
And now some magic of JVM:
...
jsentences = jcall(doc.jann, "get", JObject, (JClass,), JSentencesAnnotationClass)
# what is the actual class of returned object?
# note: this syntax is equivalent to JavaCall.getname(JavaCall.getclass(jsentences))
jsentences |> JavaCall.getclass |> JavaCall.getname
# ==> "java.util.ArrayList"
# call .get()
jcall(jsentences, "get", JObject, (jint,), 0)
# ==> java.lang.NoSuchMethodError: get
# convert ArrayList to... well... ArrayList, and try again
JArrayList = @jimport java.util.ArrayList
jsentences2 = convert(JArrayList, jsentences)
jcall(jsentences2, "get", JObject, (jint,), 0)
sent0 = jcall(jsentences2, "get", JObject, (jint,), 0)
# ==> works fine!
The reason is that document.get()
has a signature that returns java.lang.Object
, and jcall
returns JavaObject{:java.lang.Object}
. However, method actually returns an object of type java.lang.ArrayList
(which inherits from java.lang.Object
). I believe (need to check an implementation) that subsequent jcall
thinks that it should call Object.get(int)
because it’s what written in JavaObject{...}
, but instead it should call ArrayList.get(int)
.
I’ll check implementation and maybe submit another PR to JavaCall, but even with the current master you can run:
JProperties = @jimport java.util.Properties
JStanfordCoreNLP = @jimport edu.stanford.nlp.pipeline.StanfordCoreNLP
JAnnotation = @jimport edu.stanford.nlp.pipeline.Annotation
JArrayList = @jimport java.util.ArrayList
JTree = @jimport edu.stanford.nlp.trees.Tree
JSemanticGraph = @jimport edu.stanford.nlp.semgraph.SemanticGraph
JSentencesAnnotationClass = classforname("edu.stanford.nlp.ling.CoreAnnotations\$SentencesAnnotation")
JTreeClass = classforname("edu.stanford.nlp.trees.Tree")
JCollapsedCCProcessedDependenciesAnnotationClass =
classforname("edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations\$CollapsedCCProcessedDependenciesAnnotation")
# example from https://stanfordnlp.github.io/CoreNLP/api.html
pipeline = StanfordCoreNLP(Dict("annotations" =>
"tokenize, ssplit, pos, lemma, ner, parse, dcoref"))
doc = Annotation("The Beatles were an English rock band formed in Liverpool in 1960.")
annotate!(pipeline, doc)
jsentences = jcall(doc.jann, "get", JObject, (JClass,), JSentencesAnnotationClass)
jsentences = convert(JArrayList, jsentences)
sent0 = jcall(jsentences, "get", JObject, (jint,), 0)
sent0 = convert(JAnnotation, sent0)
tree = jcall(sent0, "get", JObject, (JClass,), JTreeClass)
# tree = convert(JTree, tree) # tree is null, IIUC, corresponding annotator didn't run on that sentence
semgraph = jcall(sent0, "get", JObject, (JClass,), JCollapsedCCProcessedDependenciesAnnotationClass)
semgraph = convert(JSemanticGraph, semgraph)