CoreNLP:提供pos标签

时间:2016-09-04 19:39:04

标签: parsing tokenize pos-tagger stanford-nlp

我的文字已经标记化,句子分割和POS标记。

我想使用CoreNLP来另外注释lemmas(lemma),命名实体(ner),contituency和dependency parse(parse),和共鸣(dcoref)。

是否有命令行选项和选项文件规范的组合可以从命令行实现这一点?

根据this question,我可以要求解析器将空格视为分隔标记,并将新行作为分隔句子添加到我的属性文件中:

tokenize.whitespace = true
ssplit.eolonly = true

这很有效,所以剩下的就是向CoreNLP指定我也想提供POS标签。

当单独使用Stanford Parser时,seems to be possible使用现有的POS标签,但将该语法复制到CoreNLP的调用似乎不起作用。例如,这不起作用:

java -cp *:./* -Xmx2g edu.stanford.nlp.pipeline.StanfordCoreNLP -props my-properties-file -outputFormat xml -outputDirectory my-output-dir -sentences newline -tokenized -tagSeparator / -tokenizerFactory edu.stanford.nlp.process.WhitespaceTokenizer -tokenizerMethod newCoreLabelTokenizerFactory -file my-annotated-text.txt

虽然this question涵盖了程序化调用,但我从命令行调用CoreNLP作为更大系统的一部分,所以我真的在问这是否可以通过命令行选项实现这一点。

1 个答案:

答案 0 :(得分:2)

我不认为这可以通过命令行选项实现。

如果你想要,你可以制作一个自定义注释器并将其包含在你的管道中,你就可以走这条路。

以下是一些示例代码:

package edu.stanford.nlp.pipeline;

import edu.stanford.nlp.util.logging.Redwood;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.util.concurrent.MulticoreWrapper;
import edu.stanford.nlp.util.concurrent.ThreadsafeProcessor;

import java.util.*;

public class ProvidedPOSTaggerAnnotator {

  public String tagSeparator;

  public ProvidedPOSTaggerAnnotator(String annotatorName, Properties props) {
    tagSeparator = props.getProperty(annotatorName + ".tagSeparator", "_");
  }

  public void annotate(Annotation annotation) {

    for (CoreLabel token : annotation.get(CoreAnnotations.TokensAnnotation.class)) {
      int tagSeparatorSplitLength = token.word().split(tagSeparator).length;
      String posTag = token.word().split(tagSeparator)[tagSeparatorSplitLength-1];
      String[] wordParts = Arrays.copyOfRange(token.word().split(tagSeparator), 0, tagSeparatorSplitLength-1);
      String tokenString = String.join(tagSeparator, wordParts);
      // set the word with the POS tag removed
      token.set(CoreAnnotations.TextAnnotation.class, tokenString);
      // set the POS
      token.set(CoreAnnotations.PartOfSpeechAnnotation.class, posTag);
    }
  }
}

如果您为令牌提供以“_”分隔的POS令牌,这应该有效。您可以使用forcedpos.tagSeparator属性更改它。

如果设置customAnnotator.forcedpos = edu.stanford.nlp.pipeline.ProvidedPOSTaggerAnnotator

到属性文件,在CLASSPATH中包含上面的类,然后在“tokenize”之后在注释器列表中包含“forcedpos”,你应该能够传入自己的pos标签。

我可能会对此进行更多清理,并将其实际包含在以后的版本中供人们使用!

我没有时间真正测试这段代码,如果你试一试并发现错误请告诉我,我会解决它!

相关问题