[英]How to get the equivalent Stanford-Core-nlp Python output in Java
我在 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("%s\t%d\t%d", tok.word(), tok.beginPosition(), tok.endPosition()));
}
我将单个字段作为输出,但我想获得从 Python 方法获得的直接 JSON 输出。我的问题是如何在 Java 中执行此操作。 无论我在 Python 中做什么,如何在 Java 中做到这一点? 我是 Stanford-core-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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.