Spacy Matcher Pattern

Viewed 374
import spacy
from spacy.matcher import DependencyMatcher

nlp = spacy.load("en_core_web_sm")

pattern = [
  {
    "RIGHT_ID": "target",
    "RIGHT_ATTRS": {"POS": "NOUN"}
  },

  {
    "LEFT_ID": "target",
    "REL_OP": ">",
    "RIGHT_ID": "verb",
    "RIGHT_ATTRS": {"POS": {"IN": ["VERB"]}}
  },
    {
    "LEFT_ID": "target",
    "REL_OP": ">",
    "RIGHT_ID": "adj",
    "RIGHT_ATTRS": {"POS": {"IN": ["ADJ"]}}
  }
]

matcher = DependencyMatcher(nlp.vocab)
matcher.add("FOUNDED", [pattern])

text = "the bag that I bought is really beautiful"
doc = nlp(text)
for match_id, (target, verb, adj) in matcher(doc):
    print(doc[target], doc[verb], doc[adj])

Hi I'm learning how to use Spacy Matcher, I'm doing some test to find the verb and the adj related to the noun. Hoping to get "bag bought beautiful" as a result. I don't get anything and I don't know what I have done wrong. Any idea ? Thank you !

1 Answers

If the dependency matcher isn't working the way you think it should, look at the dependency parse!

import spacy

nlp = spacy.load("en_core_web_sm")

text = "the bag that I bought is really beautiful"
doc = nlp(text)

for tok in doc:
    print(tok.i, tok, tok.pos_, tok.dep_, tok.head.i, sep="\t")

output:

0       the     DET     det     1
1       bag     NOUN    nsubj   5
2       that    DET     dobj    4
3       I       PRON    nsubj   4
4       bought  VERB    relcl   1
5       is      AUX     ROOT    5
6       really  ADV     advmod  7
7       beautiful       ADJ     acomp   5

Here you can see that "beautiful" does not have "bought" as a head but "is", because "is" is the main verb in the sentence. Since you used ">" as the rel op, you don't get a match because "beautiful" is not directly under "bought" - in fact, it's not under it at all. So if you want to match this sentence you need to rethink your rule. Frankly if you want to match relative clauses like this it's going to be a little difficult because the ADJ can be basically anywhere in the tree.

Note that your rule almost matches a simpler sentence like "I bought a beautiful bag", but that also doesn't work because "bag" is under the verb, not over it. It's usually easier to use a verb as the anchor because they're higher up in the tree.

Related