- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将 Spacy 用于 NLP 项目。我有一个要标记为新实体类型的短语列表。我最初尝试训练 NER 模型,但由于术语列表有限,我认为仅使用 Matcher 应该更容易。我在 documentation 中看到您可以基于匹配器将实体添加到文档中。我的问题是:我如何为 新 实体执行此操作,并且没有 NER 管道将任何其他标记标记为该实体?理想情况下,只有通过我的匹配器找到的 token 应该被标记为实体,但我需要将它作为标签添加到 NER 模型中,然后最终将一些标记为实体。
关于如何最好地实现这一目标的任何建议?谢谢!
最佳答案
我想你可能想要实现类似于 this example 的东西——即使用 PhraseMatcher
并分配实体的 custom pipeline component。 spaCy 的内置实体识别器也只是一个管道组件 - 因此您可以将其从管道中删除并添加您的自定义组件:
nlp = spacy.load('en') # load some model
nlp.remove_pipe('ner') # remove the entity recognizer
entity_matcher = EntityMatcher(nlp) # use your own entity matcher component
nlp.add_pipe(entity_matcher) # add it to the pipeline
from spacy.matcher import PhraseMatcher
from spacy.tokens import Span
class EntityMatcher(object):
name = 'entity_matcher'
def __init__(self, nlp, terms, label):
patterns = [nlp(term) for term in terms]
self.matcher = PhraseMatcher(nlp.vocab)
self.matcher.add(label, None, *patterns)
def __call__(self, doc):
matches = self.matcher(doc)
spans = []
for label, start, end in matches:
span = Span(doc, start, end, label=label)
spans.append(span)
doc.ents = spans
return doc
terms
列表和一个要为这些术语分配的
label
:
entity_matcher = EntityMatcher(nlp, your_list_of_terms, 'SOME_LABEL')
nlp.add_pipe(entity_matcher)
print(nlp.pipe_names) # see all components in the pipeline
nlp
时,spaCy 将标记文本文本以创建
Doc
对象并按顺序调用
Doc
上的各个管道组件。您的自定义组件的
__call__
方法然后在文档中查找匹配项,为每个匹配项创建
Span
(允许您分配自定义标签),最后将它们添加到
doc.ents
属性并返回
Doc
。
PhraseMatcher
。
关于nlp - 仅来自 PhraseMatcher 的 Spacy 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49097804/
大家好 我使用 Spacy PhraseMatcher 筛选文本以获取多个标签。 例如史蒂夫·乔布斯是苹果公司的首席执行官。 所以我想得到这个输出史蒂夫·乔布斯(人员)、首席执行官(职位)和苹果(实体
假设我有以下两句话:"Onions are being cut. However, a great big cut to the onions have been observed" ,我希望匹配短语
PhraseMatcher 检查并要求 doc 对象中标记的哪些属性来查找匹配项? 例如,如果我只是使用创建一个doc doc1 = nlp('lead') 那么“lead”标记是一个 ADJ,而如果
我正在使用 Spacy PhraseMatcher 函数从短文本列表中检测某些特定的分类法。这是一个示例列表: SK-washer SKM16-FSt-Geomet 321A SK-washer SK
我正在像这样使用 SpaCy 创建一个短语匹配器: import spacy from spacy.matcher import PhraseMatcher nlp = spacy.load("en"
我创建了一个Spacy Phrasematcher匹配文档中的名称,遵循 tutorial 。我想使用生成的匹配作为附加训练数据来训练 Spacy NER 模型。然而,我的模式分别包含全名(例如“Ba
我将 Spacy 用于 NLP 项目。我有一个要标记为新实体类型的短语列表。我最初尝试训练 NER 模型,但由于术语列表有限,我认为仅使用 Matcher 应该更容易。我在 documentation
我想运行一些多处理模块来并行地对文档运行一些短语匹配。为此,我考虑在一个进程中创建短语匹配对象,然后通过创建 PhraseMatcher 对象的副本在多个进程之间共享。该代码似乎失败了,但没有给出任何
SpaCy 文档和示例表明 PhraseMatcher 类可用于匹配文档中的标记序列。必须提供将要匹配的序列词汇表。 在我的应用程序中,我的文档是标记和短语的集合。有不同类型的实体。数据是远程自然语言
使用术语列表初始化新的 PhraseMatcher 时出现以下错误: ValueError: Pattern length (11) >= phrase_matcher.max_length (10)
我是一名优秀的程序员,十分优秀!