PhraseMatcher
While processing financial, medical, or legal text, often we have long lists and dictionaries and we want to scan the text against our lists. As we saw in the previous section, Matcher patterns are quite handcrafted; we coded each token individually. If you have a long list of phrases, Matcher is not very handy. It's not possible to code all the terms one by one.
spaCy offers a solution for comparing text against long dictionaries – the PhraseMatcher
class. The PhraseMatcher
class helps us match long dictionaries. Let's get started with an example:
import spacy from spacy.matcher import PhraseMatcher nlp = spacy.load("en_core_web_md") matcher = PhraseMatcher(nlp.vocab) terms = ["Angela Merkel", "Donald Trump", "Alexis Tsipras"] patterns = [nlp.make_doc(term) for term in terms] matcher.add("politiciansList", patterns) doc = nlp("3 EU leaders met in Berlin. German chancellor Angela Merkel first...