简体   繁体   English

如何在 Spacy 中向匹配器添加多个模式

[英]How to add multiple patterns to the matcher in Spacy

I want to know how can I add different patterns to the matcher.我想知道如何向匹配器添加不同的模式。 What I want is to do something like this我想要的是做这样的事情

matcher.add("COUNTRIES",None,*patterncountry)
matcher.add("ZONES",None,*patternzones)
matcher.add("DAYS",None,patterndays)

So now I have 3 patterns.所以现在我有 3 个模式。 How can this be achive?这怎么可能实现?

In SpaCy, the the Matcher lets you match sequences based on lists of token descriptions while the PhraseMatcher lets you efficiently match large terminology lists.在 SpaCy 中, Matcher可让您根据标记描述列表匹配序列,而PhraseMatcher可让您有效地匹配大型术语列表。 In your case the PhraseMatcher would be more appropriate to use.在您的情况下, PhraseMatcher更适合使用。

import spacy
from spacy.matcher import PhraseMatcher

nlp = spacy.load("en_core_web_sm")
phrase_matcher = PhraseMatcher(nlp.vocab)

countries = [nlp.make_doc(text) for text in ['Canada', 'United States', 'Mexico']]
days = [nlp.make_doc(text) for text in ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
                                        'Friday', 'Saturday', 'Sunday']]

phrase_matcher.add("COUNTRIES",None, *countries)
phrase_matcher.add("DAYS",None, *days)


text = 'On Monday I travelled to Canada.'
doc = nlp(text)
matches = phrase_matcher(doc)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM