如何解析一个句子并获得一个句子的标签和单词的输出?

时间:2013-10-18 16:16:55

标签: java parsing string-parsing

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

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

我该怎么做才能获得输出?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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);
    }
}