gpt4 book ai didi

java - 动态添加属性到 StanfordCoreNLP Annotator 或 Pipeline

转载 作者:行者123 更新时间:2023-11-29 04:26:02 26 4
gpt4 key购买 nike

下面是我的情况。

我有一个处理文本的 TextProcessor 类。我需要在这样的文本中找到引用,然后使用斯坦福的工具 OpenIE 提取信息。我使用这两条管道:

"tokenize,ssplit,pos,lemma,ner,parse,mention,coref" for coreferences.

"tokenize,ssplit,pos,lemma,depparse,natlog,openie" for Information Extraction.

单独使用它们来分析单个文本需要很多时间,但目前我必须这样做,因为将它们一起使用需要大量内存,而且管道会超出我的内存范围。

public class TextProcessor(){
Properties props;
StanfordCoreNLP pipeline;

public TextProcessor() {
props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,mention,coref");
pipeline = new StanfordCoreNLP(props);
}


// Performs NER and COREF
public void process(String text) {
Annotation document = new Annotation(malware.getDescription());
pipeline.annotate(document);

// Process text (tokenization, pos, lemma, ner, coref)....
}

public void extractInformation(String document) {
props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,depparse,natlog,openie");
pipeline = new StanfordCoreNLP(props);

Annotation doc = new Annotation(document);
pipeline.annotate(doc);

// Extract informations from doc ...
}

有没有办法动态地将两个管道放在一起?我的意思是,像这样:

1) "tokenize,ssplit,pos,lemma,ner,depparse,mention,coref"

2) "tokenize,ssplit,pos,lemma,ner,depparse,mention,coref,natlog,openie".

我尝试从第一个方法 process(String text) 返回一个 Annotation 对象,然后在 extractInformation(String text) 方法中将其他三个属性添加到它,像这样:

     public Annotation process(String text) {
Annotation document = new Annotation(malware.getDescription());
pipeline.annotate(document);

// Process text (tokenization, pos, lemma, ner, coref)....
return document;
}

public void extractInformation(Annotation document) {
props.setProperty("annotators","depparse,natlog,openie");
pipeline = new StanfordCoreNLP(props);
pipeline.annotate(document);

// Extract informations from doc ...
}

但是我得到这个错误:

注释器“depparse”需要注释“TextAnnotation”。此注释器的通常要求是:tokenize、ssplit、pos

我认为将新的三个属性(depparse、natlog、openie)添加到一个已经注释的文档(使用 tokenize、ssplit、pos)会起作用,但它没有。

那么,有没有办法将这些属性添加到最旧的管道,避免再次执行所有管道(加上新属性)并避免内存超出其界限?



更新

我需要做的就是

     public Annotation process(String text) {
Annotation document = new Annotation(malware.getDescription());
pipeline.annotate(document);

// Process text (tokenization, pos, lemma, ner, coref)....
StanfordCoreNLP.clearAnnotatorPool(); // <-- Added: to get rid of the models and solve the memory issue
return document;
}

public void extractInformation(Annotation document) {
props.setProperty("annotators","natlog,openie");

props.setProperty("enforceRequirements", "false") //<-- Added

pipeline = new StanfordCoreNLP(props);
pipeline.annotate(document);

// Extract informations from doc ...
}

或者,您可以使用:

pipeline = new StanfordCoreNLP(props, false);

在extractInformation(注释文档)中。

最佳答案

听起来您想构建第一个管道,在一组文档上运行它,清除内存,然后构建第二个管道并在一组文档上运行它。

如果您在同一组注释上运行第二个管道,它只会从第一个管道完成的地方开始。但是您需要将 enforceRequirements 设置为 false,这样第二个管道就不会崩溃。此外,在您使用完第一个管道后,您应该运行 StanfordCoreNLP.clearAnnotatorPool(); 以摆脱模型,否则您将无法解决内存问题。

关于java - 动态添加属性到 StanfordCoreNLP Annotator 或 Pipeline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46194378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com