From e18c16d04cf62f0cf21031ca652e3b7962ccf999 Mon Sep 17 00:00:00 2001 From: Ashish Agre Date: Sun, 23 Jul 2023 10:54:19 +0530 Subject: [PATCH] Create generic pattern sample Writing two patterns with NN for morning/evening --- Chapter04/match_generic_greetings.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Chapter04/match_generic_greetings.py diff --git a/Chapter04/match_generic_greetings.py b/Chapter04/match_generic_greetings.py new file mode 100644 index 0000000..05e1e83 --- /dev/null +++ b/Chapter04/match_generic_greetings.py @@ -0,0 +1,20 @@ +import spacy +from spacy.matcher import Matcher + +nlp = spacy.load("en_core_web_md") +doc = nlp("Good morning, I want to reserve a ticket. I will then say good evening!") + +generic_pattern = [ + {"LOWER": "good"}, + {"TAG": "NN"}, + {"IS_PUNCT": True}, +] + +matcher = Matcher(nlp.vocab) +matcher.add("generic-pattern", [generic_pattern]) + +matches = matcher(doc) + +for match_id, start, end in matches: + m_span = doc[start:end] + print(start, end, m_span)