Skip to content

Latest commit

 

History

History
143 lines (117 loc) · 4.05 KB

ner-quickstart.md

File metadata and controls

143 lines (117 loc) · 4.05 KB

NER Quick Start

Install the needed packages

At the folder where is your node project, install the @nlpjs/basic, @nlpjs/express-api-server and @nlpjs/directline-connector packages.

npm i @nlpjs/basic @nlpjs/express-api-server @nlpjs/directline-connector

Create the conf.json

Create the file conf.json with this content:

{
  "settings": {
    "nlp": {
      "corpora": ["./corpus.json"]
    },
    "api-server": {
      "port": 3000,
      "serveBot": true      
    }
  },
  "use": ["Basic", "LangEn", "ExpressApiServer", "DirectlineConnector"]
}

You'll telling the applicaition to use 4 plugins:

  • Basic: the basic plugins for an NLP backend, that includes evaluator, javascript compiler, logger, and NLP classes
  • LangEn: the plugin to use english language
  • ExpressApiServer: the plugin to have an API server done with express
  • DirectlineConnector: the plugin that uses the ExpressApiServer to serve an API for the chatbot

Also this configure the ExpressApiServer to be exposed in the port 3000 and to serve the chatbot frontend (serveBot: true). Finally, it tells the NLP to import the corpus defined in the file corpus.json.

Create the corpus.json

Add the file corpus.json with this content:

{
  "name": "Corpus with entities",
  "locale": "en-US",
  "contextData": "./heros.json",
  "data": [
    {
      "intent": "hero.realname",
      "utterances": [
        "what is the real name of @hero"
      ],
      "answers": [
        "The real name of {{ hero }} is {{ _data[entities.hero.option].realName }}"
      ]
    },
    {
      "intent": "hero.city",
      "utterances": [
        "where @hero lives?",
        "what's the city of @hero?"
      ],
      "answers": [
        "{{ hero }} lives at {{ _data[entities.hero.option].city }}"
      ]
    }
  ],
  "entities": {
    "hero": {
      "options": {
        "spiderman": ["spiderman", "spider-man"],
        "ironman": ["ironman", "iron-man"],
        "thor": ["thor"]
      }
    }
  }
}

This creates 2 intents: one to know the real name of a hero and other one to know where the hero lives. Also creates the entity to recognize thre heros: spiderman, ironman and thor, and also their synonyms. There is a part in the json to tell the NLP to load some contextData that will be used to generate the answers:

  "contextData": "./heros.json",

If you take a look at one answer, _data[entities.hero.option].city as an example, the content at the json heros.json will be accesible at the context as data. Also, the entities are accesible at the property entities, so as the entity name is hero you'll have the result from the NER for the entity hero stored at entities.hero

Create the heros.json

Create the file heros.json with this content:

{
  "spiderman": {
    "realName": "Peter Parker",
    "city": "Queens, New York"
  },
  "ironman": {
    "realName": "Tony Stark",
    "city": "Stark Tower, New York"
  },
  "thor": {
    "realName": "Odinson",
    "city": "Asgard"
  }
}

Create the index.js

Create the file index.js with this content:

const { dockStart } = require('@nlpjs/basic');

(async () => {
  const dock = await dockStart();
  const nlp = dock.get('nlp');
  await nlp.train();
})();

This initializes the project and load all the jsons building the structure when you call dockStart() and returns to you a dock for the containers. Then you can retrieve instances from the container, in this case we retrieve the nlp instance to train it.

Start the application

You can start your application running:

node index.js

Then you can navigate to http://localhost:3000 to use it.

Stored context

You'll see that you can ask for information of a hero, but also that if you're talking with the bot about a hero then you can omit the reference to the hero you're talking about. This context is stored per conversation, so different conversations have its own context variables.