axa-group / nlp.js

An NLP library for building bots, with entity extraction, sentiment analysis, automatic language identify, and so more
MIT License
6.28k stars 621 forks source link

Bug when 2 or more entities are mentionned in text input #816

Closed aRandomKiwi closed 1 year ago

aRandomKiwi commented 3 years ago

Describe the bug When two or more entities are mentioned in a user input, evaluator.js throw an uncatched exception, so there is no output text (easily reproducible with the ner example provided by the NLP.js repository).

To Reproduce Steps to reproduce the behavior:

  1. Setup the example provided by the NLP.js repository at https://github.com/axa-group/nlp.js/blob/0af1a7af4718bce008a52eb1405fc0b61d918789/docs/v4/ner-quickstart.md
  2. Launch the project (npm .) and go to http://localhost:3000
  3. Type "where thor spiderman lives?" in the user input chat (two entities mentionned)
  4. The chatbot do nothing (no response), because there is the following uncatched exception "UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'city' of undefined"
  5. (This issue happen in all cases there is more than one entity mentionned)

Expected behavior NLP.js (evaluator.js) process at least one of the mentioned entities and ignore the others instead of crashing (throwing an exception) and answer nothing.

Desktop (please complete the following information):

jesus-seijas-sp commented 3 years ago

When two entities come, then the entity is not fullfiled with the first one, it is identified as being a list instead:

image

But you can use the conditions at answers to detect:

Example of corpus:

{
  "name": "Corpus with entities",
  "locale": "en-US",
  "contextData": "./heros.json",
  "data": [
    {
      "intent": "hero.realname",
      "utterances": [
        "what is the real name of @hero"
      ],
      "answers": [
        { "answer": "The real name of {{ hero }} is {{ _data[entities.hero.option].realName }}", "opts": "entities.hero !== undefined && !entities.hero.isList" },
        { "answer": "Well, perhaps you can tell me only one hero at a time", "opts": "entities.hero !== undefined && entities.hero.isList" },
        { "answer": "You have to specify a hero", "opts": "entities.hero === undefined" }
      ]
    },
    {
      "intent": "hero.city",
      "utterances": [
        "where @hero lives?",
        "what's the city of @hero?"
      ],
      "answers": [
        { "answer": "{{ hero }} lives at {{ _data[entities.hero.option].city }}", "opts": "entities.hero !== undefined && !entities.hero.isList" },
        { "answer": "Well, perhaps you can tell me only one hero at a time", "opts": "entities.hero !== undefined && entities.hero.isList" },
        { "answer": "You have to specify a hero", "opts": "entities.hero === undefined" }
      ]
    }
  ],
  "entities": {
    "hero": {
      "options": {
        "spiderman": ["spiderman", "spider-man"],
        "ironman": ["ironman", "iron-man"],
        "thor": ["thor"]
      }
    },
    "email": "/\\b(\\w[-._\\w]*\\w@\\w[-._\\w]*\\w\\.\\w{2,3})\\b/gi"
  }
}
gauravgandhi1315 commented 3 years ago

I am seeing the same issue

atubo2012 commented 3 years ago

Hoping for a more convenient builtin solution in latest release.

Maybe "opts": "entities.hero !== undefined && !entities.hero.isList"(and other two ) could be added automatically when trainning or running.

Apollon77 commented 2 years ago

@jesus-seijas-sp I added your example to the new docs I did for v4 slot filling ... see https://github.com/axa-group/nlp.js/pull/1171/files#diff-63e03ba61fe1b78e6dbc539dfb6c81be93d6aff1e19980d9d030fd4162915890

@atubo2012 How you think this could get "more convenient"? You need to define/know what you need when you design the answers ... I think this is nothing that can be generalized

Apollon77 commented 2 years ago

PS: The only question if if we not should just catch the exception and replace nothing in such a case

aigloss commented 1 year ago

Closing due to inactivity. Please, re-open if you think the topic is still alive.