NLP: Spacy custom rule based matching

Viewed 36

I am working on spacy and need to find some information like email, phone number and multiple values from text. Below is my code. However there is something which I am doing wrong in matcher, due to which I am not getting desired output. Below is the code.

import spacy
nlp = spacy.load("en_core_web_sm")
from spacy.matcher import Matcher
matcher = Matcher(nlp.vocab)

my_pattern = [{"LOWER": "email"}, {"LOWER": "phone"}]
matcher.add('MyPattern', [my_pattern])
my_text = "email: kashif.jilani@sample.com, phone: 1234567"
my_doc = nlp(my_text)
desired_matches = matcher(my_doc)

for match_id, start, end in desired_matches:
    string_id = nlp.vocab.strings[match_id]
    span = my_doc[start:end]
    print(span.text)
1 Answers

First of all you have a problem with the formatting of the patterns. The format has to be a list of patterns and a pattern is a list of dicts.
Following your current patterns, you need to change this :
my_pattern = [{"LOWER": "email"}, {"LOWER": "phone"}]

to this:

my_pattern = [[{"LOWER": "email"}], [{"LOWER": "phone"}]]

But I believe you have a problem because you said in the original post that you want to extract info such as email and phone number but your current only extracts the words email and phone. However, you can use the spacy token matcher to autmatically extract these infos easily using the following patterns:

my_pattern = [[{'LIKE_EMAIL': True}], [{'LIKE_NUM': True}]]

Now, if you change that line, your code will look like:

nlp = spacy.load("en_core_web_sm")
from spacy.matcher import Matcher
matcher = Matcher(nlp.vocab)

my_pattern = [[{'LIKE_EMAIL': True}], [{'LIKE_NUM': True}]]
matcher.add('MyPattern',my_pattern)
my_text = "email: kashif.jilani@sample.com, phone: 1234567"
my_doc = nlp(my_text)
desired_matches = matcher(my_doc)

for match_id, start, end in desired_matches:
    string_id = nlp.vocab.strings[match_id]
    span = my_doc[start:end]
    print(span.text)

# output:
# kashif.jilani@sample.com
# 1234567

You can learn more about rule based matching over here: Rule based Matching

Related