def process_batch_transcription()

in function_app/src/components/speech.py [0:0]


def process_batch_transcription(transcription: Dict) -> Transcription:
    """
    Processes a transcription response from the Azure AI Speech Batch
    Transcription API.

    :param transcription: The raw API response from Azure AI Speech API.
    :type transcription: Dict
    :return: A processed Transcription object.
    :rtype: Transcription
    """
    word_count = 0
    # Phrases come sorted by channel -> offset. Sort by offset alone
    raw_phrases = sorted(
        transcription["recognizedPhrases"], key=lambda phrase: phrase["offsetInTicks"]
    )
    processed_phrases: list[TranscribedPhrase] = list()
    for phrase_num, raw_phrase in enumerate(raw_phrases):
        raw_words = raw_phrase["nBest"][0]["displayWords"]
        processed_words = [
            process_batch_word_dict(raw_word, word_count + i)
            for i, raw_word in enumerate(raw_words)
        ]
        word_count += len(raw_words)
        processed_phrases.append(
            process_batch_phrase_dict(raw_phrase, phrase_num, processed_words)
        )
    return Transcription(
        phrases=processed_phrases,
        transcription_type=AzureSpeechTranscriptionEnum.BATCH,
        duration_secs=transcription["durationInTicks"]
        / TIME_DIVISORS[AzureSpeechTranscriptionEnum.BATCH],
        language=None,
        raw_api_response=transcription,
    )