首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >斯坦福纳:我可以在代码中同时使用两个分类器吗?

斯坦福纳:我可以在代码中同时使用两个分类器吗?
EN

Stack Overflow用户
提问于 2014-12-02 19:38:49
回答 1查看 866关注 0票数 2

在我的代码中,我从第一个分类器获得Person识别,对于我制作的第二个分类器,我添加了一些要识别或注释为Organization的单词,但它没有注释Person

我要从他们两个人那里得到好处,我怎么能做到呢?

我正在使用Netbeans,这是代码:

代码语言:javascript
复制
String serializedClassifier = "classifiers/english.all.3class.distsim.crf.ser.gz";
String serializedClassifier2 = "/Users/ha/stanford-ner-2014-10-26/classifiers/dept-model.ser.gz";

if (args.length > 0) {
  serializedClassifier = args[0];
}

AbstractSequenceClassifier<CoreLabel> classifier = CRFClassifier.getClassifier(serializedClassifier);
AbstractSequenceClassifier<CoreLabel> classifier2 = CRFClassifier.getClassifier(serializedClassifier2);

  String fileContents = IOUtils.slurpFile("/Users/ha/NetBeansProjects/NERtry/src/nertry/input.txt");
  List<List<CoreLabel>> out = classifier.classify(fileContents);
  List<List<CoreLabel>> out2 = classifier2.classify(fileContents);

  for (List<CoreLabel> sentence : out) {
      System.out.print("\nenglish.all.3class.distsim.crf.ser.gz: ");
    for (CoreLabel word : sentence) {
      System.out.print(word.word() + '/' + word.get(CoreAnnotations.AnswerAnnotation.class) + ' ');
    }

  for (List<CoreLabel> sentence2 : out2) {
      System.out.print("\ndept-model.ser.gz");
    for (CoreLabel word2 : sentence2) {
      System.out.print(word2.word() + '/' + word2.get(CoreAnnotations.AnswerAnnotation.class) + ' ');
    }

    System.out.println();
  }
}

问题来自于我得到的结果:

代码语言:javascript
复制
english.all.3class.distsim.crf.ser.gz: What/O date/O did/O James/PERSON started/O his/O job/O in/O Human/O and/O Finance/O ?/O 
dept-model.ser.gzWhat/O date/O did/O James/ORGANIZATION started/O his/O job/O in/O Human/ORGANIZATION and/O Finance/ORGANIZATION ?/O 

它从第二个分类器中将名称识别为组织,我需要将其注释为PERSON。有什么帮助吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-08 22:33:41

您应该使用的类是NERClassifierCombiner。它的语义是,当您指定分类器时,它按照从左到右的顺序运行分类器(可以在构造函数中给出任何数字),而且以后的分类器不能注释与先前分类器的实体标记重叠的实体,但在其他情况下可以自由地添加注释。因此,在简单的偏好排序中,更喜欢早期的分类器。下面我给出一个完整的代码示例。

(如果你正在训练你自己的分类器,通常最好是把所有的实体训练在一起,这样它们就可以在指定的类别中互相影响。)但是这种简单的偏好排序通常运行得很好,我们自己使用它。)

代码语言:javascript
复制
import edu.stanford.nlp.ie.NERClassifierCombiner;
import edu.stanford.nlp.io.IOUtils;
import edu.stanford.nlp.ling.CoreLabel;

import java.io.IOException;
import java.util.List;

public class MultipleNERs {

  public static void main(String[] args) throws IOException {
    String serializedClassifier = "classifiers/english.all.3class.distsim.crf.ser.gz";
    String serializedClassifier2 = "classifiers/english.muc.7class.distsim.crf.ser.gz";

    if (args.length > 0) {
      serializedClassifier = args[0];
    }

    NERClassifierCombiner classifier = new NERClassifierCombiner(false, false, 
            serializedClassifier, serializedClassifier2);

    String fileContents = IOUtils.slurpFile("input.txt");
    List<List<CoreLabel>> out = classifier.classify(fileContents);

    int i = 0;
    for (List<CoreLabel> lcl : out) {
      i++;
      int j = 0;
      for (CoreLabel cl : lcl) {
        j++;
        System.out.printf("%d:%d: %s%n", i, j,
                cl.toShorterString("Text", "CharacterOffsetBegin", "CharacterOffsetEnd", "NamedEntityTag"));
      }
    }
  }

}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27257554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档