(
transcript: unknown,
options: TranscriptLanguageOptions = {}
)
| 57 | } |
| 58 | |
| 59 | export function assessTranscriptLanguage( |
| 60 | transcript: unknown, |
| 61 | options: TranscriptLanguageOptions = {} |
| 62 | ): TranscriptLanguageAssessment { |
| 63 | if (!Array.isArray(transcript) || transcript.length === 0) { |
| 64 | return { isEnglish: false, reason: 'NO_TRANSCRIPT' }; |
| 65 | } |
| 66 | |
| 67 | const reportedLanguages = transcript |
| 68 | .map(extractSegmentLanguage) |
| 69 | .filter((lang): lang is string => Boolean(lang)); |
| 70 | |
| 71 | if (reportedLanguages.length > 0) { |
| 72 | const hasEnglish = reportedLanguages.some( |
| 73 | (lang) => lang === 'en' || lang.startsWith('en-') |
| 74 | ); |
| 75 | return { |
| 76 | isEnglish: hasEnglish, |
| 77 | reason: hasEnglish ? 'REPORTED_ENGLISH' : 'REPORTED_NON_ENGLISH', |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | const sampleLimit = options.sampleSegments ?? DEFAULT_SAMPLE_SEGMENTS; |
| 82 | const sampleText = transcript |
| 83 | .slice(0, sampleLimit) |
| 84 | .map(extractSegmentText) |
| 85 | .join(' ') |
| 86 | .replace(/\s+/g, ' ') |
| 87 | .trim(); |
| 88 | |
| 89 | if (!sampleText) { |
| 90 | return { isEnglish: false, reason: 'EMPTY_SAMPLE' }; |
| 91 | } |
| 92 | |
| 93 | const textWithoutSpaces = sampleText.replace(/\s/g, ''); |
| 94 | const nonSpaceLength = textWithoutSpaces.length; |
| 95 | if (nonSpaceLength === 0) { |
| 96 | return { isEnglish: false, reason: 'EMPTY_SAMPLE' }; |
| 97 | } |
| 98 | |
| 99 | const englishLetterCount = (textWithoutSpaces.match(/[A-Za-z]/g) ?? []).length; |
| 100 | const englishRatio = englishLetterCount / nonSpaceLength; |
| 101 | const cjkCharacterPresent = CJK_REGEX.test(sampleText); |
| 102 | const minEnglishRatio = options.minEnglishRatio ?? DEFAULT_MIN_ENGLISH_RATIO; |
| 103 | const strictCjkThreshold = options.strictCjkThreshold ?? DEFAULT_CJK_RATIO_THRESHOLD; |
| 104 | |
| 105 | if (cjkCharacterPresent && englishRatio < strictCjkThreshold) { |
| 106 | return { isEnglish: false, reason: 'CJK_DOMINANT', englishRatio }; |
| 107 | } |
| 108 | |
| 109 | if (englishRatio < minEnglishRatio) { |
| 110 | return { isEnglish: false, reason: 'LOW_ENGLISH_RATIO', englishRatio }; |
| 111 | } |
| 112 | |
| 113 | return { isEnglish: true, reason: 'RATIO_THRESHOLD', englishRatio }; |
| 114 | } |
| 115 | |
| 116 | export function isTranscriptEnglish( |
no outgoing calls
no test coverage detected