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

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