我正在使用斯坦福大学的core-nlp管道来执行一些基本任务。下面是本教程中的示例代码副本。
public static void testcoreNLP(String inputText) throws IOException {
Properties props = new Properties();
props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
props.put("coref.md.type", "rule");
props.put("coref.mode", "statistical");
props.put("coref.doClustering", "true");
props.put("ner.model", "edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz,edu/stanford/nlp/models/ner/english.muc.7class.distsim.crf.ser.gz,edu/stanford/nlp/models/ner/english.conll.4class.distsim.crf.ser.gz");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
Annotation document = new Annotation(inputText);
pipeline.annotate(document);
List<CoreMap> sentences = document.get(SentencesAnnotation.class);
for(CoreMap sentence: sentences) {
for (CoreLabel token: sentence.get(TokensAnnotation.class)) {
String word = token.get(TextAnnotation.class);
String pos = token.get(PartOfSpeechAnnotation.class);
String ne = token.get(NamedEntityTagAnnotation.class);
System.out.println("word: " + word + " pos: " + pos + " ne:" + ne);
}
}我的方法testcoreNLP (接受字符串inputText)正被其for-loop中的另一个方法(预处理文本的textPreprocessor())调用。
据我所知,每次to testcoreNLP方法加载所有模型文件(某些领域特定的-trained模型文件),每次运行大约消耗3-5秒。
如何将模型加载从运行时中分离出来?
发布于 2017-05-04 11:43:04
您的Java应用程序只需要构建一次管道。你可以把流水线加载代码从方法中去掉,然后只执行一次。
https://stackoverflow.com/questions/43751997
复制相似问题