首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用斯坦福CoreNLP进行依赖分析中的情感排序节点?

使用斯坦福CoreNLP进行依赖分析中的情感排序节点?
EN

Stack Overflow用户
提问于 2014-05-19 12:41:38
回答 1查看 1.4K关注 0票数 3

我想对一组句子执行依存分析,并查看单个节点的情感评级,就像斯坦福情感树库(http://nlp.stanford.edu/sentiment/treebank.html)一样。

我是CoreNLP应用编程接口的新手,在忙碌了一段时间之后,我仍然不知道如何对排名节点进行依赖关系解析。使用CoreNLP可以做到这一点吗?如果是这样的话,有谁有这样做的经验吗?

EN

回答 1

Stack Overflow用户

发布于 2014-09-19 21:58:22

我修改了包含的StanfordCoreNLPDemo.java文件的代码,以满足我们的情感需求:

导入:

代码语言:javascript
运行
复制
import java.io.*;
import java.util.*;

import edu.stanford.nlp.io.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations.PredictedClass;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.util.*;

正在初始化管道。属性包括引理和情感:

代码语言:javascript
运行
复制
public class StanfordCoreNlpDemo {

  public static void main(String[] args) throws IOException {
    PrintWriter out;
    if (args.length > 1) {
      out = new PrintWriter(args[1]);
    } else {
      out = new PrintWriter(System.out);
    }
    PrintWriter xmlOut = null;
    if (args.length > 2) {
      xmlOut = new PrintWriter(args[2]);
    }
    Properties props = new Properties();
    props.put("annotators", "tokenize, ssplit, pos, lemma, parse, sentiment");
    props.setProperty("tokenize.options","normalizeCurrency=false");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

添加文本。这3句话取自你链接的网站的现场演示。我还打印了顶层注释的键,以查看您可以从它访问什么:

代码语言:javascript
运行
复制
Annotation annotation;
    if (args.length > 0) {
      annotation = new Annotation(IOUtils.slurpFileNoExceptions(args[0]));
    } else {
      annotation = new Annotation("This movie doesn't care about cleverness, wit or any other kind of intelligent humor.Those who find ugly meanings in beautiful things are corrupt without being charming.There are slow and repetitive parts, but it has just enough spice to keep it interesting.");
    }

    pipeline.annotate(annotation);
    pipeline.prettyPrint(annotation, out);
    if (xmlOut != null) {
      pipeline.xmlPrint(annotation, xmlOut);
    }

    // An Annotation is a Map and you can get and use the various analyses individually.
    // For instance, this gets the parse tree of the first sentence in the text.
    out.println();
    // The toString() method on an Annotation just prints the text of the Annotation
    // But you can see what is in it with other methods like toShorterString()
    out.println("The top level annotation's keys: ");
    out.println(annotation.keySet());

对于第一句话,我打印了它的关键字和情感。然后,我遍历它的所有节点。对于每个节点,我打印该子树的叶子,这将是这个节点引用的句子的一部分,节点的名称,它的情感,它的节点向量(我不知道是什么)和它的预测。情绪是一个整数,从0到4。0是非常负面的,1是负面的,2是中性的,3是正面的,4是非常正面的。预测是一个由4个值组成的向量,每个值都包括该节点属于上述每个类的可能性的百分比。第一个值是非常负面的类别,依此类推。最高的百分比是节点的情绪。并不是带注释的树的所有节点都有情感。似乎句子中的每个单词在树中都有两个节点。您可能希望单词是叶子,但它们只有一个子节点,这是一个节点,其键中缺少预测注释的标签。该节点的名称是相同的单词。这就是为什么我在调用函数之前检查预测注释的原因,该函数会获取它。然而,正确的方法是忽略抛出的空指针异常,但我选择详细说明,以使这个答案的读者理解没有关于情绪的信息丢失。

代码语言:javascript
运行
复制
List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
if (sentences != null && sentences.size() > 0) {
  ArrayCoreMap sentence = (ArrayCoreMap) sentences.get(0);


  out.println("Sentence's keys: ");
  out.println(sentence.keySet());

  Tree tree2 = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
  out.println("Sentiment class name:");
  out.println(sentence.get(SentimentCoreAnnotations.ClassName.class));

  Iterator<Tree> it = tree2.iterator();
  while(it.hasNext()){
      Tree t = it.next();
      out.println(t.yield());
      out.println("nodestring:");
      out.println(t.nodeString());
      if(((CoreLabel) t.label()).containsKey(PredictedClass.class)){
          out.println("Predicted Class: "+RNNCoreAnnotations.getPredictedClass(t));
      }
      out.println(RNNCoreAnnotations.getNodeVector(t));
      out.println(RNNCoreAnnotations.getPredictions(t));
  }

最后,一些更多的输出。将打印依赖项。这里的依赖关系也可以由解析树(树或tree2)的访问器访问:

代码语言:javascript
运行
复制
      out.println("The first sentence is:");
      Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
      out.println();
      out.println("The first sentence tokens are:");
      for (CoreMap token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
        ArrayCoreMap aToken = (ArrayCoreMap) token;
        out.println(aToken.keySet());
        out.println(token.get(CoreAnnotations.LemmaAnnotation.class));
      }
      out.println("The first sentence parse tree is:");
      tree.pennPrint(out);
      tree2.pennPrint(out);
      out.println("The first sentence basic dependencies are:"); 
      out.println(sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class).toString(SemanticGraph.OutputFormat.LIST));
      out.println("The first sentence collapsed, CC-processed dependencies are:");
      SemanticGraph graph = sentence.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
      out.println(graph.toString(SemanticGraph.OutputFormat.LIST));
    }
  }

}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23729829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档