UNPKG

@bbc/react-transcript-editor

Version:

A React component to make transcribing audio and video easier and faster.

41 lines 2.48 kB
"use strict";var _index=_interopRequireDefault(require("../generate-entities-ranges/index"));Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}/** * Convert autoEdit2 Json * * into * ``` const blocks = [ { text: 'Hello', type: 'paragraph', data: { speaker: 'Foo', }, entityRanges: [], }, { text: 'World', type: 'paragraph', data: { speaker: 'Bar', }, entityRanges: [], }, ]; ``` * * See samples folder and test file * for reference data structures */ /** * groups words list from autoEdit transcript based on punctuation. * @todo To be more accurate, should introduce an honorifics library to do the splitting of the words. * @param {array} words - array of words objects from autoEdit transcript */var groupWordsInParagraphs=function(autoEditText){var results=[],paragraph={words:[],text:[]};return autoEditText.forEach(function(autoEditparagraph){autoEditparagraph.paragraph.forEach(function(autoEditLine){autoEditLine.line.forEach(function(word){// adjusting time reference attributes from // `startTime` `endTime` to `start` `end` // for word object var tmpWord={text:word.text,start:word.startTime,end:word.endTime};// if word contains punctuation /[.?!]/.test(word.text)?(paragraph.words.push(tmpWord),paragraph.text.push(word.text),results.push(paragraph),paragraph={words:[],text:[]}):(paragraph.words.push(tmpWord),paragraph.text.push(word.text))})})}),results},autoEdit2ToDraft=function(autoEdit2Json){var results=[],tmpWords=autoEdit2Json.text,wordsByParagraphs=groupWordsInParagraphs(tmpWords);// console.log(JSON.stringify(results,null,2)) return wordsByParagraphs.forEach(function(paragraph,i){var draftJsContentBlockParagraph={text:paragraph.text.join(" "),type:"paragraph",data:{speaker:"TBC ".concat(i),words:paragraph.words,start:paragraph.words[0].start},// the entities as ranges are each word in the space-joined text, // so it needs to be compute for each the offset from the beginning of the paragraph and the length entityRanges:(0,_index.default)(paragraph.words,"text")};// console.log(JSON.stringify(draftJsContentBlockParagraph,null,2)) results.push(draftJsContentBlockParagraph)}),results},_default=autoEdit2ToDraft;exports.default=_default;