Search⌘ K

Solution: Semantic Parsing with spaCy

Explore how to apply semantic parsing with spaCy to create effective chatbot applications. This lesson guides you through defining intents, using pattern matching to recognize user inputs, and extracting meaningful intents from messages. Learn step-by-step methods to build and test a semantic parser that can understand diverse user requests.

We'll cover the following...

Solution

Here is the solution to the previous exercise:

Python 3.5
import spacy
from spacy.matcher import Matcher
from collections import defaultdict
# Define intents and their corresponding patterns purchase a vehicle
intents = {
"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 intent
synonyms = {
"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 matcher
nlp = spacy.load("en_core_web_md")
matcher = Matcher(nlp.vocab)
# Add patterns for each intent to the matcher
matcher.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 message
def 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] += 1
for intent, synonyms_list in synonyms.items():
for synonym in synonyms_list:
if nlp(synonym).similarity(doc) >= 0.8:
intents[intent] += 0.5
return intents
messages = [
"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 ...