DEV Community

Cover image for RASA - Synonyms
Petr Janik
Petr Janik

Posted on

RASA - Synonyms

Synonyms map extracted entities to a value other than the literal text extracted. You can use synonyms when there are multiple ways users refer to the same thing. Think of the end goal of extracting an entity and from there, figure out which values should be considered equivalent.

We have a frequency entity.

# domain.yml
# ... previous content ...
entities:
  # ... other entities ...
  - frequency
Enter fullscreen mode Exit fullscreen mode

This entity is used to fill the frequency slot that has two possible values: once a week and twice a week.

# domain.yml
# ... previous content ...
slots:
  # ... other slots ...
  frequency:
    type: categorical
    values:
      - once a week
      - twice a week
    influence_conversation: true
Enter fullscreen mode Exit fullscreen mode

The slot is filled during the newsletter_form.

# domain.yml
# ... previous content ...
forms:
  newsletter_form:
    # ... other slots ...
    frequency:
      - type: from_entity
        entity: frequency
Enter fullscreen mode Exit fullscreen mode

Users might also refer to the once a week value as once or once per week

In this case, we will define once and once per week as synonyms to once a week:
Then, if either of these phrases is extracted as an entity, it will be mapped to the value once a week.

There are two ways of defining synonyms:

First option

Create a synonym key and list the mapped synonyms.

# data/nlu.yml
# ... previous content ...
  - intent: inform_frequency
    examples: |
      # ... other examples ...
      - [once a week](frequency)
      - [once](frequency)
      - [once per week](frequency)

  - synonym: once a week
    examples: |
      - once
      - once per week
Enter fullscreen mode Exit fullscreen mode

Once per week changed to once a week

We can see, that even though user entered "once per week", the extracted entity was "once a week".

Second option

We can also define synonyms in-line in our training examples by specifying the value of the entity:

# data/nlu.yml
# ... previous content ...
  - intent: inform_frequency
    examples: |
      # ... other examples ...
      - [twice a week](frequency)
      - [twice]{"entity": "frequency", "value": "twice a week"}
      - [twice per week]{"entity": "frequency", "value": "twice a week"}
Enter fullscreen mode Exit fullscreen mode

Twice changed to twice a week

We can see, that even though user entered "twice", the extracted entity was "twice a week".

Since frequency is now an entity, we need to annotate it in the test stories.

You can learn more about synonyms in the documentation here and here.

Repository for this tutorial:

You can checkout the state of the repository at the end of this tutorial by running:

git clone --branch 20-synonyms git@github.com:petr7555/rasa-dev-tutorial.git
Enter fullscreen mode Exit fullscreen mode

Top comments (0)