借助模型自适应提高转录结果

概览

您可以使用模型自适应功能,帮助 Speech-to-Text 更加频繁识别特定字词或短语,而不是其他替代做法。例如,假设您的音频数据中出现“weather”一词的频率较高。当“Speech-to-Text”遇到“weather”一词时,您希望将该字词转录为“weather”,而不是“whether”。在这种情况下,您可以使用模型自适应功能,使 Speech-to-Text 更偏向于识别“weather”。

模型自适应功能在以下用例中特别有用:

  • 提高识别音频数据中经常出现的字词和短语的准确率。例如,您可以提醒识别模型使用用户经常说出的语音指令。

  • 扩展 Speech-to-Text 的识别字词库。 Speech-to-Text 包含一个非常庞大的词汇表。但是,如果您的音频数据经常包含常规用语中很少使用的字词(例如专有名词或特定领域的字词),则可以使用模型自适应功能添加这些字词。

  • 提供的音频含有噪音或不太清晰时,提高语音转写的准确率。

要查看模型自适应功能是否适用于您的语言,请参阅语言支持页面

提高字词和短语的识别准确率

为了提高 Speech-to-Text 在转写您的音频数据时识别出“weather”一词的概率,您可以在 SpeechAdaptation 资源中的 PhraseSet 对象中传递单个单词“weather”。

提供多字词短语时,Speech-to-Text 更有可能按顺序识别这些字词。提供短语还会提升识别短语中各部分(包括单个字词)的概率。请参阅内容限制页面,了解这些短语的数量和大小限制。

(可选)您可以使用增强型模型自适应功能微调模型自适应的强度。

使用类改善识别效果

代表自然语言中的常见概念,例如货币单位和日历日期。类让您可以提高大量字词组的转录准确率,这些字词组映射到常见概念,但不总是包含相同字词或词组。

例如,假设您的音频数据包含讲话人说出其街道地址的录音。您的某个录音可能是讲话人说“My house is 123 Main Street, the fourth house on the left”。在这种情况下,您希望 Speech-to-Text 将第一个数字序列(“123”)识别为地址而不是序号(“一百二十三”)。不过,并非所有人都住在“123 Main Street”。在一个 PhraseSet 资源中列出所有可能的街道地址是不切实际的。相反,您可以使用类来指示应该识别门牌号,无论这串数字实际是什么。在此示例中,Speech-to-Text 可以更准确地转写“123 Main Street”和“987 Grand Boulevard”等短语,因为它们都被识别为地址门牌号。

类令牌

如需在模型自适应中使用类,请在 PhraseSet 资源的 phrases 字段中包括类令牌。请参阅支持的类令牌列表,了解您的语言支持哪些类令牌。例如,如需改善源音频中地址门牌号的转写结果,请为 SpeechContext 对象提供值 $ADDRESSNUM

您可以将类用作 phrases 数组中的独立项,也可以将一个或多个类令牌嵌入到较长的多字词短语中。例如,您可以在较长的短语中以字符串的形式添加类令牌 ["my address is $ADDRESSNUM"],以指明地址门牌号。但是,如果音频中包含类似但不相同的短语,例如“I am at 123 Main Street”,则此短语无效。为帮助识别相似的短语,您可以额外添加同一个类令牌,如 ["my address is $ADDRESSNUM", "$ADDRESSNUM"]。如果您使用无效或格式错误的类令牌,Speech-to-Text 会忽略该令牌而不触发错误,但仍会使用短语的其余部分作为上下文。

自定义类

您还可以创建自己的 CustomClass(一个类,包含相关项或值的您自己的自定义列表)。例如,您要转换的音频数据可能包含数百个区域性餐厅中的任何一个的名称。餐馆名称在普通语音中相对不常见,因此被识别模型选作“正确”答案的可能性较低。在使用自定义类的情况下,当这些名称出现在音频中时,您可以使识别模型偏向于正确识别这些名称。

若要使用自定义类,请创建 CustomClass 资源,其包含所有餐馆名称作为 ClassItem。 自定义类的工作方式与预构建的类令牌相同。phrase 可以同时包含预构建的类令牌和自定义类。

ABNF 语法

您还可以使用增强巴科斯范式 (ABNF) 形式的语法来指定字词的模式。在请求的模型自适应中包含 ABNF 语法将提高 Speech-to-Text 识别与指定语法匹配的所有字词的概率。

要使用此功能,请在请求的 SpeechAdaptation 字段中添加 ABNF grammar 对象。ABNF 语法还可以包括对 CustomClassPhraseSet 资源的引用。如需详细了解此字段的语法,请参阅下面的 Speech Recognition Grammar Specificationcode sample

使用增强型功能微调转录结果

默认情况下,大多数时候,模型自适应应该已经提供了足够的效果。模型自适应增强型功能允许您通过为某些短语分配比其他短语更高的权重来提高识别模型偏差。仅在以下情况下,我们才建议您实现增强型功能:1) 已实现模型自适应;2) 您希望进一步调整模型自适应对转写结果的影响强度。

例如,您的很多录音中讲话人在询问“fare to get into the county fair”,其中“fair”一词出现的频率高于“fare”。在这种情况下,您可以使用模型自适应,通过将“fair”和“fare”添加为 PhraseSet 资源中的 phrases 来提高模型识别“fair”和“fare”的可能性。这会指示 Speech-to-Text 更频繁地识别“fair”和“fare”,而不是如“hare”或“lair”这些词。

不过,“fair”的识别频率要高于“fare”,因为它在音频中出现的频率更高。您可能已经使用 Speech-to-Text API 转录了音频,并发现在识别正确的字词(“fair”)时存在大量错误。在本例中,您可能需要额外将短语与增强型功能搭配使用,为“fair”指定比“fare”更高的增强值。为“fair”指定较高的权重值会使 Speech-to-Text API 选择“fair”的频率高于“fare”。如果没有增强值,识别模型将识别出具有相同概率的“fair”和“fare”。

增强基础知识

使用增强型功能时,可为 PhraseSet 资源中的 phrase 项指定权重值。Speech-to-Text 为音频数据中的字词选择可能的转写时,会参考此权重值。值越高,Speech-to-Text 从可能的备选项中选择该字词或短语的可能性就越大。

例如,您想为短语“我最喜欢的美国自然历史博物馆的展览是蓝鲸”指定一个增强值。如果您向 phrase 对象添加该短语并分配增强值,则识别模型更有可能完整地逐字识别该词组。

如果通过增强一个多单词短语没有得到想要的结果,我们建议将组成该短语的所有双字母组合(2个单词,按顺序)添加为附加 phrase 项,并为每个短语分配增强值。继续上面的例子,您可以研究添加额外的二元语法和 N 元语法(超过 2 个字词),例如“我的最爱”“我最爱的展览”“最爱的展览”“我最爱的美国自然历史博物馆展览”“美国自然历史博物馆”“蓝鲸”等。Speech-to-Text 识别模型更有可能识别音频中的相关短语,这些短语包含原始增强短语的一部分,但不能逐字匹配。

设置增强值

增强值必须是大于 0 的浮点值。增强值的实际上限为 20。为获得最佳转写结果,请通过向上或向下调整增强值来尝试转写结果,直到获得准确的转写结果。

较高的增强值可以减少假负例,假负例是指音频中出现的字词或短语未被 Speech-to-Text 正确识别的情况。但是,增强型功能也会增加出现假正例的可能性;假正例是指音频中不包含的字词或短语出现在转写中的情况。

获取超时通知

Speech-to-Text 响应包含 SpeechAdaptationInfo 字段,其中提供有关识别期间模型自适应行为的信息。如果发生与模型自适应相关的超时,adaptationTimeout 将为 truetimeoutMessage 将指定哪些自适应配置导致超时。发生超时时,模型自适应不会影响返回的转录内容。

使用模型自适应的示例用例

以下示例引导您完成使用模型自适应转录一段录音的过程,录音的内容是“call me fionity and oh my gosh what do we have here ionity”。在本例中,模型必须正确识别“fionity”和“ionity”。

以下命令在未使用模型自适应的情况下对音频执行识别。生成的转写结果不正确:“all me Fiona tea and oh my gosh what do we have here I own a day”。

   curl -H "Authorization: Bearer $(gcloud auth
   --impersonate-service-account=$SA_EMAIL print-access-token)" -H
   "Content-Type: application/json; charset=utf-8"
   "https://speech.googleapis.com/v1p1beta1/speech:recognize" -d '{"config":
   {"languageCode": "en-US"}, "audio":
   {"uri":"gs://biasing-resources-test-audio/call_me_fionity_and_ionity.wav"}}'
   

示例请求:

     {
       "config":{
       "languageCode":"en-US"
       },
       "audio":{
          "uri":"gs://biasing-resources-test-audio/call_me_fionity_and_ionity.wav"
       }
     }
   

使用 PhraseSet 提高转录结果

  1. 创建 PhraseSet

    curl -X POST -H "Authorization: Bearer $(gcloud auth
    --impersonate-service-account=$SA_EMAIL print-access-token)" -H
    "Content-Type: application/json; charset=utf-8"
    "https://speech.googleapis.com/v1p1beta1/projects/project_id/locations/global/phraseSets"
    -d '{"phraseSetId": "test-phrase-set-1"}'
    

    示例请求:

    {
       "phraseSetId":"test-phrase-set-1"
    }
    
  2. 获取 PhraseSet

    curl -X GET -H "Authorization: Bearer $(gcloud auth
    --impersonate-service-account=$SA_EMAIL print-access-token)" -H
    "Content-Type: application/json; charset=utf-8"
    "https://speech.googleapis.com/v1p1beta1/projects/project_id>/locations/global/phraseSets/test-phrase-set-1"\
    
  3. 将短语“fionity”和“ionity”添加到 PhraseSet,并为每个语句分配一个 boost 值 10:

    curl -X PATCH -H "Authorization: Bearer $(gcloud auth
    --impersonate-service-account=$SA_EMAIL print-access-token)" -H
    "Content-Type: application/json; charset=utf-8"
    "https://speech.googleapis.com/v1p1beta1/projects/project_id/locations/global/phraseSets/test-phrase-set-1?updateMask=phrases"\
    -d '{"phrases": [{"value": "ionity", "boost": 10}, {"value": "fionity", "boost": 10}]}'
    

    PhraseSet 现在更新为:

    {
      "phrases":[
         {
              "value":"ionity",
              "boost":10
           },
           {
              "value":"fionity",
              "boost":10
           }
        ]
     }
    
  4. 再次识别音频,这次使用模型自适应和先前创建的 PhraseSet。现在,转录的结果是正确的:“call me fionity and oh my gosh what do we have here ionity”

    curl -H "Authorization: Bearer $(gcloud auth --impersonate-service-account=$SA_EMAIL print-access-token)"
    -H "Content-Type: application/json; charset=utf-8"
    "https://speech.googleapis.com/v1p1beta1/speech:recognize" -d '{"config":
    {"adaptation": {"phrase_set_references": ["projects/project_id/locations/global/phraseSets/test-phrase-set-1"]},
    "languageCode": "en-US"}, "audio": {"uri":"gs://biasing-resources-test-audio/call_me_fionity_and_ionity.wav"}}'
    

    示例请求:

    {
       "config":{
          "adaptation":{
             "phrase_set_references":[
                "projects/project_id/locations/global/phraseSets/test-phrase-set-1"
             ]
          },
          "languageCode":"en-US"
       },
       "audio":{
          "uri":"gs://biasing-resources-test-audio/call_me_fionity_and_ionity.wav"
       }
    }
    

使用 CustomClass 提高转录结果

  1. 创建 CustomClass

    curl -X POST -H "Authorization: Bearer $(gcloud auth
    --impersonate-service-account=$SA_EMAIL print-access-token)" -H
    "Content-Type: application/json; charset=utf-8"
    "https://speech.googleapis.com/v1p1beta1/projects/project_id/locations/global/customClasses"
    -d '{"customClassId": "test-custom-class-1"}'
    

    示例请求:

    {
       "customClassId": "test-custom-class-1"
    }
    
  2. 获取 CustomClass

     curl -X GET -H "Authorization: Bearer $(gcloud auth
     --impersonate-service-account=$SA_EMAIL print-access-token)" -H
     "Content-Type: application/json; charset=utf-8"
     "https://speech.googleapis.com/v1p1beta1/projects/project_id/locations/global/customClasses/test-custom-class-1"
     

  3. 识别测试音频片段。CustomClass 为空,因此返回的转录仍然不正确:“call me Fiona tea and oh my gosh what do we have here I own a day”:

    curl -H "Authorization: Bearer $(gcloud auth
    --impersonate-service-account=$SA_EMAIL print-access-token)" -H
    "Content-Type: application/json; charset=utf-8"
    "https://speech.googleapis.com/v1p1beta1/speech:recognize" -d '{"config":
    {"adaptation": {"phraseSets": [{"phrases": [{"value":
    "${projects/project_idlocations/global/customClasses/test-custom-class-1}",
    "boost": "10"}]}]}, "languageCode": "en-US"}, "audio":
    {"uri":"gs://biasing-resources-test-audio/call_me_fionity_and_ionity.wav"}}'
    

    示例请求:

      {
       "config":{
          "adaptation":{
             "phraseSets":[
                {
                   "phrases":[
                      {
                         "value":"${projects/project_id/locations/global/customClasses/test-custom-class-1}",
                         "boost":"10"
                      }
                   ]
                }
             ]
          },
          "languageCode":"en-US"
       },
       "audio":{
          "uri":"gs://biasing-resources-test-audio/call_me_fionity_and_ionity.wav"
       }
     }
    
  4. 将“fionity”和“ionity”语句添加到自定义类:

    curl -X PATCH -H "Authorization: Bearer $(gcloud auth
    --impersonate-service-account=$SA_EMAIL print-access-token)" -H
    "Content-Type: application/json; charset=utf-8"
    "https://speech.googleapis.com/v1p1beta1/projects/project_id/locations/global/customClasses/test-custom-class-1?updateMask=items"
    -d '{"items": [{"value": "ionity"}, {"value": "fionity"}]}'
    

    这会自定义类将更新为以下内容:

    {
       "items":[
          {
             "value":"ionity"
          },
          {
             "value":"fionity"
          }
       ]
    }
    
  5. 再次识别示例音频,这次“fionity”和“ionity”在 CustomClass 中。现在,转录的结果是正确的:“call me fionity and oh my gosh what do we have here ionity”

    curl -H "Authorization: Bearer $(gcloud auth
    --impersonate-service-account=$SA_EMAIL print-access-token)" -H
    "Content-Type: application/json; charset=utf-8"
    "https://speech.googleapis.com/v1p1beta1/speech:recognize" -d '{"config":
    {"adaptation": {"phraseSets": [{"phrases": [{"value":
    "${projects/project_id/locations/global/customClasses/test-custom-class-1}",
    "boost": "10"}]}]}, "languageCode": "en-US"}, "audio":
    {"uri":"gs://biasing-resources-test-audio/call_me_fionity_and_ionity.wav"}}'
    

    示例请求:

    {
       "config":{
          "adaptation":{
             "phraseSets":[
                {
                   "phrases":[
                      {
    "value":"${projects/project_id/locations/global/customClasses/test-custom-class-1}",
                         "boost":"10"
                      }
                   ]
                }
             ]
          },
          "languageCode":"en-US"
       },
       "audio":{
          "uri":"gs://biasing-resources-test-audio/call_me_fionity_and_ionity.wav"
       }
    }
    

请参阅 PhraseSet 中的 CustomClass

  1. 更新之前创建的 PhraseSet 资源以引用 CustomClass

    curl -X PATCH -H "Authorization: Bearer $(gcloud auth
    --impersonate-service-account=$SA_EMAIL print-access-token)" -H
    "Content-Type: application/json; charset=utf-8"
    "https://speech.googleapis.com/v1p1beta1/projects/project_id/locations/global/phraseSets/test-phrase-set-1?updateMask=phrases"
    -d '{"phrases": [{"value": "${projects/project_id/locations/global/customClasses/test-custom-class-1}", "boost": 10}]}'
    

    示例请求:

    {
       "config":{
          "adaptation":{
             "phraseSets":[
                {
                   "phrases":[
                      {
                         "value":"${projects/project_id/locations/global/customClasses/test-custom-class-1}",
                         "boost":"10"
                      }
                   ]
                }
             ]
          },
          "languageCode":"en-US"
       },
       "audio":{
          "uri":"gs://biasing-resources-test-audio/call_me_fionity_and_ionity.wav"
       }
    }
    
  2. 使用 PhraseSet 资源(其引用 CustomClass)识别音频。转写正确:“call me fionity and oh my gosh what do we have here ionity”。

    curl -H "Authorization: Bearer $(gcloud auth
    --impersonate-service-account=$SA_EMAIL print-access-token)" -H
    "Content-Type: application/json; charset=utf-8"
    "https://speech.googleapis.com/v1p1beta1/speech:recognize" -d '{"config":
    {"adaptation": {"phrase_set_references":
    ["projects/project_id/locations/global/phraseSets/test-phrase-set-1"]},
    "languageCode": "en-US"}, "audio":
    {"uri":"gs://biasing-resources-test-audio/call_me_fionity_and_ionity.wav"}}'
    

    示例请求:

    {
       "phrases":[
          {
             "value":"${projects/project_id/locations/global/customClasses/test-custom-class-1}",
             "boost":10
          }
       ]
    }
    

删除 CustomClassPhraseSet

  1. 删除 PhraseSet

    curl -X DELETE -H "Authorization: Bearer $(gcloud auth
    --impersonate-service-account=$SA_EMAIL print-access-token)" -H
    "Content-Type: application/json; charset=utf-8"
    "https://speech.googleapis.com/v1p1beta1/projects/project_id/locations/global/phraseSets/test-phrase-set-1"
    
  2. 删除 CustomClass

    curl -X DELETE -H "Authorization: Bearer $(gcloud auth
    --impersonate-service-account=$SA_EMAIL print-access-token)" -H
    "Content-Type: application/json; charset=utf-8"
    "https://speech.googleapis.com/v1p1beta1/projects/project_id/locations/global/customClasses/test-custom-class-1"
    

使用 ABNF Grammar 提高转写结果

  1. 使用 abnf_grammar 识别音频。此示例引用了 CustomClass 资源:projects/project_id/locations/global/customClasses/test-custom-class-1、内嵌 CustomClass:test-custom-class-2、类词元:ADDRESSNUM 和 PhraseSet 资源:projects/project_id/locations/global/phraseSets/test-phrase-set-1。字符串中的第一条规则(在外部声明之后)将被视为根。

    示例请求:

    {
       "config":{
          "adaptation":{
             "abnf_grammar":{
                "abnf_strings": [
                  "external ${projects/project_id/locations/global/phraseSets/test-phrase-set-1}" ,
                  "external ${projects/project_id/locations/global/customClasses/test-custom-class-1}" ,
                  "external ${test-custom-class-2}" ,
                  "external $ADDRESSNUM" ,
                  "$root = $test-phrase-set-1 $name lives in $ADDRESSNUM;" ,
                  "$name = $title $test-custom-class-1 $test-custom-class-2" ,
                  "$title = Mr | Mrs | Miss | Dr | Prof ;"
                ]
             }
          }
       }
    }
    

后续步骤