spacy-course
spacy-course copied to clipboard
Missing implementation to output matches from the matcher at chapter1_03_rule-based-matching.md
Phenomenon
There is the output of the matches from the matcher at chapter1_03_rule-based-matching.md Matching lexical attributes, but the implementation to get it is missing.
doc = nlp("2018 FIFA World Cup: France won!")
(The implementation is missing to output the following 2018 FIFA World Cup:
from doc
.
2018 FIFA World Cup:
Proposal
Add the implementation to get the output. The following code is one of the candidate.
matcher = Matcher(nlp.vocab)
matcher.add("IPHONE_PATTERN", None, pattern)
matches = matcher(doc)
for match_id, start, end in matches:
matched_span = doc[start:end]
print(matched_span.text)
But a little verbose. We can avoid the repetition of the code by function.
def print_matches(doc, pattern);
matcher = Matcher(nlp.vocab)
matcher.add("PATTERN", None, pattern)
matches = matcher(doc)
for match_id, start, end in matches:
matched_span = doc[start:end]
print(matched_span.text)
Then, we can write as following.
doc = nlp("2018 FIFA World Cup: France won!")
print_matches(doc, pattern)