斯坦福分析师(Typed Dependencee)例外

时间:2015-06-01 09:37:44

标签: java eclipse exception nlp stanford-nlp

我想在Stanford Parser中看到NLP的Dependence输出格式。我从这个链接下载了jar文件。 http://nlp.stanford.edu/software/lex-parser.shtml

然后我在Eclipse中导入它并编写以下代码。

package hammad.NLP;

import java.io.StringReader;
import java.util.Collection;
import java.util.List;

import com.chaoticity.dependensee.Main;

import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.process.CoreLabelTokenFactory;
import edu.stanford.nlp.process.PTBTokenizer;
import edu.stanford.nlp.process.TokenizerFactory;
import edu.stanford.nlp.trees.GrammaticalStructure;
import edu.stanford.nlp.trees.GrammaticalStructureFactory;
import edu.stanford.nlp.trees.PennTreebankLanguagePack;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreebankLanguagePack;
import edu.stanford.nlp.trees.TypedDependency;

public class Test {
/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    try
    {

        String text = "A quick brown fox jumped over the lazy dog.";
        TreebankLanguagePack tlp = new PennTreebankLanguagePack();
        GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
        LexicalizedParser lp = LexicalizedParser.loadModel("C:/Stanford Parser/Java/stanford-parser-full-2015-04-20/stanford-parser-3.5.2-models/edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
        lp.setOptionFlags(new String[]{"-maxLength", "500", "-retainTmpSubcategories"});
        TokenizerFactory<CoreLabel> tokenizerFactory =
                PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
        List<CoreLabel> wordList = tokenizerFactory.getTokenizer(new StringReader(text)).tokenize();
        Tree tree = lp.apply(wordList);    
        GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
        Collection<TypedDependency> tdl = gs.typedDependenciesCCprocessed(true);

        Main.writeImage(tree,tdl, "image.png",3);

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

}

它提供以下例外。

Exception in thread "main" java.lang.NoSuchMethodError: edu.stanford.nlp.trees.TypedDependency.gov()Ledu/stanford/nlp/trees/TreeGraphNode;

我搜索了TypedDependency并发现gov()存在于其中但异常即将到来。我搜索了很多关于它但没有找到任何帮助。如果你帮助我,我将感激你。

1 个答案:

答案 0 :(得分:1)

这是由于最近发布的斯坦福NLP代码与DependenSee之间不兼容,后者是针对斯坦福分析师的v2.0.5(2013-04-05)而构建的。如果您注释掉Main.writeImage(tree,tdl, "image.png",3);行,而只是执行System.out.println(tdl);,那么它可以正常工作。

您可以选择:不使用DependenSee,修复DependenSee以与最近的Stanford NLP版本兼容(可能很简单),或者降级到旧版本的Stanford NLP ....