4
$\begingroup$

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"]

JavaClass[edu.stanford.nlp.ling.CoreAnnotations$AnswerAnnotation,<>]

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

w

« 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)

http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/util/ArrayCoreMap.html#get(java.lang.Class)

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

2 Answers 2

6
$\begingroup$

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

LoadJavaClass["com.wolfram.jlink.JLinkClassLoader"];
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"};

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

LoadJavaClass["edu.stanford.nlp.ie.crf.CRFClassifier"];
LoadJavaClass["com.wolfram.jlink.JLinkClassLoader"];

$classifier = CRFClassifier`getClassifierNoExceptions @ $crf;
$annotation = JLinkClassLoader`classFromName @
                "edu.stanford.nlp.ling.CoreAnnotations$AnswerAnnotation";

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}]
]

(*
{{{Prophecy,O},{tells,O},{us,O},{more,O},{about,O},{the,O},
  {prophet,O},{than,O},{about,O},{the,O},{world,O},{.,O}}}
*)
$\endgroup$
3
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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