Skip to content

Latest commit

 

History

History
144 lines (117 loc) · 4.05 KB

ner-quickstart.md

File metadata and controls

144 lines (117 loc) · 4.05 KB

NER Quick Start

Install the needed packages

In your node project folder, 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 are 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 Express API server
  • DirectlineConnector: the plugin that uses the ExpressApiServer to serve an API for the chatbot

Also this configures the ExpressApiServer to be exposed at 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. It also creates the entity to recognize the 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 accessible in the context as data. Also, the entities are accessible in the property entities, so because the entity name is hero you'll have the result from the NER for the entity hero stored in 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 loads all the jsons. It also builds the structure when you call dockStart() and then it returns 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 about 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 their own context variables.