Thierry Templier
Thierry Templier

Reputation: 202346

How to extract variables from sentences with node-nlp

I'm trying to extract variable(s) from sentences with node-nlp using the following code:

const { NlpManager, ConversationContext } = require('node-nlp');

const manager = new NlpManager({ languages: ['en'] });
const context = new ConversationContext();

(async () => {
  manager.addDocument('en', 'Hello my name is %name%', 'greeting.hello');
  manager.addDocument('en', 'I have to go', 'greeting.bye');
  manager.addAnswer('en', 'greeting.hello', 'Hey there!');
  manager.addAnswer('en', 'greeting.bye', 'Till next time, {{name}}!');

  manager.train();

  const result1 = await manager.process('en', 'Hello my name is Thierry', context);
  console.log(result1);
  console.log(context);

  const result2 = await manager.process('en', 'I have to go', context);
  console.log(result2);
})();

The context doesn't contain a name variable...

I opened an issue directly on the github project page but the answer suggested to add this:

manager.addNamedEntityText(
  'name',
  'John',
  ['en'],
  ['john', 'John'], 
);

But it's not exactly what I want because a variable name is added only if there is either "John" or "john" for the name in the sentence.

I also saw in this issue https://github.com/axa-group/nlp.js/issues/133#issuecomment-503223171 what there are restrictions on variable names.

Thanks for your help! Thierry

Upvotes: 1

Views: 1825

Answers (1)

jseijas
jseijas

Reputation: 134

Is an error in the documentation. This can be implemented, but the problem is the "limitations" or the misanderstanding of how it will work. I mean, it can be implemented that way: when training, it can detect entities present in the utterances that are not in the NER, then detect if the entities are the last part of a sentence, and then build a new Trim entity based on the words before the entity. So given "hello my name is %name%" it can resolve that given this intent, it should try to search the word "is" and the things after can be the entity.

But now then are problems, and are the reasons why is not implemented: - Someone can write "Hello my name is John and I want a beer", then the chatbot can extract the entity and think that the name is "John and I want a beer". - If to avoid that you think on extracting only one word, then "Hello my name is Paul Henri" will think that the name is "Paul". - If it's searching by the word "is" then "Hello today is a wonderfull day and my name is John" will think that the name is "a wonderfull day and my name is John".

So to achieve what you want I recommend you to take a look here: https://github.com/axa-group/nlp.js/blob/master/docs/ner-manager.md#trim-named-entities This tell you how to implement trim named entities based on the position to another words.

By the way, I'll fix the error in the docs.

Upvotes: 3

Related Questions