Reputation: 3
Im trying to identify entities using regex and tag them using entity ruler. Regex pattern returns a match for Matcher but doesnt return same for Entity ruler and works in normal regex as well.
from spacy.matcher import Matcher
text = u"Name: first last \n Phone: +1223456790 \n e-mail: [email protected]."
doc = nlp(text)
matcher = Matcher(nlp.vocab)
pattern = [{"LOWER": {'REGEX' :"\w+\.\w+\@\w+\.com"}}]
matcher.add("email", None, pattern)
matches = matcher(doc)
print([doc[start:end] for match_id, start, end in matches])
output: [[email protected]]
`
text = u"Name: first last \n Phone: +1223456790 \n e-mail: [email protected]."
from spacy.lang.en import English
from spacy.pipeline import EntityRuler
nlp = English()
ruler = EntityRuler(nlp, overwrite_ents=True)
pattern = [{"label": "Email", "pattern":
{"LOWER": {'REGEX' : "\w+\.\w+\@\w+\.com"}}
}]
ruler.add_patterns(patterns)
nlp.add_pipe(ruler, name='customer')
text = u"Name: first last \n Phone: +1223456790 \n e-mail: [email protected]."
doc = nlp(text)
for ent in doc.ents:
print(ent.text,ent.label_)
` Output: None
Upvotes: 0
Views: 989
Reputation: 11474
The EntityRuler
pattern needs to be provided as a list of token dicts just as in the Matcher
pattern:
pattern = [{"label": "Email", "pattern":
[{"LOWER": {'REGEX' : "\w+\.\w+\@\w+\.com"}}]
}]
Upvotes: 2