nlp.js icon indicating copy to clipboard operation
nlp.js copied to clipboard

Unable to recreate context behavior shown in the doc .

Open Nahdus opened this issue 5 years ago • 6 comments

The context behavior doesn't seem to work the way its described in the documentation

below code is given as example for inplementing context in the Doc

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

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

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()
  .then(result => manager.process('en', 'Hello my name is John', context))
  .then(result => manager.process('en', 'I have to go', context))
  .then(result => console.log(result.answer));

running this code results output:

Till next time, !

The behavior expected as per the doc is

Till next time, john !

even though the doc points out it to be See you soon, John! which could be a typo

I can achieve the expected behavior by adding named entity text

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

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


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.addNamedEntityText(
  'name',
  'John',
  ['en'],
  ['john', 'John'], 
  );


manager.train()
  .then(async result => {
    r = await manager.process('en', 'Hello my name is John', context)
    console.log(context)
    return r
  })
  .then(async result => await manager.process('en', 'I have to go', context)
    )
  .then(result => {
       console.log(result.answer)
  })

If this is how context is expected to be set up it not very clear in the doc, and it is not very useful for contextual use of name at least in this case. since it will only work if the name is John.

Nahdus avatar Dec 19 '18 10:12 Nahdus

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

This statement just let the intent search a keyword within the sentence, and even if the intent doesn't match, it will still be available as {{name}}.

faapio avatar Mar 15 '19 07:03 faapio

Would we then proceed to add each possible name in the world into name? Shouldn't there be a more dynamic solution to this?

tumenor avatar Apr 27 '19 00:04 tumenor

No comment here? Does anybody have a workable solution here?

egeste avatar Jun 18 '19 04:06 egeste

Looks like the context instance doesn't respect the variable name defined in the document. I don't know if this is "correct", but it works...

Here's my current configuration

export default manager => {
  // mint/burn
  manager.addDocument('en', 'mint %amount%', 'economy.mint')
  manager.addDocument('en', 'mint %amount% more currency', 'economy.mint')
  manager.addDocument('en', 'add %amount% currency', 'economy.mint')
  manager.addDocument('en', 'increase the economy by %amount%', 'economy.mint')

  manager.addAnswer('en', 'economy.mint', 'economy mint {{amount}}')
}

Output: economy mint

Looking into the context object, I see this:

ConversationContext { settings: {}, number: '1000', daterange: '1000' }

Replacing amount with number works.

export default manager => {
  // mint/burn
  manager.addDocument('en', 'mint %number%', 'economy.mint')
  manager.addDocument('en', 'mint %number% more currency', 'economy.mint')
  manager.addDocument('en', 'add %number% currency', 'economy.mint')
  manager.addDocument('en', 'increase the economy by %number%', 'economy.mint')

  manager.addAnswer('en', 'economy.mint', 'economy mint {{number}}')
}

Output: economy mint 1000

Digging deeper

This issue appears to be related to the ner manager whitelist with regard to what entities can be identified and extracted.

class NerManager {
  // ...
  constructor(settings) {
    // ...
    let list = this.settings.builtinWhitelist || [
      'age',
      'currency',
      'dimension',
      'temperature',
      'number',
      'numberrange',
      'ordinal',
      'percentage',
      'email',
      'hashtag',
      'ip',
      'mention',
      'phonenumber',
      'url',
      'date',
      'daterange',
      'datetime',
      'datetimealt',
      'time',
      'set',
      'timerange',
      'timezone',
      'boolean',
      'duration',
    ];
    // ...

egeste avatar Jun 18 '19 17:06 egeste

Hello @egeste,

Did you finally find out the solution for this problem? Because I can't make the context feature to work...

See this issue: https://github.com/axa-group/nlp.js/issues/232.

Thanks for your help! Thierry

templth avatar Aug 05 '19 12:08 templth

In fact it is "easy": An entity needs a "rule" on how to detect it - unless it is one of the potentially automatically detected builtin entities as listed in https://github.com/axa-group/nlp.js/issues/133#issuecomment-503223171

Just the "is contained in the text" is not enough

Apollon77 avatar Aug 08 '22 20:08 Apollon77

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

aigloss avatar Nov 25 '22 09:11 aigloss

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

I tried but output is undefined

Nicat-dcw avatar Apr 28 '23 17:04 Nicat-dcw