Pregunta

I am using the below code to parse a sentence and get the output but it's showing an error

(method apply in class LexicalizedParser cannot be applied to given types;
  required: List<? extends HasWord>
  found: String
  reason: actual argument String cannot be converted to List<? extends HasWord> by method invocation conversion)
at line parse = (Tree) lp.apply(sent):


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

public class ParserDemo1 {
    public static void main(String[] args){
        LexicalizedParser lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
        lp.setOptionFlags(new String[]{"-maxLength", "80", "-retainTmpSubcategories"});
        String sent="Ohio is located in America";
        Tree parse;
        parse = (Tree) lp.apply(sent);

        List taggedWords = parse.taggedYield();
        System.out.println(taggedWords);
    }
} 

What should I do to get the output?

¿Fue útil?

Solución

Your error indicates that your string "sent" is not a valid data type for the apply method. You need datatype LIST! Try putting your "sent" string into a LIST(String) variable, and passing that instead! :)

Otros consejos

Here is your answer:

You should use parse instead of apply.

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

public class ParserDemo1 {
    public static void main(String[] args){
        LexicalizedParser lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
        lp.setOptionFlags(new String[]{"-maxLength", "80", "-retainTmpSubcategories"});
        String sent="Ohio is located in America";
        Tree parse;
        parse = (Tree) lp.parse(sent);

        List taggedWords = parse.taggedYield();
        System.out.println(taggedWords);
    }
} 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top