首页 > 解决方案 > 如何在 Google Speech API 中显示单词级别的置信度分数

问题描述

我在 Cloud Functions 中包含了 Google Speech API。我想获得单词级别的置信度分数,所以我将“enableWordConfidence”设置为 true。出于某种原因,响应不会返回单词级别的置信度分数。

我曾尝试将 de-DE 和 en-US 作为语言代码,但两者都不起作用。这是来自 Google 的官方文档,但是复制该代码并在云函数中运行它也没有返回单词级别的置信度。 https://cloud.google.com/speech-to-text/docs/word-confidence

这是代码:

const filePath = `gs://PATH_TO_AUDIO.flac`

const audio = {
    uri: filePath,
};

const config = {
    encoding: 'FLAC',
    sampleRateHertz: 16000,
    languageCode: languageCode,
    enableSpeakerDiarization: true,
    enableWordConfidence: true,
    useEnhanced: true,
    enableWordTimeOffsets: true,
    enableAutomaticPunctuation: true,
};

if(languageCode == 'en-US') {
    config.model = 'video'
}

const request = {
    audio: audio,
    config: config,
};

const client = new speech.SpeechClient();

return client
.longRunningRecognize(request)
.then(data => {
    const operation = data[0];

    return operation.promise();
}).then(data => {
    const response = data[0];

    if(response.results) {
        return storeSegmentInMeeting(response.results, noteId);
    } else {
        return null;
    }
}).catch(err => {
    return console.error('ERROR:', err);
});

我希望得到每个单词的置信度分数,所以有什么想法吗?

标签: node.jsgoogle-speech-api

解决方案


好的,除非您切换到库的“V1P1BETA”版本,否则这可能不起作用。它具有更多功能,包括单词级别的置信度。我建议你试试。

我不知道你是否已经这样做了,所以我分享这个以防万一。

你需要:

const speech = require('@google-cloud/speech').v1p1beta1;

推荐阅读