Spaces:
Runtime error
Runtime error
Download streaming-react-app/src/sliceTranslationSentencesUtils.ts from MACXXX/seamless-streaming: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/spaces/MACXXX/seamless-streaming/resolve/main/streaming-react-app/src/sliceTranslationSentencesUtils.ts
- Command line
-
hf download hf://spaces/MACXXX/seamless-streaming/streaming-react-app/src/sliceTranslationSentencesUtils.ts
-
curl -L -o sliceTranslationSentencesUtils.ts https://huggingface.co/spaces/MACXXX/seamless-streaming/resolve/main/streaming-react-app/src/sliceTranslationSentencesUtils.ts
1.21 kB
| import {TranslationSentences} from './types/StreamingTypes'; | |
| export function getTotalSentencesLength( | |
| translatedSentences: TranslationSentences, | |
| ) { | |
| return translatedSentences.reduce((acc, curr) => acc + curr.length, 0); | |
| } | |
| /** | |
| * @returns A new array of strings where the total length of the strings === targetIndex, | |
| * aka it's as if we joined all the strings together, called joined.slice(0, targetIndex), and then | |
| * split the string back into an array of strings. | |
| */ | |
| export function sliceTranslationSentencesUpToIndex( | |
| translatedSentences: TranslationSentences, | |
| targetIndex: number, | |
| ): TranslationSentences { | |
| return translatedSentences.reduce<TranslationSentences>((acc, sentence) => { | |
| const accTotalLength = getTotalSentencesLength(acc); | |
| if (accTotalLength === targetIndex) { | |
| return acc; | |
| } | |
| // If adding the current sentence does not exceed the targetIndex, then add the whole sentence | |
| if (accTotalLength + sentence.length <= targetIndex) { | |
| return [...acc, sentence]; | |
| } | |
| // If adding the current sentence DOES exceed the targetIndex, then slice the sentence and add it | |
| return [...acc, sentence.slice(0, targetIndex - accTotalLength)]; | |
| }, []); | |
| } | |