Skip to content

Instantly share code, notes, and snippets.

@MarketingPip
Created July 5, 2023 18:51
Show Gist options
  • Save MarketingPip/fe41c37b3038331934ddf133997c27d9 to your computer and use it in GitHub Desktop.
Save MarketingPip/fe41c37b3038331934ddf133997c27d9 to your computer and use it in GitHub Desktop.
Using NLP.js to extract some entities
/// Published to help someone out with this task! If they are reading this - hope this helped you! :)
// - ps check out @ https://github.com/MarketingPipeline and feel free to star something! :D
import { containerBootstrap } from "https://cdn.skypack.dev/@nlpjs/core@4.26.1";
import { Nlp } from "https://cdn.skypack.dev/@nlpjs/nlp@4.26.1";
import { LangEn } from "https://cdn.skypack.dev/@nlpjs/lang-en-min@4.26.1";
(async () => {
const container = await containerBootstrap();
container.use(Nlp);
container.use(LangEn);
const nlp = container.get('nlp');
nlp.settings.autoSave = false;
nlp.addLanguage('en');
nlp.addDocument('en', 'I want a @subject from @place with @extra', 'hungry')
nlp.addNerBetweenLastCondition('en', 'subject', 'a', 'from');
nlp.addNerBetweenLastCondition('en', 'place', 'from', 'with');
nlp.addNerAfterLastCondition('en', 'extra', 'with');
await nlp.train();
const response = await nlp.process('en', 'I want a coffee from Dunkin Donuts with sugar');
console.log({
subject:response.entities[0].utteranceText,
place:response.entities[1].utteranceText,
extras:response.entities[2].utteranceText
});
/* Outputs:
{
"subject": "coffee",
"place": "Dunkin Donuts",
"extras": "sugar"
}
*/
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment