2

以下是使用 Stanford Parser API 从标记化字符串数组创建树输出的示例代码。此代码可在此处获得。

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

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

        String[] sent = { "This", "is", "an", "easy", "sentence", "." };
        Tree parse = (Tree) lp.apply(Arrays.asList(sent));
        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();

        TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
        tp.printTree(parse);
    }
} 

上面的代码不起作用。它给了我这些错误:

Loading parser from serialized file englishPCFG.ser.gz ... done [1.6 sec].
Following exception caught during parsing:
    java.lang.ClassCastException: java.lang.String cannot be cast to     edu.stanford.nlp.ling.HasWord
    at edu.stanford.nlp.parser.lexparser.ExhaustivePCFGParser.parse(ExhaustivePCFGParser.java:357)
    at edu.stanford.nlp.parser.lexparser.LexicalizedParserQuery.parse(LexicalizedParserQuery.java:247)
    at edu.stanford.nlp.parser.lexparser.LexicalizedParser.apply(LexicalizedParser.java:283)
    at Test.main(Test.java:15)
Recovering using fall through strategy: will construct an (X ...) tree.
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be   cast to edu.stanford.nlp.ling.HasWord
    at edu.stanford.nlp.parser.lexparser.LexicalizedParser.apply(LexicalizedParser.java:298)
    at Test.main(Test.java:15)

Process completed.

谁能解释一下这个异常报告?

4

0 回答 0