是否可以通过传递字符串而不是字符串数组来使用斯坦福解析器来解析句子。这是他们在简短教程 ( 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 数据库获取句子并将它们直接解析为字符串。我可以对句子进行分词,并将单词、逗号和句点添加到字符串数组中,但是,要对这些句子进行分词,我必须使用斯坦福分词器、PTBTokenizer。此处列出了此标记生成器的构造函数
( See Docs )
需要一个“java.io.FileReader”对象,但我没有从目录中读取文件。所以我想知道是否有一种方法可以通过传递字符串直接解析句子,或者是否可以通过标记句子来解决我的问题而不需要“java.io.FileReader”对象。
最佳答案
对于简单的使用,使用语法的默认分词器和默认分词器选项,您可以使用一种简单方便的方法:
lp.parse(String)
但是您指向的 PTBTokenizer
方法不采用 FileReader
,它们只采用 Reader
,因此您也可以通过将字符串包装在 StringReader
中,轻松地将 PTBTokenizer
指向字符串。如果您需要更多地控制标记化的发生方式,那么这是正确的方法。
关于java - 通过传递字符串而不是字符串数组来解析句子斯坦福解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20813541/