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! :)
How to parse a sentence and get output of tags and words for a sentence?
-
01-07-2022 - |
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?
Solución
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