I am trying to get started with the Stanford NER tools tools in Mathematica, and am having difficulty translating the demo they provide into JLink. The relevant code in the demo is:

/* Usage: java -cp "stanford-ner.jar:." NERDemo */

import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.ling.CoreAnnotations;


 for (CoreLabel word : sentence) { 
 ... w.word() ... word.get(CoreAnnotations.AnswerAnnotation.class) ...

So I tried:

InstallJava[ClassPath -> ".../stanford-ner.jar"];

answerAnnotation = LoadJavaClass["edu.stanford.nlp.ling.CoreAnnotations$AnswerAnnotation"]


(...things that appear to be working correctly...)


« JavaObject[edu.stanford.nlp.ling.CoreLabel]»

w@word[] (* works *)
w@get[answerAnnotation] (* $Failed *)

Java::argx1: Method named get defined in class edu.stanford.nlp.ling.CoreLabel was called with an incorrect number or type of arguments. The argument was JavaClass[edu.stanford.nlp.ling.CoreAnnotations$AnswerAnnotation,<>].

The (inherited) method signature of get is

get(java.lang.Class<? extends TypesafeMap.Key<VALUE>> key)


  • $\begingroup$ Try instead passing into w@get the following: JavaNew["edu.stanford.nlp.ling.CoreAnnotations$AnswerAnnotation"]@getClass[] (untested). $\endgroup$ – Leonid Shifrin May 22 '14 at 19:24
  • $\begingroup$ cha $ ching. Thanks $\endgroup$ – mfvonh May 22 '14 at 19:30

A Java class object can be recovered using the JLink class loader, e.g.:

answerAnnotation =
  JLinkClassLoader`classFromName @ "edu.stanford.nlp.ling.CoreAnnotations$AnswerAnnotation"

This is necessary because the objects returned by LoadJavaClass are symbolic proxies, not the actual Java class objects themselves.

Here is a complete example loosely following NERDemo.java:

$root = "/some/path/to/stanford-ner-distro";
$jar = FileNameJoin @ {$root, "stanford-ner.jar"};
$crf = FileNameJoin @ {$root, "classifiers", "english.all.3class.distsim.crf.ser.gz"};

ReinstallJava[ClassPath -> $jar, JVMArguments -> "-Xmx8g"];


$classifier = CRFClassifier`getClassifierNoExceptions @ $crf;
$annotation = JLinkClassLoader`classFromName @

JavaBlock @ Module[{labels}
, labels = JavaObjectToExpression /@ JavaObjectToExpression @ $classifier @ classify @
             "Prophecy tells us more about the prophet than about the world."
; Map[{# @ toString[], # @ get @ $annotation}&, labels, {2}]


Leonid Shifrin's answer does the trick: JavaNew["edu.stanford.nlp.ling.CoreAnnotations$AnswerAnnotation"]@getClass[].


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.