gpt4 book ai didi

javascript - 如何向 Web Speech API 添加标点符号?

转载 作者:行者123 更新时间:2023-11-28 03:42:20 29 4
gpt4 key购买 nike

我正在使用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);
};

最佳答案

即使我正在从网络语音 API 中寻找更好的答案,但目前我已经使用标点符号 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 });
}
};
});

在我的 Controller 中:

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

关于javascript - 如何向 Web Speech API 添加标点符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48839314/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com