【问题标题】:Spacy phrasematcher adding patterns with same match idSpacy 短语匹配器添加具有相同匹配 id 的模式
【发布时间】:2019-12-17 01:14:10
【问题描述】:

我正在使用 spacy 的 PhraseMatcher 来识别用户 cmets 中的药物名称。当应用程序服务器启动时,我正在从一个大文件中预加载名称,但不想在每个文档进程中都这样做,因为这需要一段时间。 在处理新文档时,我想将识别的新药物添加到短语匹配器中,以便它识别新名称。但是,spacy 不会接受具有相同 match_id 的新药。任何帮助将不胜感激。

# Global preload when application server starts
import spacy
from spacy.matcher import PhraseMatcher
nlp = spacy.blank("en")

drug_list = []  # file import with 15k drug names
phrasematcher = PhraseMatcher(nlp.vocab, attr='LOWER')
phrasematcher.add('drug', [nlp(itm) for itm in drug_list])
# The above process takes about couple of seconds to load so this process cannot run everytime

# Post process new documents
phrasematcher.add('drug', [nlp(itm) for itm in ['new drug 1', 'new drug 2']])

我收到以下错误。

span = Span(doc_pm, start, end, label=match_id)
File "span.pyx", line 118, in spacy.tokens.span.Span.__cinit__
ValueError: [E084] Error assigning label ID 16065740214838660377 to span: not in StringStore.

当我使用 match_id 查找哈希然后使用它时,我得到一个不同的错误。

phrasematcher.add(phrasematcher.vocab.strings['drug'], [nlp(itm) for itm in ['new drug 1', 'new drug 2']])

错误:

File "phrasematcher.pyx", line 222, in spacy.matcher.phrasematcher.PhraseMatcher.add
TypeError: an integer is required
Spacy 2.2 
Ubuntu 16 
Python 3.6

【问题讨论】:

    标签: python-3.x spacy


    【解决方案1】:

    问题可能是doc_pm 没有使用用于创建PhraseMatcher 的相同nlp 管道进行处理,因此它们无法访问相同的底层StringStore。检查是否doc_pm.vocab == phrasematcher.vocab。如果它们不相同,那就是导致StringStore 错误与Span 的原因。

    最直接的解决方案是始终使用相同的nlp 管道,这样所有组件都只有一个词汇。如果由于某种原因这不可行,它也应该在PhraseMatcher vocab 中显式查找标签:

    span = Span(doc_pm, start, end, label=phrasematcher.vocab.strings[match_id])
    

    【讨论】:

    • 感谢您的建议。我从维护管道中得到了一些提示,我意识到我可以使用 EntityRuler 解决我的问题。
    猜你喜欢
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多