|
| 1 | +/** |
| 2 | + * Convert Speechmatics |
| 3 | + */ |
| 4 | + |
| 5 | +import generateEntitiesRanges from '../generate-entities-ranges/index.js'; |
| 6 | + |
| 7 | +/** |
| 8 | + * groups words list from speechmatics based on punctuation. |
| 9 | + * @todo To be more accurate, should introduce an honorifics library to do the splitting of the words. |
| 10 | + * @todo As this function is also used in the bbc-kaldi adapter, should it be refactored into its own file? |
| 11 | + * @param {array} words - array of words objects from speechmatics transcript |
| 12 | + */ |
| 13 | + |
| 14 | +const groupWordsInParagraphs = (words) => { |
| 15 | + const results = []; |
| 16 | + let paragraph = { words: [], text: [] }; |
| 17 | + |
| 18 | + words.forEach((word) => { |
| 19 | + // if word contains punctuation |
| 20 | + if (/[.?!]/.test(word.punct)) { |
| 21 | + paragraph.words.push(word); |
| 22 | + paragraph.text.push(word.punct); |
| 23 | + results.push(paragraph); |
| 24 | + // reset paragraph |
| 25 | + paragraph = { words: [], text: [] }; |
| 26 | + } else { |
| 27 | + paragraph.words.push(word); |
| 28 | + paragraph.text.push(word.punct); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + return results; |
| 33 | +}; |
| 34 | + |
| 35 | +/** |
| 36 | + * Determines the speaker of a paragraph by comparing the start time of the paragraph with |
| 37 | + * the speaker times. |
| 38 | + * @param {float} start - Starting point of paragraph |
| 39 | + * @param {array} speakers - list of all speakers with start and end time |
| 40 | + */ |
| 41 | +const getSpeaker = (start, speakers) => { |
| 42 | + for (var speakerIdx in speakers) { |
| 43 | + const speaker = speakers[speakerIdx]; |
| 44 | + if (start >= speaker.start & start < speaker.end) { |
| 45 | + return speaker.name; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return 'UNK'; |
| 50 | +}; |
| 51 | + |
| 52 | +/** |
| 53 | + * Speechmatics treats punctuation as own words. This function merges punctuations with |
| 54 | + * the pevious word and adjusts the total duration of the word. |
| 55 | + * @param {array} words - array of words objects from speechmatics transcript |
| 56 | + */ |
| 57 | +const curatePunctuation = (words) => { |
| 58 | + const curatedWords = []; |
| 59 | + words.forEach((word) => { |
| 60 | + if (/[.?!]/.test(word.name)) { |
| 61 | + curatedWords[curatedWords.length-1].name = curatedWords[curatedWords.length-1].name + word.name; |
| 62 | + curatedWords[curatedWords.length-1].duration = (parseFloat(curatedWords[curatedWords.length-1].duration) + parseFloat(word.duration)).toString(); |
| 63 | + } else { |
| 64 | + curatedWords.push(word); |
| 65 | + } |
| 66 | + } |
| 67 | + ); |
| 68 | + |
| 69 | + return curatedWords; |
| 70 | +}; |
| 71 | + |
| 72 | +const speechmaticsToDraft = (speechmaticsJson) => { |
| 73 | + const results = []; |
| 74 | + |
| 75 | + let tmpWords; |
| 76 | + tmpWords = curatePunctuation(speechmaticsJson.words); |
| 77 | + tmpWords = tmpWords.map((element, index) => { |
| 78 | + return ({ |
| 79 | + start: element.time, |
| 80 | + end: (parseFloat(element.time) + parseFloat(element.duration)).toString(), |
| 81 | + confidence: element.confidence, |
| 82 | + word: element.name.toLowerCase().replace(/[.?!]/g, ''), |
| 83 | + punct: element.name, |
| 84 | + index: index, |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + let tmpSpeakers; |
| 89 | + tmpSpeakers = speechmaticsJson.speakers; |
| 90 | + tmpSpeakers = tmpSpeakers.map((element) => { |
| 91 | + return ({ |
| 92 | + start: element.time, |
| 93 | + end: (parseFloat(element.time) + parseFloat(element.duration)).toString(), |
| 94 | + name: element.name, |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + const wordsByParagraphs = groupWordsInParagraphs(tmpWords); |
| 99 | + |
| 100 | + wordsByParagraphs.forEach((paragraph) => { |
| 101 | + const paragraphStart = paragraph.words[0].start; |
| 102 | + const draftJsContentBlockParagraph = { |
| 103 | + text: paragraph.text.join(' '), |
| 104 | + type: 'paragraph', |
| 105 | + data: { |
| 106 | + speaker: getSpeaker(paragraphStart, tmpSpeakers), |
| 107 | + words: paragraph.words, |
| 108 | + start: paragraphStart |
| 109 | + }, |
| 110 | + // the entities as ranges are each word in the space-joined text, |
| 111 | + // so it needs to be compute for each the offset from the beginning of the paragraph and the length |
| 112 | + entityRanges: generateEntitiesRanges(paragraph.words, 'punct'), // wordAttributeName |
| 113 | + }; |
| 114 | + results.push(draftJsContentBlockParagraph); |
| 115 | + }); |
| 116 | + |
| 117 | + return results; |
| 118 | +}; |
| 119 | + |
| 120 | +export default speechmaticsToDraft; |
0 commit comments