Reputation: 85
I am looking for a solution to print all the matching in a line using Spacy matcher
The example goes like this, Here I am trying to extract experience.
doc = nlp("1+ years of experience in XX, 2 years of experiance in YY")
pattern = [{'POS': 'NUM'}, {'ORTH': '+', "OP": "?"}, {"LOWER": {"REGEX": "years?|months?"}}]
matcher = Matcher(nlp.vocab)
matcher.add("Skills", None, pattern)
matches = matcher(doc)
pirnt(doc[matches[0][1]:matches[0][2]]
Here I am getting output 1+ years
.
But I am looking for a solution having output
['1+ years','2 years']
Upvotes: 1
Views: 804
Reputation: 627419
You should specify the first item as 'LIKE_NUM': True
:
pattern = [{'LIKE_NUM': True}, {'ORTH': '+', "OP": "?"}, {"LOWER": {"REGEX": "(?:year|month)s?"}}]
I also contracted the years?|months?
to (?:year|month)s?
, you might even consider matching full token string using ^(?:year|month)s?$
, but that is not necessary at this point.
Code:
import spacy
from spacy.matcher import Matcher
nlp = spacy.load("en_core_web_sm")
matcher = Matcher(nlp.vocab)
pattern = [{'LIKE_NUM': True}, {'ORTH': '+', "OP": "?"}, {"LOWER": {"REGEX": "(?:year|month)s?"}}]
matcher.add("Skills", None, pattern)
doc = nlp("1+ years of experience in XX, 2 years of experiance in YY")
matches = matcher(doc)
for _, start, end in matches:
print(doc[start:end].text)
Output:
1+ years
2 years
Upvotes: 2