You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractTranscriptionService.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Abstract class representing an interface to implement a speech-to-text
  3. * service on.
  4. */
  5. const TranscriptionService = function() {
  6. throw new Error('TranscriptionService is abstract and cannot be'
  7. + 'created');
  8. };
  9. /**
  10. * This method can be used to send the recorder audio stream and
  11. * retrieve the answer from the transcription service from the callback
  12. *
  13. * @param {RecordingResult} recordingResult a recordingResult object which
  14. * includes the recorded audio stream as a blob
  15. * @param {Function} callback which will retrieve the a RecordingResult with
  16. * the answer as a WordArray
  17. */
  18. TranscriptionService.prototype.send = function send(recordingResult, callback) {
  19. this.sendRequest(recordingResult.blob, response => {
  20. if (this.verify(response)) {
  21. recordingResult.wordArray = this.formatResponse(response);
  22. } else {
  23. console.log('the retrieved response from the server is not valid!');
  24. recordingResult.wordArray = [];
  25. }
  26. callback(recordingResult);
  27. });
  28. };
  29. /**
  30. * Abstract method which will rend the recorder audio stream to the implemented
  31. * transcription service and will retrieve an answer, which will be
  32. * called on the given callback method
  33. *
  34. * @param {Blob} audioBlob the recorded audio stream as a single Blob
  35. * @param {function} callback function which will retrieve the answer
  36. * from the service
  37. */
  38. // eslint-disable-next-line no-unused-vars
  39. TranscriptionService.prototype.sendRequest = function(audioBlob, callback) {
  40. throw new Error('TranscriptionService.sendRequest is abstract');
  41. };
  42. /**
  43. * Abstract method which will parse the output from the implemented
  44. * transcription service to the expected format
  45. *
  46. * The transcriber class expect an array of word objects, where each word
  47. * object is one transcribed word by the service.
  48. *
  49. * The expected output of this method is an array of word objects, in
  50. * the correct order. That is, the first object in the array is the first word
  51. * being said, and the last word in the array is the last word being said
  52. *
  53. * @param response the answer from the speech-to-text server which needs to be
  54. * formatted
  55. * @return {Array<Word>} an array of Word objects
  56. */
  57. // eslint-disable-next-line no-unused-vars
  58. TranscriptionService.prototype.formatResponse = function(response) {
  59. throw new Error('TranscriptionService.format is abstract');
  60. };
  61. /**
  62. * Abstract method which will verify that the response from the server is valid
  63. *
  64. * @param response the response from the server
  65. * @return {boolean} true if response is valid, false otherwise
  66. */
  67. // eslint-disable-next-line no-unused-vars
  68. TranscriptionService.prototype.verify = function(response) {
  69. throw new Error('TranscriptionService.verify is abstract');
  70. };
  71. module.exports = TranscriptionService;