斯坦福解析器:一个句子一个句子的命令行



是否有一种方法可以从命令行调用斯坦福解析器,以便它一次解析一个句子,并且在特定句子出现问题时只是转到下一个句子?

更新:

我一直在改编斯坦福发布的脚本dnlp帮助。然而,我注意到,在corenlp(2015-04-20)的最后一个版本中,CCprocessed依赖项存在问题:崩溃似乎没有发生(如果我在输出上执行grep pre_,我什么也找不到)。例如,崩溃适用于2015-04-20和PCFG,所以我认为这个问题是特定于模型的。

如果我在corenlp 2015-01-29中使用完全相同的java类(与depparse。模型更改为解析。模型,并删除原始依赖部分),折叠工作得很好。也许我只是用错误的方式使用解析器,这就是为什么我在这里重新发布,而不是开始一个新的帖子。下面是类的更新代码:

import java.io.*;
import java.util.*;
import edu.stanford.nlp.io.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.semgraph.*;
import edu.stanford.nlp.trees.TreeCoreAnnotations.*;
import edu.stanford.nlp.util.*;

public class StanfordSafeLineExample {
public static void main (String[] args) throws IOException {
    // build pipeline                                                                                                                                                                                    
    Properties props = new Properties();
    props.setProperty("annotators","tokenize, ssplit, pos, lemma, depparse");
    props.setProperty("ssplit.eolonly","true");
    props.setProperty("tokenize.whitespace","false");
    props.setProperty("depparse.model", "edu/stanford/nlp/models/parser/nndep/english_SD.gz");
    props.setProperty("parse.originalDependencies", "true");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    // open file                                                                                                                                                                                         
    BufferedReader br = new BufferedReader(new FileReader(args[0]));
    // go through each sentence                                                                                                                                                                          
    for (String line = br.readLine() ; line != null ; line = br.readLine()) {
        try {
            Annotation annotation = new Annotation(line);
            pipeline.annotate(annotation);
            ArrayList<String> edges = new ArrayList<String>();
            CoreMap sentence = annotation.get(CoreAnnotations.SentencesAnnotation.class).get(0);
            System.out.println("sentence: "+line);
            for (CoreLabel token: annotation.get(CoreAnnotations.TokensAnnotation.class)) {
                    Integer identifier = token.get(CoreAnnotations.IndexAnnotation.class);
                    String word = token.get(CoreAnnotations.TextAnnotation.class);
                    String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
                    String lemma = token.get(CoreAnnotations.LemmaAnnotation.class);
                    System.out.println(identifier+"t"+word+"t"+pos+"t"+lemma);
            }
            SemanticGraph tree = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
            SemanticGraph tree2 = sentence.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
            System.out.println("---BASIC");
            System.out.println(tree.toString(SemanticGraph.OutputFormat.READABLE));
            System.out.println("---CCPROCESSED---");
            System.out.println(tree2.toString(SemanticGraph.OutputFormat.READABLE)+"</s>");
        } catch (Exception e) {
            System.out.println("Error with this sentence: "+line);
            System.out.println("");
        }
    }
}

}

处理这个问题有很多方法。

我的方法是运行斯坦福CoreNLP管道。

在这里你可以得到相应的jar:

http://nlp.stanford.edu/software/corenlp.shtml

进入目录stanford-corenlp-full-2015-04-20后

你可以发出这样的命令:

java -cp "*" -Xmx2g edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,parse -ssplit。eolonly -outputFormat text -file sample_sentences.txt

sample_sentences.txt将包含要解析的句子,每行一个句子

这将把结果放在sample_sentences.txt中。你可以用一些简单的脚本来提取。

如果你改变-outputFormat为json而不是text,你会得到一些json,你可以很容易地加载并从

得到解析

如果您对这种方法有任何问题,请告诉我,我可以修改答案以进一步帮助您/澄清!

更新:

我不知道你是怎么操作的,但是这些选项可能会有帮助。

如果您使用-fileList在文件列表上运行管道,而不是在单个文件上运行管道,然后使用以下标志:-continueOnAnnotateError它应该会跳过错误文件,这是一种进步,尽管不可避免地不只是跳过错误句子

我写了一些Java来做你所需要的,所以我会尽量在接下来的24小时内发布,如果你只是想使用我拼凑的Java代码,我还在检查它…

下面是一些示例代码:

import java.io.*;
import java.util.*;
import edu.stanford.nlp.io.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.semgraph.*;
import edu.stanford.nlp.trees.TreeCoreAnnotations.*; 
import edu.stanford.nlp.util.*;

public class StanfordSafeLineExample {
        public static void main (String[] args) throws IOException {
            // build pipeline
            Properties props = new Properties();
            props.setProperty("annotators","tokenize, ssplit, pos, depparse");
            props.setProperty("ssplit.eolonly","true");
            StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
            // open file
            BufferedReader br = new BufferedReader(new FileReader(args[0]));
            // go through each sentence
            for (String line = br.readLine() ; line != null ; line = br.readLine()) {
                try {
                    Annotation annotation = new Annotation(line);
                    pipeline.annotate(annotation);
                    ArrayList<String> edges = new ArrayList<String>();
                    CoreMap sentence = annotation.get(CoreAnnotations.SentencesAnnotation.class).get(0);
                    SemanticGraph tree = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
                    System.out.println("---");
                    System.out.println("sentence: "+line);
                    System.out.println(tree.toString(SemanticGraph.OutputFormat.READABLE));
                } catch (Exception e) {
                    System.out.println("---");
                    System.out.println("Error with this sentence: "+line);
                }
            }
        }
}

指令:

  • 剪切并粘贴到standfordsafelineexample .java
  • 将该文件放入目录stanford- corenpp -full-2015-04-20
  • javac -cp "*:."StanfordSafeLineExample.java
  • 将句子每行一个句子添加到名为sample_sentences.txt的文件中
  • java -cp "*:."StanfordSafeLineExample sample_sentences.txt

最新更新