|
| 1 | +/** |
| 2 | + * Converts GCP Speech to Text Json to DraftJs |
| 3 | + * see `sample` folder for example of input and output as well as `example-usage.js` |
| 4 | + */ |
| 5 | + |
| 6 | +import generateEntitiesRanges from '../generate-entities-ranges/index.js'; |
| 7 | + |
| 8 | +const NANO_SECOND = 1000000000; |
| 9 | + |
| 10 | +/** |
| 11 | + * attribute for the sentences object containing the text. eg sentences ={ punct:'helo', ... } |
| 12 | + * or eg sentences ={ text:'hello', ... } |
| 13 | + * @param sentences |
| 14 | + */ |
| 15 | +export const getBestAlternativeSentence = sentences => { |
| 16 | + if (sentences.alternatives.length === 0) { |
| 17 | + return sentences[0]; |
| 18 | + } |
| 19 | + |
| 20 | + const sentenceWithHighestConfidence = sentences.alternatives.reduce(function( |
| 21 | + prev, |
| 22 | + current |
| 23 | + ) { |
| 24 | + return parseFloat(prev.confidence) > parseFloat(current.confidence) |
| 25 | + ? prev |
| 26 | + : current; |
| 27 | + }); |
| 28 | + |
| 29 | + return sentenceWithHighestConfidence; |
| 30 | +}; |
| 31 | + |
| 32 | +export const trimLeadingAndTailingWhiteSpace = text => { |
| 33 | + return text.trim(); |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * GCP does not provide a nanosecond attribute if the word starts at 0 nanosecond |
| 38 | + * @param startSecond |
| 39 | + * @param nanoSecond |
| 40 | + * @returns {number} |
| 41 | + */ |
| 42 | +const computeTimeInSeconds = (startSecond, nanoSecond) => { |
| 43 | + |
| 44 | + let seconds = parseFloat(startSecond); |
| 45 | + |
| 46 | + if (nanoSecond !== undefined) { |
| 47 | + seconds = seconds + parseFloat(nanoSecond / NANO_SECOND); |
| 48 | + } |
| 49 | + |
| 50 | + return seconds; |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * Normalizes words so they can be used in |
| 55 | + * the generic generateEntitiesRanges() method |
| 56 | + **/ |
| 57 | +const normalizeWord = (currentWord, confidence) => { |
| 58 | + |
| 59 | + return { |
| 60 | + start: computeTimeInSeconds(currentWord.startTime.seconds, currentWord.startTime.nanos), |
| 61 | + end: computeTimeInSeconds(currentWord.endTime.seconds, currentWord.endTime.nanos), |
| 62 | + text: currentWord.word, |
| 63 | + confidence: confidence |
| 64 | + }; |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + * groups words list from GCP Speech to Text response. |
| 69 | + * @param {array} sentences - array of sentence objects from GCP STT |
| 70 | + */ |
| 71 | +const groupWordsInParagraphs = sentences => { |
| 72 | + const results = []; |
| 73 | + let paragraph = { |
| 74 | + words: [], |
| 75 | + text: [] |
| 76 | + }; |
| 77 | + |
| 78 | + sentences.forEach((sentence) => { |
| 79 | + const bestAlternative = getBestAlternativeSentence(sentence); |
| 80 | + paragraph.text.push(trimLeadingAndTailingWhiteSpace(bestAlternative.transcript)); |
| 81 | + |
| 82 | + bestAlternative.words.forEach((word) => { |
| 83 | + paragraph.words.push(normalizeWord(word, bestAlternative.confidence)); |
| 84 | + }); |
| 85 | + results.push(paragraph); |
| 86 | + paragraph = { words: [], text: [] }; |
| 87 | + }); |
| 88 | + |
| 89 | + return results; |
| 90 | +}; |
| 91 | + |
| 92 | +const gcpSttToDraft = gcpSttJson => { |
| 93 | + const results = []; |
| 94 | + // const speakerLabels = gcpSttJson.results[0]['alternatives'][0]['words'][0]['speakerTag'] |
| 95 | + // let speakerSegmentation = typeof(speakerLabels) != 'undefined'; |
| 96 | + |
| 97 | + const wordsByParagraphs = groupWordsInParagraphs(gcpSttJson.results); |
| 98 | + |
| 99 | + wordsByParagraphs.forEach((paragraph, i) => { |
| 100 | + const draftJsContentBlockParagraph = { |
| 101 | + text: paragraph.text.join(' '), |
| 102 | + type: 'paragraph', |
| 103 | + data: { |
| 104 | + speaker: paragraph.speaker ? `Speaker ${ paragraph.speaker }` : `TBC ${ i }`, |
| 105 | + words: paragraph.words, |
| 106 | + start: parseFloat(paragraph.words[0].start) |
| 107 | + }, |
| 108 | + // the entities as ranges are each word in the space-joined text, |
| 109 | + // so it needs to be compute for each the offset from the beginning of the paragraph and the length |
| 110 | + entityRanges: generateEntitiesRanges(paragraph.words, 'text') // wordAttributeName |
| 111 | + }; |
| 112 | + results.push(draftJsContentBlockParagraph); |
| 113 | + }); |
| 114 | + |
| 115 | + return results; |
| 116 | +}; |
| 117 | + |
| 118 | +export default gcpSttToDraft; |
0 commit comments