entity
This block captures an NLP entity regardless of the incoming message intention.
The NLP engine must be enabled and configured in
bot/config/nlp.rb
file in order to implement this block.Platform | Supported |
---|---|
Messenger | |
WhatsApp | |
Telegram |
For this example, we will use the entity
wit$datetime:datetime
(Wit.ai Built-In)And suppose the following message arrives:
"tomorrow at 7am"
As you can see in this message, there is no clear intention. But implementing this block, where we send a reply with two options, we can try to understand the message intention.
class MainContext < Conversation
def blocks
entity "wit$datetime:datetime" do |values|
datetime = values.first[:value]
@reply.quick_reply(
"What do you want me to alert at #{datetime}?",
[
{
title: "Set Alarm",
payload: set_payload("alarms/new")
},
{
title: "Set a Reminder",
payload: set_payload("reminders/new")
}
]
)
end
end
end
To further understand how entities work, we will create in Wit.ai an entity called
colors
where we will train the NLP engine with various color options.The entity can be created and trained directly from the Wit.ai Dashboard or via API for which Kogno has a method.
On the project's directory, open the console by running the
kogno c
command and then execute the following:nlp = Kogno::Nlp.new
nlp.wit.entity_create(
{
name: "color",
roles: ["name"],
lookups: ["keywords"],
keywords:[
{keyword: "red", synonyms: ["Red"]},
{keyword: "orange", synonyms: ["Orange"]},
{keyword: "Yellow", synonyms: ["Yellow"]},
{keyword: "Green", synonyms: ["Green"]},
{keyword: "Cyan", synonyms: ["Cyan"]},
{keyword: "Blue", synonyms: ["Blue"]},
{keyword: "Magenta", synonyms: ["Magenta"]},
{keyword: "Purple", synonyms: ["Purple"]},
{keyword: "White", synonyms: ["White"]},
{keyword: "Black", synonyms: ["Black"]},
{keyword: "Gray", synonyms: ["Gray","Gray"]},
{keyword: "Silver", synonyms: ["Silver"]},
{keyword: "Pink", synonyms: ["Pink"]},
{keyword: "Maroon", synonyms: ["Maroon"]},
{keyword: "Brown", synonyms: ["Brown"]},
{keyword: "Beige", synonyms: ["Beige"]},
{keyword: "Tan", synonyms: ["Tan"]},
{keyword: "Peach", synonyms: ["Peach"]},
{keyword: "Lime", synonyms: ["Lime"]},
{keyword: "Olive", synonyms: ["Olive"]},
{keyword: "Turquoise", synonyms: ["Turquoise"]}
]
}
)
This will create the
color
entity with the role name
. In the following example,
MainContext
will catch any incoming messages such as: "I want a blue t-shirt", "grass is green" and so on.class MainContext < Conversation
def blocks
entity "color:name" do |values|
color = values.first[:value]
@reply.text "You've said the color #{color}!"
end
end
end
Last modified 6mo ago