我试图用ML5 sentiment api评估一本书的情绪:
const sentiment = ml5.sentiment('movieReviews', modelReady)
// When the model is loaded
function modelReady() {
// model is ready
console.log("Model Loaded!");
}
// make the prediction
fetch('../../data/brothers.txt')
.then(response => response.text())
.then(text => {
const prediction = sentiment.predict(text)
console.log(prediction)
createDiv('predicted sentiment - ' + prediction)
})
function setup() {
}text很好地加载,我可以将它打印到控制台。以下错误发生在使用predict方法的行中:

如果我用一个单词替换text,错误将保持不变。那么,我在这里做错了什么,怎么做呢?
发布于 2020-10-03 23:45:28
它不能工作,因为函数在加载模型之前被称为(正如@dwosk所指出的那样)。对predict的调用必须在模型准备好后完成。
换句话说,函数必须在A而不是B调用
function modelReady() {
console.log("Model Loaded!");
[A: THIS LOADS WHEN THE MODEL IS READY]
}
[B: THIS LOADS BEFORE THE MODEL IS READY]下面是一个有用的示例:https://codepen.io/adelriosantiago/pen/RwaXjez?editors=1011
请注意,为了简单起见,对fetch函数进行了模拟,以返回“一只美丽的快乐猫”。
https://stackoverflow.com/questions/64189212
复制相似问题