0
0
音声認識APIを使ってみよう!

[Google Cloud Speech-to-Text API(V2)] Javaで認識精度を向上させるカスタムクラスを取得する

Posted at

Cloud Speech-to-Text API V2(Java)を使用して、
認識精度を向上させるカスタムクラスを取得する方法についてご紹介します。

カスタムクラス(CustomClass)とは
カスタムクラス(CustomClass)はモデル適応機能を実現させるためのリソースの1つです。

モデル適応機能を使用すると提案される可能性がある他の候補よりも、
Speech-to-Textが特定の単語やフレーズをより高い頻度で認識するように設定できます。

カスタムクラス(CustomClass)リソースでは下記が設定できます。

できること 概要
固有名称(店名や商品名など)の認識 カスタムクラスを使用すると、音声に出現した際に固有名称を正しく認識するように認識モデルにバイアスをかけることができます。

APIを利用する環境の準備から始める場合や、コードを実行する際は、
⧉[Google Cloud Speech-to-Text API(V2)] JavaでSpeech-to-Text APIを使ってみる
を参照ください。

No 目次
1 カスタムクラス一覧を取得
1 スコープ
2 実行
3 レスポンスの内容
2 カスタムクラスを取得
1 スコープ
2 実行
3 レスポンスの内容

1. カスタムクラス一覧を取得

カスタムクラス一覧を取得します。

1.1. スコープ

OAuth2.0でこのAPIを実行するには、以下のスコープを指定してください。
サービスアカウントで実行する場合はスコープの指定は不要です。

https://www.googleapis.com/auth/cloud-platform

1.2. 実行

public static void main(String[] args) throws Exception{
    try(SpeechClient client = getSpeechClient()){
        ListCustomClassesRequest.Builder builder = ListCustomClassesRequest.newBuilder();
        builder.setParent(String.format("projects/%s/locations/%s", "プロジェクトID","ロケーションID"));
        
        SpeechClient.ListCustomClassesPagedResponse response = client.listCustomClasses(builder.build());
        for(CustomClass customClass : response.iterateAll()) {
            System.out.println(customClass);
        }
        System.out.println(response.getNextPageToken());
    }
}

1.2.1. HTTPリクエスト

GET: https://speech.googleapis.com/v2/projects/{プロジェクトID}/locations/{ロケーションID}/customClasses
が実行されます。

1.2.2. クエリパラメータ

ListCustomClassesRequest.Builderのsetメソッドにより、リクエストボディを追加できます。
build()メソッドでListCustomClassesRequestインスタンスを取得し、
listCustomClasses()に渡します。

メソッド 引数 説明
setParent String 【必須】リソース名
形式:"projects/{プロジェクトID}/locations/{ロケーションID}"
setPageSize int 取得するカスタムクラスの最大数(最大値100)
setPageToken String ページトークン
setShowDeleted boolean 削除されたリソースを表示するか

1.3. レスポンスの内容

SpeechClient.ListCustomClassesPagedResponse

メソッド 戻り値 説明
iterateAll Iterable<CustomClass> カスタムクラスのリスト
getNextPageToken String 次ページのトークン

CustomClass

メソッド 戻り値 説明
getName String カスタムクラスのリソース名
形式:"projects/{プロジェクトID}/locations/{ロケーションID}/customClasses/{カスタムクラスID}"
getUid String システムによって割り当てられたカスタムクラスID
getDisplayName String カスタムクラスの名前(63文字まで)
getItemsList List<ClassItem> カスタムクラスのリスト
getState State(enum) カスタムクラスのライフサイクル状態
getCreateTime Timestamp カスタムクラスの作成時間
getUpdateTime Timestamp カスタムクラスが最後に変更された時刻
getDeleteTime Timestamp カスタムクラスの削除が要求された時刻
getExpireTime Timestamp カスタムクラスがパージされる時刻
getEtag String チェックサム値
getReconciling boolean カスタムクラスが更新中か
getKmsKeyName String カスタムクラスの暗号化に使用されるKMSキーの名前
形式:"projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{crypto_key}"
getKmsKeyVersionName String カスタムクラスの暗号化に使用されるKMSキーのバージョン名
形式:"projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{crypto_key}/cryptoKeyVersions/{crypto_key_version}"

ClassItem

メソッド 戻り値 説明
getValue String クラス項目の値

State

定義値 内容
STATE_UNSPECIFIED 未指定
ACTIVE 通常のアクティブな状態
DELETED カスタムクラスは削除された

2. カスタムクラスを取得

指定したカスタムクラスの情報を取得します。

2.1. スコープ

OAuth2.0でこのAPIを実行するには、以下のスコープを指定してください。
サービスアカウントで実行する場合はスコープの指定は不要です。

https://www.googleapis.com/auth/cloud-platform

2.2. 実行

public static void main(String[] args) throws Exception{
    try(SpeechClient client = getSpeechClient()){
        GetCustomClassRequest.Builder builder = GetCustomClassRequest.newBuilder();
        builder.setName(String.format("projects/%s/locations/%s/customClasses/%s", "プロジェクトID","ロケーションID","カスタムクラスID"));
        
        CustomClass response = client.getCustomClass(builder.build());
        System.out.println(response);
    }
}

2.2.1. HTTPリクエスト

GET: https://speech.googleapis.com/v2/projects/{プロジェクトID}/locations/{ロケーションID}/customClasses/{カスタムクラスID}
が実行されます。

2.2.2. クエリパラメータ

クエリパラメータはありません。

2.3. レスポンスの内容

CustomClass

1.3. レスポンスの内容(CustomClass)と同じです。



おしまい。。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0