3

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

1 回答 1

2

即使我正在从网络语音 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;
}
于 2018-03-27T08:23:19.703 回答