I'm trying to match a specific pattern: any verb with a noun ending with a s, t or l. E.g.: Like cat, Eat meal, Make Spices
How Can i do this?
I Know i was doing this:
nlp =spacy.load("en_core_web_sm")
matcher = Matcher(nlp.vocable)
pattern = [{"POS": "VERB"}, {"POS": "NOUN"}]
matcher.add("mypattern", [pattern])
doc = nlp(Verbwithnoun)
matches = matcher(doc)
for match_id, start, end in matches:
string_id = nlp.vocab.strings[match_id]
print(doc[start:end)
But that prints all verb with nouns not the noun ending with a t,l or s. How Can i get spacy to match only specific nouns ending with a t,l or s?
CodePudding user response:
You can post-process the results by checking if the phrase you get ends with either of the three letters:
import spacy
from spacy.matcher import Matcher
nlp = spacy.load("en_core_web_sm")
matcher = Matcher(nlp.vocab)
pattern = [{"POS": "VERB"}, {"POS": "DET", "OP" : "?"}, {"POS": "NOUN"}]
matcher.add("mypattern", [pattern])
Verbwithnoun = "I know the language. I like the cat, I eat a meal, I make spices."
doc = nlp(Verbwithnoun)
matches = matcher(doc)
for match_id, start, end in matches:
string_id = nlp.vocab.strings[match_id]
phrase = doc[start:end]
if phrase.text.endswith('s') or phrase.text.endswith('t') or phrase.text.endswith('l'):
print(doc[start:end])
Output:
like the cat
eat a meal
make spices
CodePudding user response:
Post processing is fine but you can also use regex in patterns directly. See the docs.
nlp =spacy.load("en_core_web_sm")
matcher = Matcher(nlp.vocable)
pattern = [{"POS": "VERB"}, {"POS": "NOUN", "TEXT": {"REGEX": "[lst]$"}}]
matcher.add("mypattern", [pattern])
doc = nlp(Verbwithnoun)
matches = matcher(doc)
for match_id, start, end in matches:
string_id = nlp.vocab.strings[match_id]
print(doc[start:end)