-1

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)
4
  • Provide the stacktrace, and identify the line which throws the exception. Commented Mar 13, 2012 at 4:20
  • Post the full stack trace, not just error message Commented Mar 13, 2012 at 4:20
  • 1
    I already update the question with the full track trace Commented Mar 13, 2012 at 4:25
  • Does this answer your question? java.lang.ClassCastException Commented Mar 15, 2024 at 10:53

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.