1

我正在使用下面的代码来解析一个句子并获取输出,但它显示了一个错误

(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);
    }
} 

我应该怎么做才能得到输出?

4

2 回答 2

0

您的错误表明您的字符串“sent”不是 apply 方法的有效数据类型。您需要数据类型LIST!尝试将“已发送”字符串放入 LIST(String) 变量中,然后将其传递!:)

于 2013-10-18T17:23:05.970 回答
0

这是你的答案:

您应该使用parse而不是应用。

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);
    }
} 
于 2015-07-14T23:12:14.053 回答