[英]How does SpaCy's “Matcher” (Gazetteer) format work, for the nlp.matcher.add method?
我已经开始使用Spacy.io的NLP包,并检查了一些介绍以及一些示例代码。
我对spacy.en.English.matcher.add方法很感兴趣 - 添加我自己的实体的格式是什么? 虽然解释了基本格式,但似乎还有其他功能可用。 我添加的实体可以链接到dbpedia / wikipedia条目或其他外部链接吗?
这是Spacy matcher示例中的代码: https : //github.com/honnibal/spaCy/blob/master/examples/matcher_example.py
nlp.matcher.add(
"GoogleNow", # Entity ID: Not really used at the moment.
"PRODUCT", # Entity type: should be one of the types in the NER data
{"wiki_en": "Google_Now"}, # Arbitrary attributes. Currently unused.
[ # List of patterns that can be Surface Forms of the entity
# This Surface Form matches "Google Now", verbatim
[ # Each Surface Form is a list of Token Specifiers.
{ # This Token Specifier matches tokens whose orth field is "Google"
ORTH: "Google"
},
{ # This Token Specifier matches tokens whose orth field is "Now"
ORTH: "Now"
}
],
[ # This Surface Form matches "google now", verbatim, and requires
# "google" to have the NNP tag. This helps prevent the pattern from
# matching cases like "I will google now to look up the time"
{
ORTH: "google",
TAG: "NNP"
},
{
ORTH: "now"
}
]
]
)
感谢您的时间。
当然你可以链接它们,但据我所知,这并不是spaCy开箱即用的。 您可以设置自己的类别类型(例如,SINGER而不是PRODUCT;请注意,这当前已损坏,您可能需要使用v0.93),然后使用DBpedia条目填充它(例如David Bowie而不是Google Now )。 完成此操作后,您可以使用实体及其URL之间的映射。 正如此评论所暗示的那样,自动完成最后一个链接的东西可能会出现
{"wiki_en": "Google_Now"}, # Arbitrary attributes. Currently unused.
使用spaCy> v1,您现在可以向匹配器添加回调函数 。 我可以想象这样的东西适用于你的用例:
def getlink(matcher, doc, i, matches):
spans = [(ent_id, label, doc[start : end]) for ent_id, label, start, end in matches]
for span in spans:
**do something to get link from wikipedia**
matcher.add_entity('David Bowie', on_match=getlink)
matcher.add_pattern('David Bowie', {ORTH: 'David'}, {ORTH: 'Bowie'}])
doc = Doc(matcher.vocab, words=[u'David', u'Bowie', u'Space', u'Oddity'])
matcher(doc)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.