简体   繁体   中英

java lang Class Cast Exception

I have written a code which can reduce the grammatical boundaries for a text, but when I run the program this exception comes up

java.lang.ClassCastException

here is the class that i run,

public class paerser {
public static void main (String [] arg){
    LexicalizedParser lp = new LexicalizedParser("grammar/englishPCFG.ser.gz");
        lp.setOptionFlags("-maxLength", "500", "-retainTmpSubcategories");
        TreebankLanguagePack tlp = new PennTreebankLanguagePack();
       GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
       String text = "John, who was the CEO of a company, played golf.";
       edu.stanford.nlp.trees.Tree parse = lp.apply(Arrays.asList(text));
       GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
       List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
       System.out.println(tdl);

}
}

Updated,

here is the full stack trace ...

Loading parser from serialized file grammar/englishPCFG.ser.gz ... done [1.5 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:346)
    at edu.stanford.nlp.parser.lexparser.LexicalizedParser.parse(LexicalizedParser.java:386)
    at edu.stanford.nlp.parser.lexparser.LexicalizedParser.apply(LexicalizedParser.java:304)
    at paerser.main(paerser.java:19)
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:317)
    at paerser.main(paerser.java:19)

Stacktrace shows that ExhaustivePCFGParser's parse method is being used. It expects a List of HasWord objects. You are passing a list of String. Hence, the exception.

public boolean parse(List<? extends HasWord> sentence) { // ExhaustivePCFGParser

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM