0

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

1 Answer 1

2

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"}}]
           }]
1
  • Can you maybe help me with this?
    – taga
    Commented Mar 27, 2021 at 0:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.