【问题标题】:How can I effectively build a sentiment model training dataset using Stanford CoreNLP?如何使用斯坦福 CoreNLP 有效地构建情感模型训练数据集?
【发布时间】:2016-05-17 22:26:43
【问题描述】:

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

我想出了如何为“我不爱你”这句话创建如下所示的树。通过 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

问题是我无法弄清楚如何使用解析器从句子中生成它。如果有人可以提供指导,或将我引导到可以解释此过程的文档,那将极大地帮助我。

【问题讨论】:

  • 您不能为您的训练数据集生成标签,您必须手动提供标签才能相应地train您的模型。

标签: nlp stanford-nlp


【解决方案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);

    }
}

【讨论】:

  • 感谢您的代码!它使用 Stanford-corenlp-3.6.0.jar 编译得很漂亮,但我无法运行它。你能告诉我这是什么意思吗?错误:发生 JNI 错误,请检查您的安装并重试 线程“main”中的异常 java.lang.NoClassDefFoundError: edu/stanford/nlp/trees/Tree at java.lang.Class.getDeclaredMethods0(Native Method) at java .lang.Class.privateGetDeclaredMethods(Unknown Source) at java.lang.Class.privateGetMethodRecursive(Unknown Source) at java.lang.Class.getMethod0(Unknown Source) ...
  • ... at java.lang.Class.getMethod(Unknown Source) at sun.launcher.LauncherHelper.validateMainClass(Unknown Source) at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) 原因: java.lang.ClassNotFoundException: edu.stanford.nlp.trees.Tree at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass( Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 7 更多
  • 你是如何运行代码的?您似乎遇到了某种 CLASSPATH 问题。
  • 我在包含 java、class 和 jar 文件的文件夹中。我尝试运行以下示例的不同组合。示例: java –cp “Stanford-corenlp3.6.0.jar” –classpath 。子树示例
  • 如果你在包含所有 jar 包的主分发文件夹中,你应该运行 java -cp "*:." ...你需要所有的罐子,你需要在你的 CLASSPATH 中编译 SubTreesExample
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多