SpaCy" Matcher" (Gazetteer)格式工作,对于nlp.matcher.add方法?

时间:2016-01-04 20:09:24

标签: python spacy

我开始使用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"
            }
        ]
    ]
)

感谢您的时间。

2 个答案:

答案 0 :(得分:2)

当然你可以链接它们,但据我所知,这并不是spaCy开箱即用的。您可以设置自己的类别类型(例如,SINGER而不是PRODUCT;请注意,这当前已损坏,您可能需要使用v0.93),然后使用DBpedia条目填充它(例如 David Bowie 而不是 Google Now )。完成此操作后,您可以使用实体及其URL之间的映射。自动完成最后一个链接的东西可能会出现,因为此评论建议

 {"wiki_en": "Google_Now"}, # Arbitrary attributes. Currently unused.

答案 1 :(得分:1)

使用spaCy> v1,您现在可以add a callback function到匹配器。我可以想象这样的东西适用于你的用例:

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)