我想完成这个:
我在中间件中有一个拼写检查器。如果用户的输入文本与更正的文本不匹配,我想在继续之前与用户确认更正的文本。我尝试了几种方法,但未能实现这一目标。这是我写的代码:
app.js:
var builder = require('botbuilder');
var restify = require('restify');
var spellService = require('./spell-service')
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create connector and listen for messages
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
server.post('/api/messages', connector.listen());
var bot = new builder.UniversalBot(connector, function (session) {
session.beginDialog('search');
});
bot.dialog('search', [
function (session, args, next) {
builder.Prompts.text(session, 'Type the name of a company you would like to search');
}
]);
//=========================================================
// Spell checker
//=========================================================
const dontCorrectIdentifier = "dontCorrect";
bot.use({
botbuilder: function (session, next) {
if (session.message.text.startsWith(dontCorrectIdentifier)) {
session.message.text = session.message.text.replace(dontCorrectIdentifier, "");
next();
} else {
spellService
.getCorrectedText(session.message.text)
.then(function (text) {
if (session.message.text !== text) {
var msg = new builder.Message(session)
.text("Did you mean \"" + text + "\"?")
.suggestedActions(builder.SuggestedActions.create(session, [
builder.CardAction.postBack(session, dontCorrectIdentifier + text, "Yes"),
builder.CardAction.postBack(session, dontCorrectIdentifier + session.message.text, "No")
]
));
builder.Prompts.text(session, msg);
}
next();
})
.catch(function (error) {
console.error(error);
next();
});
}
}
});
拼写service.js
// from https://dev.cognitive.microsoft.com/docs/services/56e73033cf5ff80c2008c679/operations/56e73036cf5ff81048ee6727
var request = require('request');
var SPELL_CHECK_API_URL = 'https://api.cognitive.microsoft.com/bing/v7.0/SpellCheck?mkt=en-US&mode=proof',
SPELL_CHECK_API_KEY = 'MY KEY';
/**
* Gets the correct spelling for the given text
* @param {string} text The text to be corrected
* @returns {Promise} Promise with corrected text if succeeded, error otherwise.
*/
exports.getCorrectedText = function (text) {
return new Promise(function (resolve, reject) {
if (text) {
var requestData = {
url: SPELL_CHECK_API_URL,
headers: {
"Ocp-Apim-Subscription-Key": SPELL_CHECK_API_KEY
},
form: {
text: text
},
json: true
};
request.post(requestData, function (error, response, body) {
if (error) {
reject(error);
} else if (response.statusCode != 200) {
reject(body);
} else {
var previousOffset = 0;
var result = '';
for (var i = 0; i < body.flaggedTokens.length; i++) {
var element = body.flaggedTokens[i];
// append the text from the previous offset to the current misspelled word offset
result += text.substring(previousOffset, element.offset);
// append the first suggested correction instead of the misspelled word
result += element.suggestions[0].suggestion;
// Increment the offset by the length of the misspelled word
previousOffset = element.offset + element.token.length;
}
// Append the text after the last misspelled word.
if (previousOffset < text.length) {
result += text.substring(previousOffset);
}
resolve(result);
}
});
} else {
resolve(text);
}
})
};
根据Gary的建议,这是正确的方法:
bot.use({
botbuilder: function (session, next) {
if (session.message.text.startsWith(dontCorrectIdentifier)) {
session.message.text = session.message.text.replace(dontCorrectIdentifier, "");
next();
} else {
spellService
.getCorrectedText(session.message.text)
.then(function (text) {
if (session.message.text !== text) {
var msg = new builder.Message(session)
.text("Did you mean \"" + text + "\"?")
.suggestedActions(builder.SuggestedActions.create(session, [
builder.CardAction.postBack(session, dontCorrectIdentifier + text, "Yes"),
builder.CardAction.postBack(session, dontCorrectIdentifier + session.message.text, "No")
]
));
session.send(msg);
} else {
next();
}
})
.catch(function (error) {
console.error(error);
next();
});
}
}
});
修改builder
中间的逻辑流程,这对我来说很好:
bot.use({
botbuilder: (session, next) => {
if (session.message.text.startsWith(dontCorrectIdentifier)) {
session.message.text = session.message.text.replace(dontCorrectIdentifier, "");
next();
} else {
var text = session.message.text;
var msg = new builder.Message(session)
.text("Did you mean \"" + text + "\"?")
.suggestedActions(builder.SuggestedActions.create(session, [
builder.CardAction.postBack(session, dontCorrectIdentifier + text, "Yes"),
builder.CardAction.postBack(session, dontCorrectIdentifier + session.message.text, "No")
]));
session.send(msg);
}
},
})
使用session.send(msg);
替换builder.Prompts.text(session, msg)
。并在else条件下删除next()
。
您需要在URL中附加文本参数。我试着卷曲:
curl -v -X GET https://api.cognitive.microsoft.com/bing/v7.0/SpellCheck?mkt=en-US&mode=proof&text=hollo%2Cwrld
另一件事是.js文件中拼写和服务之间的连字符。您可以尝试先检查json响应,看看是否正在进行校正。