What's the ideal way to include dictionaries (gazetteer) in spaCy to improve NER?
我目前正在替换基于nltk实体提取和regexp匹配的系统,其中我有几个命名实体字典。字典实体既有普通类型(PERSON(雇员)等)又有自定义类型(例如SKILL)。我想使用预先训练的spaCy模型,并以某种方式包括我的词典,以提高NER的准确性。这是我对可能的方法的想法:
-
使用spaCy的Matcher API,遍历字典并使用回调添加每个短语以添加实体?
-
我刚刚发现了spacy-lookup,这似乎是提供单词/短语匹配的长列表的一种简便方法。
-
但是,如果我想进行模糊匹配怎么办?有没有一种方法可以直接添加到Vocab中,从而通过Bloom过滤器/ n-gram词向量进行一些模糊匹配,还是有一些适合这种需求的扩展?否则我想我可以复制spacy-lookup并用其他东西替换flashtext机械,例如Levenshtein距离。
-
在玩spaCy的过程中,我确实尝试仅使用字典中的单个单词直接训练NER(没有任何句子上下文),这确实"有效"。但是,我当然必须格外小心,以防止模型忘记所有内容。
任何帮助表示赞赏,我觉得这必须是一个非常普遍的要求,并且很想听听对人们来说最有效的方法。
我建议您查看spaCy的实体标尺。 如果将现有字典转换为用于匹配的模式,则可以为每个实体和新类型添加规则。
这非常强大,因为您可以将其与标准spacy模型中可用的现有统计NER相结合,以实现您提到的某些"模糊匹配"。 从文档:
The entity ruler is designed to integrate with spaCy’s existing statistical models and enhance the named entity recognizer. If it’s added before the"ner" component, the entity recognizer will respect the existing entity spans and adjust its predictions around it. This can significantly improve accuracy in some cases. If it’s added after the"ner" component, the entity ruler will only add spans to the doc.ents if they don’t overlap with existing entities predicted by the model. To overwrite overlapping entities, you can set overwrite_ents=True on initialization.
我将Matcher与动态生成的回调一起使用。 我认为效果很好。
我很好奇为什么Matcher不支持模糊匹配,并发现了spacy作者在一个封闭的问题上的评论。
You really want to precompute the search sets, rather than do them on-the-fly in the matcher. Once you've precomputed the similarity values, you can use extension attributes and a >= comparison in the Matcher to perform the search.
I think this is a case where the implementation details strongly matter, and an API that obscures them would actually be a disservice.
我认为这是一个好方法,它告诉您如何构建所需的内容。