cancel
Showing results for 
Search instead for 
Did you mean: 

Rename a string with an entity value

0 Kudos

hello everyone, I hope you can help us:

we have a Skill based chatbot and we use enriched entities to capture all the different ways used by the customer to define a product name: for example, for a product like SAP Crystal Reports I have a series of synonyms such as "CR, Crystal, crystal report" etc., but they are all recognized as part of the same enriched value "productname": Crystal Reports, therefore

if the user types "where do I find documents for CR?"

the system recognizes #product.raw = CR + #product.productname = Crystal Reports

I would like to use the SAP CAI Scripting syntax (https://help.sap.com/viewer/a4522a393d2b4643812b7caadfe90c18/latest/en-US/b4f08a9a66434327a405b6934880445c.html) to replace, in the user statement, the raw value with the enriched one, so

if the user types "where do I find documents for CR?"

the JSON script replaces it with "where do I find documents for Crystal Reports?"

I thought this could be accomplished with "replace", so I tried the following syntaxes but none of them works

"{{json (replace nlp.source nlp.entities.product.raw nlp.entities.product.productname)}}"

"{{replace nlp.source #product.raw #product.productname}}"

"{{replace nlp.source "#product.raw" "#product.productname"}}"

Could anyone point me in the right direction?

Accepted Solutions (1)

Accepted Solutions (1)

Dan_Wroblewski
Developer Advocate
Developer Advocate
0 Kudos

The syntax would be like this:

{{replace nlp.source "i" "a"}}

This would take the input hi and replace the i with an a:

If you want to reference the value of an entity, you must reference the memory not #productname:

{{memory.product.raw}}
0 Kudos

daniel.wroblewski

first of all: thanks a mil for taking the time to help us!

What I understand from your answer is that the "replace" function works correctly with simple strings (this was clear from the documentation) or with memory values BUT not directly with entities (this was not clear in the documentation: I'll contact the colleagues), so I need to first put my entity into a memory value and then use the memory value as an operand inside the "replace" statement.

I tried but I'm still doing something incorrectly:

1. I set a memory value PRODUCT with "{{#product}}"

2. I set a second memory value ALTEREDSOURCE where I put the following JSON statement

"{{replace nlp.source memory.PRODUCT.raw memory.PRODUCT.prdname}}"

3. I typed price of CR

BUT when I go to display {{memory.ALTEREDSOURCE}} I still get my original source "price of CR", not the desired new source "price of Crystal Reports": in short, it has not been replaced.

I also tried to encapsulate in inverted commas "{{replace nlp.source "memory.PRODUCT.raw" "memory.PRODUCT.prdname"}}"

Could you kindly help me to understand what I'm doing incorrectly?

Dan_Wroblewski
Developer Advocate
Developer Advocate

First, you cannot use #entity in handlebars helpers -- this is clearly marked in the help https://help.sap.com/viewer/a4522a393d2b4643812b7caadfe90c18/latest/en-US/858ae05dc3f743998ecc7d299e...

Dan_Wroblewski
Developer Advocate
Developer Advocate
0 Kudos

Second, what you wrote should work (the first version, NOT the inverted commas thing), so it depends on the values and where you placed these actions. We can have a look Monday.

0 Kudos

daniel.wroblewski thanks a mil! This is now solved thanks to your input:

1. I set a memory value PRODUCT to capture the entity: unlike what I've done in the past, I couldn't just use the standard way

"{{#product}}" but I had to toggle JSON and remove the ""

2. I set a memory value ALTEREDSTATEMENT and, this time, I did NOT use the JSON toggle but rather the NON JSON option, and set it as follows "{{replace nlp.source memory.PRODUCT.raw memory.PRODUCT.prdname}}"

3. I typed "price of CR"

RESULT: when I go to print {{memory.ALTEREDSTATEMENT}} I can now see "price of Crystal Reports", as desired

Have a great weekend!

Answers (0)