1 回答
TA贡献1816条经验 获得超6个赞
我没有计时,但你可以试试这个示例代码(将测试字符串添加到字符串列表中)......它应该同时适用于 4 个文档:
package edu.stanford.nlp.examples;
import edu.stanford.nlp.pipeline.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class MultiThreadStringExample {
public static class AnnotationCollector<T> implements Consumer<T> {
List<T> annotations = new ArrayList<T>();
public void accept(T ann) {
annotations.add(ann);
}
}
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,depparse");
props.setProperty("threads", "4");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
AnnotationCollector<Annotation> annCollector = new AnnotationCollector<Annotation>();
List<String> exampleStrings = new ArrayList<String>();
for (String exampleString : exampleStrings) {
pipeline.annotate(new Annotation(exampleString), annCollector);
}
Thread.sleep(10000);
List<CoreDocument> coreDocs =
annCollector.annotations.stream().map(ann -> new CoreDocument(ann)).collect(Collectors.toList());
for (CoreDocument coreDoc : coreDocs) {
System.out.println(coreDoc.tokens());
}
}
}
添加回答
举报