我想要一個溫暖的(已加載的)解析器來解析輸入,而不是每次我想要解析輸入時創建一個新實例。用於創建溫暖的StanfordNLP解析器的最小示例
我想要一個功能類似於http://nlp.stanford.edu:8080/parser/的解析器。我從Maven安裝了stanford-corenlp
。我執行了StanfordCoreNlpDemo
類。
但我被困在如何將解析器嵌入到我自己的程序中。請提供以編程方式創建解析器的最簡單示例。
我想要一個溫暖的(已加載的)解析器來解析輸入,而不是每次我想要解析輸入時創建一個新實例。用於創建溫暖的StanfordNLP解析器的最小示例
我想要一個功能類似於http://nlp.stanford.edu:8080/parser/的解析器。我從Maven安裝了stanford-corenlp
。我執行了StanfordCoreNlpDemo
類。
但我被困在如何將解析器嵌入到我自己的程序中。請提供以編程方式創建解析器的最簡單示例。
但是記住的是:!
斯坦福核心NLP =斯坦福分析器;前者包含解析器以及其他NLP工具。
核心NLP吃了很多你的RAM!
我一直在努力實現同樣的目標。這是我迄今爲止的一個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);
}
}
你可以試試這個方法
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);
}
}
}
這個特殊的代碼,你可以得到在句子中的所有實體
雖然這是一個非常基本的代碼 – Rakshith 2014-01-09 06:20:25
注意,作爲文檔狀態,解析器是*不*線程安全的。 – 2013-04-26 00:27:26
實際上,自2012年2月發佈2.0版本以來,解析器一直是線程安全的......我想你會發現一些過時的文檔。 – 2014-01-26 19:30:50