pietrop / slate-transcript-editor

A React component to make correcting automated transcriptions of audio and video easier and faster. Using the SlateJs editor.
https://pietrop.github.io/slate-transcript-editor
Other
75 stars 33 forks source link

Semplify the logic for `generatePreviousTimingsUpToCurrent` #41

Closed pietrop closed 3 years ago

pietrop commented 3 years ago

Leaving a not to look into simplifying the logic for generatePreviousTimingsUpToCurrent which used in css injection paragraph highlighting feature. To see if it can be implemented without being reliant on the actual list of words.

verbose generate previous timings up to current func

/**
 * See explanation in `src/utils/dpe-to-slate/index.js` for how this function works with css injection
 * to provide current paragaph's highlight.
 * @param {Number} currentTime - float in seconds
 */
const generatePreviousTimingsUpToCurrent = (currentTime) => {
  const lastWordStartTime = props.transcriptData.words[props.transcriptData.words.length - 1].start;
  const lastWordStartTimeInt = parseInt(lastWordStartTime);
  const emptyListOfTimes = Array(lastWordStartTimeInt);
  const listOfTimesInt = [...emptyListOfTimes.keys()];
  const listOfTimesUpToCurrentTimeInt = listOfTimesInt.splice(0, currentTime, 0);
  const stringlistOfTimesUpToCurrentTimeInt = listOfTimesUpToCurrentTimeInt.join(' ');
  return stringlistOfTimesUpToCurrentTimeInt;
};

One line

const generatePreviousTimingsUpToCurrent = (currentTime) => {
  return [...Array(parseInt(props.transcriptData.words[props.transcriptData.words.length - 1].start)).keys()].splice(0, currentTime, 0).join(' ');
};

possible solution

simplified without using words just making a list of possible time integers up to current

function generatePreviousTimingsUpToCurrent(start) {
  return new Array(parseInt(start))
    .fill(1)
    .map((_, i) => i + 1)
    .join(' ');
}

Mostly to reduce potential issues when there's bugs on the list of words, eg missing start, end attributes etc.. Which there shouldn't be but might come from upstream issues, eg from STT converters.

To do this, it requires generatePreviousTimingsUpToCurrent untangling in the code where this is being used and replace all instances.