Aan de slagGa gratis aan de slag

Debugging patterns (2)

Both patterns in this exercise contain mistakes and won't match as expected. Can you fix them?

The nlp and a doc have already been created for you. If you get stuck, try printing the tokens in the doc to see how the text will be split and adjust the pattern so that each dictionary represents one token.

Deze oefening maakt deel uit van de cursus

Advanced NLP with spaCy

Cursus bekijken

Oefeninstructies

  • Edit pattern1 so that it correctly matches all case-insensitive mentions of "Amazon" plus a title-cased proper noun.
  • Edit pattern2 so that it correctly matches all case-insensitive mentions of "ad-free", plus the following noun.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Create the match patterns
pattern1 = [{'LOWER': 'Amazon'}, {'IS_TITLE': True, 'POS': 'PROPN'}]
pattern2 = [{'LOWER': 'ad-free'}, {'POS': 'NOUN'}]

# Initialize the Matcher and add the patterns
matcher = Matcher(nlp.vocab)
matcher.add('PATTERN1', None, pattern1)
matcher.add('PATTERN2', None, pattern2)

# Iterate over the matches
for match_id, start, end in matcher(doc):
    # Print pattern string name and text of matched span
    print(doc.vocab.strings[match_id], doc[start:end].text)
Code bewerken en uitvoeren