Skip to content

Instantly share code, notes, and snippets.

@b3tts32
Created August 5, 2020 22:57
Show Gist options
  • Save b3tts32/6a5097570c8275258b8815366d43661a to your computer and use it in GitHub Desktop.
Save b3tts32/6a5097570c8275258b8815366d43661a to your computer and use it in GitHub Desktop.
const { NlpManager } = require('node-nlp');
const manager = new NlpManager({ languages: ['en','nl'], nlu: { useNoneFeature: false, log: false } });
async function mainExtractEntities() {
const result = await manager.extractEntities('en', 'Are you able to identify that [email protected] is an email and [email protected] is another email so there are 2 emails in 20 minutes?');
console.log(result.entities);
}
async function mainFullExample() {
manager.addDocument('en', 'My mail is %email%', 'email');
manager.addDocument('en', 'My email is %email%', 'email');
manager.addDocument('en', 'Here you have my email: %email%', 'email');
manager.addDocument('en', 'Hello', 'greet');
manager.addDocument('en', 'Good morning', 'greet');
manager.addDocument('en', 'good afternoon', 'greet');
manager.addDocument('en', 'good evening', 'greet');
manager.addAnswer('en', 'email', 'Your email is {{email}}');
manager.addAnswer('en', 'greet', 'Hi!');
await manager.train();
let result = await manager.process('en', 'I think that my mail is [email protected]');
console.log(result);
result = await manager.process('en', 'Hello bot!');
// console.log(result);
}
async function getStockEntities(){
manager.addNamedEntityText("direction", "top", ["en"], ["highest gaining", "most rise", "most made"]);
manager.addNamedEntityText("direction", "bottom", ["en"], ["highest losing", "highest loss", "most lost", "most loss"]);
manager.addDocument("en", "Give me the %quantity% %direction% stocks %date%", "Order");
manager.addDocument("en", "Give me the stocks %quantity% %direction% %date%", "Order");
manager.addDocument("en", "Show me the stocks %quantity% %direction% %date%", "Order");
manager.addDocument("en", "Let me see the stocks %quantity% %direction% %date%", "Order");
await manager.train();
let result = await manager.process('Give me the top 20 highest losing stocks last sunday');
console.log(result.entities[2]);
// if(result.intent == "Order"){
// let res = await manager.extractEntities('en', 'Give me the top 10 highest gaining stocks before April 1');
// console.log(res.entities[2].resolution);
// }
//
}
async function main() {
// const manager = new NlpManager({
// languages: ['en', 'nl'],
// nlu: { useNoneFeature: false, log: false },
// });
manager.addNamedEntityText("size", "grande", ["en", "nl"], ["Grande", "Large", "Triple", "Grote", "Groot"]);
manager.addNamedEntityText("size", "short", ["en", "nl"], ["Short", "Small", "Single", "Kleine", "Klein"]);
manager.addNamedEntityText("size", "tall", ["en", "nl"], ["Tall", "Medium", "Double"]);
manager.addNamedEntityText("drink", "caffeAmericano", ["en", "nl"], ["Caffè Americano", "Caffè Americanos", "Americano", "Americanos"]);
manager.addNamedEntityText("drink", "caffèLatte", ["en", "nl"], ["Caffè Latte", "Caffè Lattes", "Latte", "Lattes"]);
manager.addNamedEntityText("drink", "caffèMocha", ["en", "nl"], ["Caffè Mocha", "Caffè Mochas", "Mocha", "Mochas"]);
manager.addNamedEntityText("drink", "cappuccino", ["en", "nl"], ["Cappuccino", "Cappuccinos"]);
manager.addNamedEntityText("drink", "caramelMacchiato", ["en", "nl"], ["Caramel Macchiato", "Caramel Macchiatos"]);
manager.addNamedEntityText("drink", "espresso", ["en", "nl"], ["Espresso", "Espressos"]);
manager.addNamedEntityText("drink", "espressoConPanna", ["en", "nl"], ["Espresso Con Panna", "Espressos Con Panna"]);
manager.addNamedEntityText("drink", "espressoMacchiato", ["en", "nl"], ["Espresso Macchiato", "Espresso Macchiatos", "Macchiato", "Macchiatos"]);
manager.addNamedEntityText("drink", "whiteChocolateMocha", ["en", "nl"], ["White Chocolate Mocha", "White Chocolate Mochas"]);
manager.addDocument("en", "%drink%.", "Order");
manager.addDocument("en", "%size% %drink%.", "Order");
manager.addDocument("en", "And one %drink%.", "Order");
manager.addDocument("en", "And one %size% %drink%.", "Order");
manager.addDocument("en", "Can I get a %drink% please?", "Order");
manager.addDocument("en", "Can I get a %size% %drink% please?", "Order");
manager.addDocument("en", "Can I order a %drink% please?", "Order");
manager.addDocument("en", "Can I order a %size% %drink% please?", "Order");
manager.addDocument("en", "Finally one %drink%.", "Order");
manager.addDocument("en", "Finally one %size% %drink%.", "Order");
manager.addDocument("en", "Give me a %drink%.", "Order");
manager.addDocument("en", "Give me a %size% %drink%.", "Order");
manager.addDocument("en", "I want a %drink%.", "Order");
manager.addDocument("en", "I want a %size% %drink%.", "Order");
manager.addDocument("en", "Lastly one %drink%.", "Order");
manager.addDocument("en", "Lastly one %size% %drink%.", "Order");
manager.addDocument("en", "One %drink%.", "Order");
manager.addDocument("en", "One %size% %drink%.", "Order");
await manager.train();
const result = await manager.process('Give me a White Chocolate Mocha');
console.log(result);
}
// mainExtractEntities();
// mainFullExample();
getStockEntities();
// main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment