python-3.xnlpspacy-3

Spacy dependency matcher doesnt find matches in reverse


I am trying to find words related to 'poss' to the word 'my' but it doesn't work. For example in reverse,


pattern = [
    {
        "RIGHT_ID": "anchor_founded",
        "RIGHT_ATTRS": {"ORTH": "CEO"}
    },
    {
        "LEFT_ID": "anchor_founded",
        "REL_OP": ">",
        "RIGHT_ID": "founded_subject",
        "RIGHT_ATTRS": {"DEP": "poss"},
    }
]

matcher.add("FOUNDED", [pattern])
doc = nlp("My experienced CEO, has founded two AI startups.")
matches = matcher(doc)

gives anchor_founded: CEO founded_subject: My

But if I anchor this in "My"

pattern = [
    {
        "RIGHT_ID": "anchor_founded",
        "RIGHT_ATTRS": {"ORTH": "My"}
    },
    {
        "LEFT_ID": "anchor_founded",
        "REL_OP": "<",
        "RIGHT_ID": "founded_subject",
        "RIGHT_ATTRS": {"DEP": "poss"},
    }
]

doesn't give any matches. Any thoughts? Ideally I want to get the second thing to work.


Solution

  • {"DEP": "poss"} is a token pattern, which doesn't match the attributes for "founded", only for "My".

    Inspect token.dep_ for the whole sentence and while it may vary a bit by model version, it will be something like this:

    doc = nlp("My experienced CEO, has founded two AI startups.")
    print([t.dep_ for t in doc])
    # ['poss', 'amod', 'nsubj', 'punct', 'aux', 'ROOT', 'nummod', 'compound', 'dobj', 'punct']