Named entities
A named entity is a word or a phrase that distinguishes an object or a phenomenon among other objects or phenomena of a similar type. These can be names of cities, countries, currencies.
NLU rules can include named entities just like any other named pattern, and so recognize named entity dictionary entries in user requests.
Declaration
Follow the following steps to declare a pattern as a named entity. We shall be using a fragment of a cities dictionary for this example, placed into dicts/cities.csv.
1;New York City, New York, NY;{"name": "New York", "lat": 40.71427, "lon": -74.00597}
-
Import the named entity dictionary into the
.scscript file via therequiretag:require: dicts/cities.csv
name = Cities
var = Cities -
Define an entity converter in one of the
.jsfiles orinitblocks:$global.cityConverter = function($parseTree) {
var id = $parseTree.Cities[0].value;
return Cities[id].value;
};tipUsing converters is optional, but it simplifies accessing values associated with the entities in the dictionary. -
Declare a named pattern using a special
$entitypattern element. Pass the named entity dictionary name in angle brackets and the converter name as a parameter after||:patterns:
$city = $entity<Cities> || converter = cityConvertercautionIf a pattern is created with
$entity<>andconverter, its name must not match the name of an NLU core entity. For example, if a project has an$Examplepattern and anExampleentity, it might cause errors during the script execution.
How to use
Consider the following state that will trigger on requests with the $city entity mentioned:
state: City
q!: * $city *
On an example request I come from New York, information on this entity will be contained in the $parseTree:
{
"city": [
{
// Supplementary keys: tag, pattern, etc.
"value": {
"name": "New York",
"lat": 40.71427,
"lon": -74.00597
}
}
],
"_city": {
"name": "New York",
"lat": 40.71427,
"lon": -74.00597
}
}
<pattern_name> key contains detailed information on all entities recognized in the request, while the _<pattern_name> contains only the value of the first such entity.The value differs based on whether or not a converter is defined for the entity:
- If a converter is present, the value it returns is saved in
$parseTree. - If there is no converter, only the entity ID is saved.