我想要一个温暖的(已经加载的)解析器来解析输入,而不是每次我想解析输入时都创建一个新实例。
我想要一个功能与http://nlp.stanford.edu:8080/parser/类似的解析器。我从Maven安装了stanford-corenlp
。我执行了StanfordCoreNlpDemo
课程。
但我仍然坚持如何将解析器嵌入到我自己的程序中。请提供以编程方式创建解析器的最小示例。
答案 0 :(得分:1)
但请记住:
Stanford Core NLP!= Stanford Parser;前者包括解析器和其他NLP工具。
Core NLP占用了大量的内存!
我一直在努力实现同样的目标。这就是我到目前为止为网络服务所做的事情,你可以用单身人士做类似的事情。
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);
}
}
答案 1 :(得分: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);
}
}
}
这个特殊代码可以获得句子中的所有实体