如何在Java中获得等效的Stanford-Core-nlp Python输出



我在Python中有一个这样的函数-

nlp = StanfordCoreNLP(host + ":" + port)
text = "Joshua Brown, 40, was killed in Florida in May when his Tesla failed to " 
"differentiate between the side of a turning truck and the sky while " 
"operating in autopilot mode."
output = nlp.annotate(
text,
properties={
"outputFormat": "json",
"annotators": "depparse,ner,entitymentions,sentiment"
}
)
pprint(output)

得到如下输出-

{
'sentences': [
{
'basicDependencies': [
{
'dep': 'ROOT',
'dependent': 7,
'dependentGloss': 'killed',
'governor': 0,
'governorGloss': 'ROOT'
},
{
'dep': 'compound',
'dependent': 1,
'dependentGloss': 'Joshua',
'governor': 2,
'governorGloss': 'Brown'
},
…
],
'enhancedDependencies': [
{
'dep': 'ROOT',
'dependent': 7,
'dependentGloss': 'killed',
'governor': 0,
'governorGloss': 'ROOT'
},
{
'dep': 'compound',
'dependent': 1,
'dependentGloss': 'Joshua',
'governor': 2,
'governorGloss': 'Brown'
},

但是我想在Java中做同样的事情,所以我写了一个Java方法,像-

public class NlpTest implements RequestHandler<Input,Output>{
public static String text = "Multiple locations in Maharashtra linked to state transport minister Anil Parab were raided this morning by the Enforcement Directorate (ED) as part of a money-laundering probe relating to alleged irregularities in a land deal.";

@Override
public Output handleRequest(Input input, Context context) {

String output="";
try {
System.out.println("entering method handleRequest");
System.out.println("text = " + text);
Properties props = new Properties();
//props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref, tokensregex, sentiment");           //------>here
props.setProperty("annotators", "depparse,ner,entitymentions,sentiment");
props.setProperty("ner.additional.tokensregex.rules","src/main/java/custom.rules");
//            props.setProperty("coref.algorithm", "neural");
props.setProperty("ner.useSUTime", "false");
props.setProperty("outputFormat", "json");

StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

CoreDocument doc = new CoreDocument(text);
// annotate
pipeline.annotate(doc);
System.out.println("pipeline="+pipeline.toString());
System.out.println("doc.entityMentions() = " + doc.entityMentions());
// display tokens
for (CoreLabel tok : doc.tokens()) {
System.out.println(String.format("%st%dt%d", tok.word(), tok.beginPosition(), tok.endPosition()));
}

我得到单独的字段作为输出,但我想获得直接的JSON输出,我从Python方法中获得。我的问题是如何在Java中做到这一点。不管上面我在Python中做什么,如何在Java中做到这一点?我是斯坦福核心nlp的新手,任何帮助都很感激。谢谢!

您似乎已经很熟悉Java了。指向edu.stanford.nlp.pipeline.JSONOutputter是否足够,或者您需要更多的信息?

https://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/pipeline/JSONOutputter.html

你可以在:edu.stanford.nlp.pipeline.JSONOutputterTest中找到一个使用的例子。

最新更新