Solution: Semantic Parsing with spaCy
Let's look at the solution to the previous exercise.
We'll cover the following...
Solution
Here is the solution to the previous exercise:
Press + to interact
import spacyfrom spacy.matcher import Matcherfrom collections import defaultdict# Define intents and their corresponding patterns purchase a vehicleintents = {"greeting": [{"LOWER": "hi"},{"LOWER": "there"}]}buy_car = [[{"LOWER": "buy"},{"POS": "DET"} , {"LOWER": "car"}],[{"LOWER": "purchase"}, {"POS": "DET"} ,{"LOWER": "vehicle"}]]book_flight = [[{"LOWER": "reserve"}, {"POS": "DET"},{"LOWER": "flight"}],[{"LOWER": "book"},{"POS": "DET"},{"LOWER": "flight"}]]# Define synonyms for each intentsynonyms = {"buy_car": ["purchase car", "get a car", "buy vehicle"],"book_flight": ["reserve flight", "book airfare", "buy plane ticket"],"greeting":["Hi", "hi"]}# Initialize Spacy language model and matchernlp = spacy.load("en_core_web_md")matcher = Matcher(nlp.vocab)# Add patterns for each intent to the matchermatcher.add("buy_car",buy_car)matcher.add("book_flight",book_flight)for intent, patterns in intents.items():matcher.add(intent,[patterns])# Define function to extract intents from user messagedef extract_intents(message):doc = nlp(message)matches = matcher(doc)intents = defaultdict(float)for match_id, start, end in matches:intent = nlp.vocab.strings[match_id]intents[intent] += 1for intent, synonyms_list in synonyms.items():for synonym in synonyms_list:if nlp(synonym).similarity(doc) >= 0.8:intents[intent] += 0.5return intentsmessages = ["Hi there","I want to buy a car","Can you help me book a flight?","I want to purchase a vehicle","I'm looking reserve a flight"]for message in messages:intents = extract_intents(message)print("Message: ", message)print("Intents: ", intents)
Solution explanation
Lines 1–3: Import the necessary packages for the chatbot. spacy
is the main package for natural language processing and Matcher
is a class within spacy.matcher
that allows for pattern matching in text. defaultdict
is a class within collections that creates a dictionary with a default value of zero for any ...