Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing implementation to output matches from the matcher at chapter1_03_rule-based-matching.md #85

Open
icoxfog417 opened this issue Sep 21, 2020 · 0 comments

Comments

@icoxfog417
Copy link
Contributor

icoxfog417 commented Sep 21, 2020

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant