我正在使用Web speech api,并想知道是否有关于如何检测或添加标点符号(逗号或句号)的文档。
我的代码目前看起来像这样:
var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onstart = function() {
recognizing = true;
};
recognition.onend = function() {
recognizing = false;
if (ignoreOnend) {
return;
}
if (!finalTranscript) {
return;
}
};
recognition.onresult = function(event) {
var interimTranscript = '';
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
finalTranscript += event.results[i][0].transcript;
} else {
interimTranscript += event.results[i][0].transcript;
}
}
finalTranscript = capitalize(finalTranscript);
finalSpan.innerHTML = linebreak(finalTranscript);
interimSpan.innerHTML = linebreak(interimTranscript);
};
答案 0 :(得分:0)
即使我正在寻找网络语音API的更好答案,现在,我已经使用了punctuator api来做到这一点。
NodeJs中的服务器端代码:
var logger = require('../helper/logger');
var moduleName = "punctuatorService";
var request = require('request');
var punctuatorUrl = 'http://bark.phon.ioc.ee/punctuator';
var punctuatorHelpers = {
Punctuate: function (req, res) {
var options = {
url: punctuatorUrl,
form: {
text: req.body.text
}
};
request.post(options, (error, response, body) => {
if (error) {
logger.log(req.user.userId, moduleName, "Punctuate",
logger.severity.error, "Error occured while fetching the Punctuate details.", "");
return res.json({ success: false, message: 'Something went wrong while saving.', error: error });
}
res.json({ success: true, message: body});
});
}
};
module.exports = punctuatorHelpers;
最后,我添加了标点符号。这是代码。
在我的Angular API服务中:
apiService.factory('punctuator', function ($http) {
var url = 'http://bark.phon.ioc.ee/punctuator';
return {
punctuate: function (message) {
return $http.post('/api/v1/punctuator/get', { "text": message });
}
};
});
在我的控制器中:
var punctuatorService = function (msg, punctuatedText_CB) {
punctuator.punctuate(msg).then(function (response) {
if (response.data.success) {
punctuatedText_CB(response.data.message);
}
else {
console.log(response.data.error);
punctuatedText_CB("");
}
}, function (error) {
console.log(error);
punctuatedText_CB("");
});
};
if (event.results[i].isFinal) {
final_transcript += event.results[i][0].transcript + " ";
punctuatorService(event.results[i][0].transcript, function (punctuatedText){
//Process the transcript with dialogflow and attach to the template.
processFinalSpeech(punctuatedText);
});
} else {
interim_transcript += event.results[i][0].transcript;
}