Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AbstractTranscriptionService.js 2.9KB

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