( topics: Topic[] | null | undefined, transcript: TranscriptSegment[] | null | undefined, )
| 300 | } |
| 301 | |
| 302 | export function hydrateTopicsWithTranscript( |
| 303 | topics: Topic[] | null | undefined, |
| 304 | transcript: TranscriptSegment[] | null | undefined, |
| 305 | ): Topic[] { |
| 306 | if (!Array.isArray(topics) || topics.length === 0) { |
| 307 | return Array.isArray(topics) ? topics : []; |
| 308 | } |
| 309 | |
| 310 | const normalizedTranscript = normalizeTranscript(transcript); |
| 311 | const hasTranscript = normalizedTranscript.length > 0; |
| 312 | |
| 313 | let transcriptIndex: TranscriptIndex | null = null; |
| 314 | |
| 315 | return topics.map((topic) => { |
| 316 | let hydratedSegments = normalizeSegments((topic as any)?.segments); |
| 317 | |
| 318 | if (hasTranscript && hydratedSegments.length === 0) { |
| 319 | if (topic.quote?.text) { |
| 320 | transcriptIndex = ensureTranscriptIndex(transcriptIndex, normalizedTranscript); |
| 321 | const match = findTextInTranscript( |
| 322 | normalizedTranscript, |
| 323 | topic.quote.text, |
| 324 | transcriptIndex, |
| 325 | { |
| 326 | strategy: 'all', |
| 327 | minSimilarity: 0.75, |
| 328 | }, |
| 329 | ); |
| 330 | |
| 331 | if (match) { |
| 332 | const segment = createSegmentFromMatch( |
| 333 | match, |
| 334 | normalizedTranscript, |
| 335 | topic.quote.text, |
| 336 | ); |
| 337 | if (segment) { |
| 338 | hydratedSegments = [segment]; |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | if (hydratedSegments.length === 0) { |
| 344 | const segmentFromTimestamp = createSegmentFromTimestamp( |
| 345 | topic.quote?.timestamp, |
| 346 | normalizedTranscript, |
| 347 | topic.quote?.text, |
| 348 | ); |
| 349 | if (segmentFromTimestamp) { |
| 350 | hydratedSegments = [segmentFromTimestamp]; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | if (hydratedSegments.length === 0) { |
| 355 | const fallbackSegment = createFallbackSegment(normalizedTranscript); |
| 356 | if (fallbackSegment) { |
| 357 | hydratedSegments = [fallbackSegment]; |
| 358 | } |
| 359 | } |
no test coverage detected