如何使用Stanford CoreNLP有效地构建情感模型训练数据集?

时间:2016-05-12 22:26:09

标签: nlp stanford-nlp

我有兴趣用我自己的数据集训练一个新的情绪模型。我知道我需要创建一个标有句子及其组成短语和单词的情感文件。

我想通过BuildBinarizedDataset找到如何为“我不爱你”这句话创建如下的树:

(1 (1 I) (1 (1 (1 (1 do) (1 not)) (1 (1 love) (1 you))) (1 .)))

然而,以这种格式手动添加标签似乎非常困难,特别是对于较长句子中的短语。如果我可以为标记目的生成以下内容,那么当我准备好训练新模型时进行转换将会容易得多。

sentiment_score pline1

sentiment_score  phrase1

sentiment_score  phrase2

...........................

sentiment_score  phraseN

BLANK ROW

sentiment_score pline2

问题在于我无法弄清楚如何使用解析器从句子中生成这个。如果有人可以提供指导,或者指导我解释这个过程的文档,那将对我有很大的帮助。

1 个答案:

答案 0 :(得分:1)

这是我编写的一些示例代码,用于浏览树并打印出每个子树。因此,要获得打印输出,您只需使用我编写的printSubTrees方法,并将其打印出情感树中的所有内容。

import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.Word;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.parser.lexparser.TreeBinarizer;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.trees.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

public class SubTreesExample {

    public static void printSubTrees(Tree inputTree) {
        ArrayList<Word> words = new ArrayList<Word>();
        for (Tree leaf : inputTree.getLeaves()) {
            words.addAll(leaf.yieldWords());
        }
        System.out.print(inputTree.label()+"\t");
        for (Word w : words) {
            System.out.print(w.word()+ " ");
        }
        System.out.println();
        for (Tree subTree : inputTree.children()) {
            printSubTrees(subTree);
        }
    }

    public static void main(String[] args) {
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        String text = "I do not love you.";
        Annotation annotation = new Annotation(text);
        pipeline.annotate(annotation);
        Tree sentenceTree = annotation.get(CoreAnnotations.SentencesAnnotation.class).get(0).get(
                TreeCoreAnnotations.TreeAnnotation.class);
        printSubTrees(sentenceTree);

    }
}
相关问题