我将Stanford Parser(版本3.5.2)用于NLP应用程序,该应用程序依赖于对依赖关系分析以及来自其他来源的信息的分析。到目前为止,我已经将其用于英语,如下所示:
import java.io.StringReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.process.Tokenizer;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.trees.GrammaticalStructure;
import edu.stanford.nlp.trees.GrammaticalStructureFactory;
import edu.stanford.nlp.trees.TreebankLanguagePack;
import edu.stanford.nlp.trees.TypedDependency;
/**
* Stanford Parser Wrapper (for Stanford Parser Version 3.5.2).
*
*/
public class StanfordParserWrapper {
public static void parse(String en, String align, String out) {
// setup stanfordparser
String grammar = "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz";
String[] options = { "-outputFormat", "wordsAndTags, typedDependencies" };
LexicalizedParser lp = LexicalizedParser.loadModel(grammar, options);
TreebankLanguagePack tlp = lp.getOp().langpack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
// read document
Iterable<List<? extends HasWord>> sentences;
Reader r = new Reader(en);
String line = null;
List<List<? extends HasWord>> tmp = new ArrayList<List<? extends HasWord>>();
while ((line = r.getNext()) != null) {
Tokenizer<? extends HasWord> token = tlp.getTokenizerFactory()
.getTokenizer(new StringReader(line));
List<? extends HasWord> sentence = token.tokenize();
tmp.add(sentence);
}
sentences = tmp;
Reader alignment = new Reader(align);
Writer treeWriter = new Writer(out);
// parse
long start = System.currentTimeMillis();
// System.err.print("Parsing sentences ");
int sentID = 0;
for (List<? extends HasWord> sentence : sentences) {
Tree t = new Tree();
t.setSentID(++sentID);
System.out.println("parse Sentence " + t.getSentID() + " "
+ sentence + "...");
// System.err.print(".");
edu.stanford.nlp.trees.Tree parse = lp.parse(sentence);
// ROOT node
Node root = new Node(true, true);
t.setNode(root);
// tagging
int counter = 0;
for (TaggedWord tw : parse.taggedYield()) {
Node n = new Node();
n.setNodeID(++counter);
n.setSurface(tw.value());
n.setTag(tw.tag());
t.setNode(n);
}
t.setSentLength(t.getNodes().size() - 1);
// labeling
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
for (TypedDependency td : tdl) {
Node dep = t.getNodes().get(td.dep().index());
Node gov = t.getNodes().get(td.gov().index());
dep.setLabel(td.reln().toString());
gov.setChild(dep);
dep.setParent(gov);
}
// combine with alignment
t.initialize(alignment.readNextAlign());
treeWriter.write(t);
}
long stop = System.currentTimeMillis();
System.err.println("...done! [" + (stop - start) / 1000 + " sec].");
treeWriter.close();
}
public static void main(String[] args) {
if (args.length == 3) {
parse(args[0], args[1], args[2]);
} else {
System.out.println("Usage:");
}
}
}
“节点”和“树”是我自己的类,而不是斯坦福解析器的类。
我的问题是:我该如何为德语做同样的事情?当我将英语语法模型替换为“ edu / stanford / nlp / models / lexparser / germanPCFG.ser.gz”时,出现以下异常:
Exception in thread "main" java.lang.UnsupportedOperationException: No GrammaticalStructureFactory defined for edu.stanford.nlp.trees.international.negra.NegraPennLanguagePack
at edu.stanford.nlp.trees.AbstractTreebankLanguagePack.grammaticalStructureFactory(AbstractTreebankLanguagePack.java:591)
at StanfordParserWrapper.parse(StanfordParserWrapper.java:46)
at StanfordParserWrapper.main(StanfordParserWrapper.java:117)
“ germanFactored”模型也是如此。显然,我需要在这里做一些不同的事情,因为德国模型不支持GrammaticalStructureFactory。有什么方法仍然可以从德语文本中获取GrammaticalStructure,还是必须完全不同地为德语编写代码?如果是这样,我将不胜感激一些指针,我已经寻找了很多信息,但是找不到我想要的东西。
这似乎很相关:How to parse languages other than English with Stanford Parser? in java, not command lines但是,它只是告诉我,中国模型支持GrammaticalStructureFactory,但不支持德语解析。
非常感谢,
Ĵ
最佳答案
你不知道Stanford解析器不支持德语的依存关系分析(这是从GrammaticalStructureFactory获得的)。
您可以尝试其他依赖项解析器。尽管斯坦福用户将构成树转换为依存关系树是基于规则的,但替代方法通常是概率性的。
mate-tools具有依赖项解析和德语模型
您可以使用MaltParser自己滚动(我认为有些Tüba D/Z语料库的版本与MaltParser兼容)
或者您可以查看ParZu(但请注意,它是Prolog)
关于java - 如何使用Stanford Parser获得德语句子的GrammaticalStructure对象?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31975893/