node.js - 在 Node.js 上训练分类器(自然 - NLP)以查找意外句子

标签 node.js nlp classification

一些上下文:Node.js、Bot、natural module .

我想构建一个机器人,并且我正在使用自然模块来解析用户输入并对其进行总体分类。

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);

这些测试工作正常:

  "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 } ]

这个示例不是一个很好的示例,但您可以看到它与任何一个标签都不能很好地匹配。 越高,置信度越高。

您可以检查它的值以确保它高于特定水平。我喜欢使用 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 的意图数组,按置信度分数降序排序。

更多信息请访问https://github.com/NaturalNode/natural#classifiers
排序功能的功劳Sort array of objects by string property value in JavaScript

关于node.js - 在 Node.js 上训练分类器(自然 - NLP)以查找意外句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37242999/

相关文章:

nlp - 是否有一个相当准确的启发式方法来检测英语句子的主语和宾语?

python-3.x - h2o 与 scikit 学习混淆矩阵

node.js - 当我运行 npm start 时,未创建我的主输出文件

node.js - 自动浏览器不提交表单

node.js - 如何下载 AWS 证书以将其与 NodeJS 一起使用

比较想法相似性的算法(作为字符串)

javascript - 如何在从外部连接时将外部文件包含到 Node js 项目中

python - 如何根据 python 和 NLPK 中 CSV 文件的训练数据预测位置

algorithm - 遗传算法与matlab如何使用分类精度作为适应度函数

python - 具有交叉熵损失的 Softmax 激活导致两个类别的输出分别准确地收敛于 0 和 1