1

我想要一个温暖的(已经加载的)解析器来解析输入,而不是每次我想要解析输入时都创建一个新实例。

我想要一个功能类似于http://nlp.stanford.edu:8080/parser/的解析器。我stanford-corenlp从 Maven 安装。我执行了StanfordCoreNlpDemo课程。

但是我被困在如何将解析器嵌入到我自己的程序中。请提供以编程方式创建解析器的最小示例。

4

2 回答 2

1

但请记住:

  • 斯坦福核心 NLP != 斯坦福解析器;前者包括解析器和其他 NLP 工具。

  • 核心 NLP 会占用大量内存!

我一直在努力实现同样的目标。到目前为止,这就是我对 Web 服务的了解,您可以对单例进行类似的操作。

    public class NLPServlet extends HttpServlet {
    private StanfordCoreNLP pipeline;
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        try {
            Properties props = new Properties();
            props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
            this.pipeline = new StanfordCoreNLP(props);
        } catch (Exception e) {
            System.err.println("Error " + e.getLocalizedMessage());
        }
    }
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        text="blah, blah, blah.";

        // create an empty Annotation just with the given text
        Annotation document = new Annotation(text);

        // run all Annotators on this text
        pipeline.annotate(document);

    }
}
于 2013-04-26T00:26:32.457 回答
0

你可以试试这个方法

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreeCoreAnnotations;
import edu.stanford.nlp.util.ArrayCoreMap;
import edu.stanford.nlp.util.CoreMap;

public class getentity{
    public static void main(String[]args) throws IOException{
     Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, parse,sentiment");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        Annotation annotation= new Annotation("project is good but management is bad, work-culture is good");
        pipeline.annotate(annotation);
        List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
        if (sentences != null && sentences.size() > 0) {

            ArrayCoreMap sentence = (ArrayCoreMap) sentences.get(0);
            Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
            for (CoreMap token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
                ArrayCoreMap aToken = (ArrayCoreMap) token;
                }
             SemanticGraph graph = sentence.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);

            String k=graph.toString("plain");
            System.out.println(k);

  }
  }
}

这个特定的代码你可以得到句子中的所有实体

于 2014-01-08T08:05:16.613 回答