gpt4 book ai didi

stanford-nlp - 斯坦福 NLP 命名实体的多个标记

转载 作者:行者123 更新时间:2023-12-02 03:09:47 27 4
gpt4 key购买 nike

我正在试验使用 Stanford Core NLP 进行命名实体识别。

一些命名实体由多个标记组成,例如,Person:“Bill Smith”。我无法弄清楚使用什么 API 调用来确定何时应将“Bill”和“Smith”视为单个实体,以及何时应将它们视为两个不同的实体。

是否有一些不错的文档可以解释这一点?

这是我当前的代码:

    InputStream is = getClass().getResourceAsStream(MODEL_NAME);
if (MODEL_NAME.endsWith(".gz")) {
is = new GZIPInputStream(is);
}
is = new BufferedInputStream(is);

Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");

AbstractSequenceClassifier<CoreLabel> classifier = CRFClassifier.getClassifier(is);
is.close();

String text = "Hello, Bill Smith, how are you?";

List<List<CoreLabel>> sentences = classifier.classify(text);
for (List<CoreLabel> sentence: sentences) {
for (CoreLabel word: sentence) {
String type = word.get(CoreAnnotations.AnswerAnnotation.class);
System.out.println(word + " is of type " + type);
}
}

此外,我不清楚为什么“PERSON”注释会作为 AnswerAnnotation 而不是 CoreAnnotations.EntityClassAnnotation、EntityTypeAnnotation 或其他东西返回。

最佳答案

您应该使用“entitymentions”注释器,它将使用与实体相同的 ner 标签标记连续的标记序列。每个句子的实体列表将存储在 CoreAnnotations.MentionsAnnotation.class 键下。每个提及的实体本身都是一个 CoreMap。

查看此代码可能会有所帮助:

https://github.com/stanfordnlp/CoreNLP/blob/master/src/edu/stanford/nlp/pipeline/EntityMentionsAnnotator.java

一些示例代码:

import java.io.*;
import java.util.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.util.*;



public class EntityMentionsExample {

public static void main (String[] args) throws IOException {
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,entitymentions");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
String text = "Joe Smith is from Florida.";
Annotation annotation = new Annotation(text);
pipeline.annotate(annotation);
System.out.println("---");
System.out.println("text: " + text);
for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
for (CoreMap entityMention : sentence.get(CoreAnnotations.MentionsAnnotation.class)) {
System.out.print(entityMention.get(CoreAnnotations.TextAnnotation.class));
System.out.print("\t");
System.out.print(
entityMention.get(CoreAnnotations.NamedEntityTagAnnotation.class));
System.out.println();
}
}
}
}

关于stanford-nlp - 斯坦福 NLP 命名实体的多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40618856/

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