通过传递String而不是字符串数组来解析Stanford Parser

时间:2013-12-28 10:35:57

标签: java nlp stanford-nlp

是否可以通过传递字符串而不是字符串数组来解析使用Stanford Parser的句子。这是他们在简短教程(See Docs)中给出的示例:

以下是示例:

    import java.util.*;
    import edu.stanford.nlp.ling.*;
    import edu.stanford.nlp.trees.*;
    import edu.stanford.nlp.parser.lexparser.LexicalizedParser;

    class ParserDemo {
      public static void main(String[] args) {
        LexicalizedParser lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
        lp.setOptionFlags(new String[]{"-maxLength", "80", "-retainTmpSubcategories"});

        String[] sent = { "This", "is", "an", "easy", "sentence", "." }; // This is the sentence to be parsed
        List<CoreLabel> rawWords = Sentence.toCoreLabelList(sent);
        Tree parse = lp.apply(rawWords);
        parse.pennPrint();
        System.out.println();

        TreebankLanguagePack tlp = new PennTreebankLanguagePack();
        GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
        GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
        List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
        System.out.println(tdl);
        System.out.println();

      }

}

我试图看看我是否可以这样做,因为我需要从MySQL数据库中获取句子并将它们直接解析为字符串。我可以纠正句子并将单词,逗号和句点添加到字符串数组中。但是,为了对这些句子进行标记,我将不得不使用Stanford Tokenizer,PTBTokenizer。此处列出的此tokenizer的构造函数

See Docs

需要“java.io.FileReader”对象,但我不是从目录中读取文件。所以我想知道是否有办法通过传递字符串直接解析句子,或者如果我可以通过标记句子而不需要“java.io.FileReader”对象来解决我的问题。

1 个答案:

答案 0 :(得分:1)

对于简单用法,使用语法的默认tokenizer和默认tokenizer选项,您可以使用一种简单方便的方法:

lp.parse(String)

但是您指向的PTBTokenizer方法不会使用FileReader,只需要Reader,因此您也可以轻松地将PTBTokenizer指向将字符串包装在StringReader中的字符串。如果您需要更多地控制标记化的发生,这是正确的方法。

相关问题