How to use Composite Entity

I am trying to use composite entity by using 2 sub entities firstName and lastName, but the bot is not evaluating their value. The error message and debug information is also not enough useful to debug it further.

Could you please send me a link which has a concrete example of composite entity. I have checked the sample bot on cloud and documentation example on below link but still not much helpful:

We are validating the use case that you have mentioned in the Zendesk ticket (using sub entities as string) . We will share the further update accordingly.

Bear in mind that string entities are greedy in what they grab as their value unless you are explicit in where they should end.
Two string entities in a composite entity are going to “compete” for the same words.

1 Like

Thanks @andy.heydon for you reply.

Actually in the composite entity I have set the pattern as ‘my first name is @firstName and last name is @lastName

And if I provide the bot input as ‘my first name is himanshu and last name is goel’. For this input also bot is getting confused and not working correctly.

We are having some scenarios where we want to search records based on different parameters, some of the parameters are of string types’

Himanshu,

That pattern does not quite how the composite entity works. Let me describe how it does and what you can do to fix your test case.

When a composite entity is reached in the dialog flow then all of the subentities mentioned in any of the composite entities patterns are identified. It is quite possible for different composite entity patterns to contain different combinations of subentities, and in different orders. Each composite entity pattern describes a relationship (in terms of word order) between the subentities, and as patterns are evaluated in order then you might want to relax the need for some of the subentities if the full set are not found.

Once we have the complete set of subentities then we final all possible instances of those subentities, independently of what the composite entity patterns look like, and we record where those are.

Then we go back to the composite entity patterns and evaluate each one to identify the specific set of subentity instances that match that specific pattern until we get a match.

What that means is that the composite entity pattern is not used to identify the subentities. So in your example “my first name is @firstName and last name is @lastName” is not used to find each of those subentities, which means that each of the subentities will match the entire string, and that is why the composite entity is empty in your scenario.

(The reason for not using the composite entity pattern in the subentity identification is that patterns can be far more complex than just a sequence of words.)

So to fix your test case, then do these things:

  1. On the firstName entity, add the pattern: first name is * [ and , > ]

  2. On the lastName entity, add the pattern: last name is * [ and , > ]

  3. Change the composite entity pattern to: << @firstName @lastName >>

Now there’s all kinds of special syntax in there that helps to reduce the number of patterns, and you could have other “simpler” patterns that deal with each scenario individually, but let me explain them.

First adding a pattern to a subentity, and strings especially because they can match anything and everything, helps to define where the value starts and stops. So “first name is”, as you know, will say that the entity value will start after these words.

The piece enclosed in square brackets means that one of the “tokens” inside the brackets has to be present for this pattern to match - a mandatory choice. This provides an end point for the string, and those endpoints are either the word “and”, a comma, or the end of the sentence - “>” means sentence end.

Without the mandatory choice then the default end point will be the end of the sentence, so you would never need to write “last name is * >” - the “>” is implied. But when you have a choice after the asterisk then you have to include the sentence end as a viable option (and as the last item in the choice because there is an ordering to the choices).

Using patterns like this for the subentities means that those names can be supplied in any order, e.g. “my last name is Goel, my first name is Himanshu”, and also allows for additional words to be placed before, in between or after each of those phrases.

The composite entity pattern is enclosed in double angle brackets, << and >>, and those mean match all of the enclosed “tokens” but in any order and with any number of words between them. The composite entity doesn’t care about how we determined a firstName and a lastName, just that both of them have been found in the sentence. You can choose to not have the brackets, in which case the firstName must be strictly before the lastName, but being too restrictive means that some utterances will not match.

One final point about patterns is that the platform actually tests each pattern several times with an increasing number of wildcards between each token. So we start off seeing if the firstName is directly followed by lastName, then we try with 1 extra word between them, then 2 extra words. This is why in this case I am suggesting to enclose the subentities in double angle brackets because you could say “my first name is Himanshu and then my last name is Goel” - there are a lot of words between Himanshu and Goel. You can easily conceive of other variations with even more words between them.

4 Likes

Thanks @andy.heydon for a detailed answer. This makes sense to me.

Actually I referred the documentation while creating this composite entity and tried to simulate the same approach while using the string type of sub-entities.

If possible could you please provide me some more links which I can refer to read and understand of inner working of these scenarios in much more details, any books or videos references will also work. Maybe you can include links of these additional resources as a further readings in some references section in the documentation itself.

@himanshu.goel.mca I don’t have any more links, which is why I created the detailed answer.

No problem @andy.heydon and Thanks again, I really appreciate for that detailed answer it cleared many doubts of mine.