spaCy getting the sentences in which a dependency pattern was found

Viewed 53

I used spaCy's dependency matcher to find all the verbs that have a dependency "nsubj" attached to them.

Here is part of the file:

text_file = "The rabbits are jumping in the garden. The grass is getting shorter and shorter by the day. Every evening, they come to this garden at the same time."

The code I used to extract the matches (which works well):

dep_matcher = DependencyMatcher(lang_model.vocab)

dep_pattern = [{'RIGHT_ID': 'verb', 'RIGHT_ATTRS': {'POS': 'VERB'}},
               {'LEFT_ID': 'verb', 'REL_OP': '>', 'RIGHT_ID': 'subject', 'RIGHT_ATTRS': {'DEP': 'nsubj'}}
              ]

dep_matcher.add('nsubj_verb', patterns=[dep_pattern])
doc = lang_model(df1['Transcriptions'][0])
matches_complete = dep_matcher(doc)
inc_sentences = []
text_for_patterns = []
for match in matches_complete:
    pattern_name = match[0]
    matches = match[1]
    verb, subject = matches[0], matches[1]
    patterns = (lang_model.vocab['nsubj_verb'].text, doc[subject], doc[verb])
    text_for_patterns.append(patterns)
    for sentence in doc.sents:
        if not any(word in text_for_patterns for word in sentence.text):   
            inc_sentences.append(sentence.text)

The output I get:

[('nsubj_verb', rabbits, jumping),
 ('nsubj_verb', grass, getting),
 ('nsubj_verb', they, come)
]

Now, I'm very stuck when it comes to getting the sentences in which the matches were found.

Here is what I tried so far (follows directly the first part of the code you can find above):

inc_sentences = []

for sentence in doc.sents:
    if not any(word in sentence.text for word in text_for_patterns):   
        inc_sentences.append(sentence.text)

When I do that, I get this error:

TypeError: 'in <string>' requires string as left operand, not tuple

I feel like this is the closest I've come to the solution... Is there even a way to get what I want?

1 Answers

You can get the sentence that contains a token by using token.sent. Example:

import spacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("I like ginger. It's tasty.")

print(doc[-2]) # => tasty
print(doc[-2].sent) # => It's tasty.

So you can use that with the results of your matcher to get the sentence.

Related