gpt4 book ai didi

nlp - 使用斯坦福 POS Tagger 获取多个标签

转载 作者:行者123 更新时间:2023-12-01 16:42:31 25 4
gpt4 key购买 nike

我正在使用 Stanford POS Tagger 执行 POS 标记。标注器仅返回输入句子的一种可能的标注。例如,当提供输入句子“The clown weeps.”时,词性标注器会生成(错误的)“The_DT clown_NN weeps_NNS ._.”。

但是,我的应用程序将尝试解析结果,并且可能会拒绝 POS 标记,因为无法解析它。因此,在此示例中,它将拒绝“The_DT clown_NN weeps_NNS ._”。但会接受“The_DT clown_NN weeps_VBZ ._”。我认为这是解析器的置信度较低的标记。

因此,我希望词性标注器为每个单词的标注提供多种假设,并用某种置信值进行注释。通过这种方式,我的应用程序可以选择具有最高置信度的 POS 标记,从而实现其目的的有效解析。

我没有找到方法让斯坦福 POS Tagger 为每个单词(甚至整个句子)生成多个(n 个最佳)标记假设。有没有办法做到这一点? (或者,我也可以使用另一个性能相当、支持此功能的词性标注器。)

最佳答案

OpenNLP允许获得 POS 标记的最佳值:

Some applications need to retrieve the n-best pos tag sequences and not only the best sequence. The topKSequences method is capable of returning the top sequences. It can be called in a similar way as tag.

Sequence topSequences[] = tagger.topKSequences(sent);

Each Sequence object contains one sequence. The sequence can be retrieved via Sequence.getOutcomes() which returns a tags array and Sequence.getProbs() returns the probability array for this sequence.

此外,还有一种方法可以让 spaCy 做这样的事情:

Doc.set_extension('tag_scores', default=None)
Token.set_extension('tag_scores', getter=lambda token: token.doc._.tag_scores[token.i])

class ProbabilityTagger(Tagger):
def predict(self, docs):
tokvecs = self.model.tok2vec(docs)
scores = self.model.softmax(tokvecs)
guesses = []
for i, doc_scores in enumerate(scores):
docs[i]._.tag_scores = doc_scores
doc_guesses = doc_scores.argmax(axis=1)

if not isinstance(doc_guesses, numpy.ndarray):
doc_guesses = doc_guesses.get()
guesses.append(doc_guesses)
return guesses, tokvecs


Language.factories['tagger'] = lambda nlp, **cfg: ProbabilityTagger(nlp.vocab, **cfg)

然后,每个标记将具有 tag_scores,其中包含来自 spaCy 的 tag map 的每个词性的概率。 .

来源:https://github.com/explosion/spaCy/issues/2087

关于nlp - 使用斯坦福 POS Tagger 获取多个标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16791716/

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