在node.js上训练分类器(自然 – NLP),用于意外的句子
一些上下文:Node.js,Bot, 自然模块 。
我想build立一个Bot,我正在使用自然模块来parsing和整体分类用户input。
var classifier = new natural.BayesClassifier(); classifier.addDocument('Hi', 'welcome'); classifier.addDocument('Hello', 'welcome'); classifier.addDocument('Hey', 'welcome'); classifier.addDocument('Good', 'welcome'); ... //back to home classifier.addDocument('go back to home', 'back2home'); classifier.addDocument('go back home', 'back2home'); classifier.addDocument('return', 'back2home'); classifier.addDocument('return to home', 'back2home'); ... classifier.train(); ... classifier.classify(text);
那些testing工作正常:
"I would like to go back home" => back2home "Hi" => welcome
一切都很好,但如果用户文本包含诸如“bla bla bla”之类的东西,我想要知道文本在上述任何情况下都不够用。 “bla bla bla”返回我=>欢迎,但实际上我想它返回这样的“未知”/不明白。
这是一种“训练”分类器的方式吗? 谢谢。
您可以使用getClassifications()
方法获取分类列表以及关联分数或“置信度”。 从该列表中,您可以确定哪个(如果有)最匹配。 例如:
console.log(classifier.getClassifications('blah blah blah'));
输出:
[ { label: 'welcome', value: 0.5 }, { label: 'back2home', value: 0.5 } ]
这个例子不是一个好的例子,但是你可以看到它和任何一个标签都不匹配。 value
越高,信心越高。
你可以检查它的价值,以确保它在一定的水平之上。 我喜欢使用0.8
作为我的检查值。 循环播放结果。
const results = classifier.getClassifications('blah blah blah'); let intents = []; // Check for confidence greater than 8 results.forEach((result) => { if(result.value > 0.8) { intents.push(result); } }); // Sort intents array by object.value intents.sort((a,b) => { if(a.value < b.value) { return -1; } if(a.value > b.value) { return 1; } return 0; });
你现在有一系列的置信度大于0.8的intents
,按照他们的置信度sorting。
更多信息在https://github.com/NaturalNode/natural#classifiers
用于sorting函数的函数通过JavaScript中的string属性值sorting对象的数组