コルネの進捗や備忘録が記されたなにか

進捗や成果物や備忘録てきななにかを雑に更新していきます。

Azure Cognitive Service for Languageの感情分析機能を利用してみよう


スポンサードリンク

はじめに

AzureではAzure Cognitive Servicesという、最近流行りのAIをデータサイエンスのスキルや知識がなくとも利用可能なクラウドサービスが提供されています。

learn.microsoft.com

このCognitive Servicesでは現在、主に以下5つのサービスが展開されています。

  • Vision
  • Speech
  • Language
  • Decision
  • Azure OpenAI Service

今回はこれらのうち"Language"機能の1つである「感情分析」を利用していきます。

learn.microsoft.com

Azure Cognitive Service for Languageを利用する

<参考>

learn.microsoft.com

Azure上にリソースを作成する

  1. Azure portalにアクセスして、Cognitive Servicesを開きます。
  2. 言語リソースの作成を選択してリソースの作成を行います。
    既定では以下機能が現在使用可能ですね。
  3. 必要項目を入力のうえ、「責任ある AI の通知」条項に同意して作成します。
  4. 作成完了です。

次にリソースに移動してキーとエンドポイントを取得しましょう。

キーとエンドポイントの取得

  1. デプロイされたリソースに移動して「キーとエンドポイント」を開きます。
  2. キーとエンドポイントを控えます。

Power AutomateでAzure Cognitive Service for Languageを利用する

接続の作成

Azure Cognitive Service for Languageのアクションのいずれかを追加するか、接続から"Azure Cognitive Service for Language"を選択して接続の作成を行います。

先ほど作成したキーとエンドポイントを利用して接続の作成を行います。

プロパティ名 設定する値
認証の種類 API キー
アカウント キー キー 1
サイトの URL エンドポイント

アクションの作成

感情の分析を行うアクションは「センチメント」アクションなので、以下アクションのいずれかを追加します。

今回はプレビューでないV3.0の方を利用したいと思います。

learn.microsoft.com

以下のように設定します。
言語は既定では英語になっているので、日本語を利用する場合は ja と設定してあげてください。

感情分析に利用する文章は以下のようなポジティブとネガティブな内容が混在したようなメッセージを投げてみます。

今日はいい天気ですが、私は体調不良で寝込んでいます。

実行すると、以下のような実行結果が得られました。

{
  "statusCode": 200,
  "body": {
    "statistics": {
      "documentsCount": 1,
      "validDocumentsCount": 1,
      "erroneousDocumentsCount": 0,
      "transactionsCount": 1
    },
    "documents": [
      {
        "id": "aa0f55fb-a40e-4e38-81c2-8d799ab7896b",
        "sentiment": "negative",
        "statistics": {
          "charactersCount": 26,
          "transactionsCount": 1
        },
        "confidenceScores": {
          "positive": 0.1,
          "neutral": 0.18,
          "negative": 0.73
        },
        "sentences": [
          {
            "sentiment": "negative",
            "confidenceScores": {
              "positive": 0.1,
              "neutral": 0.18,
              "negative": 0.73
            },
            "offset": 0,
            "length": 26,
            "text": "今日はいい天気ですが、私は体調不良で寝込んでいます。"
          }
        ],
        "warnings": []
      }
    ],
    "errors": [],
    "modelVersion": "2022-11-01"
  }
}

"sentiment": "negative" という結果より、ネガティブな結果だと分析されましたね!

ではポジティブな文章も投げてみましょうか。

飲みに行きたい!串カツが食べたい!!

実行結果は以下。

{
  "statusCode": 200,
  "body": {
    "statistics": {
      "documentsCount": 1,
      "validDocumentsCount": 1,
      "erroneousDocumentsCount": 0,
      "transactionsCount": 1
    },
    "documents": [
      {
        "id": "5e656390-eb06-4086-8a92-42884e0ff4fb",
        "sentiment": "positive",
        "statistics": {
          "charactersCount": 18,
          "transactionsCount": 1
        },
        "confidenceScores": {
          "positive": 0.72,
          "neutral": 0.24,
          "negative": 0.03
        },
        "sentences": [
          {
            "sentiment": "positive",
            "confidenceScores": {
              "positive": 0.85,
              "neutral": 0.13,
              "negative": 0.02
            },
            "offset": 0,
            "length": 8,
            "text": "飲みに行きたい!"
          },
          {
            "sentiment": "positive",
            "confidenceScores": {
              "positive": 0.6,
              "neutral": 0.36,
              "negative": 0.04
            },
            "offset": 8,
            "length": 10,
            "text": "串カツが食べたい!!"
          }
        ],
        "warnings": []
      }
    ],
    "errors": [],
    "modelVersion": "2022-11-01"
  }
}

ポジティブ判定を受けましたね!

余談

旧text Analytics(テキスト分析)アクションがこちらのアクションに置き換わったわけですが、当時この分析では "!" をたくさんつければポジティブ判定になってしまう。
といったようなことがあったかと思います。

これを「センチメント (V3.0)」でも試してみました。

試した文章は

働きたくない

働きたくない!!!!!

です。

それぞれ実行結果は以下のようになりました。

働きたくない

[
  {
    "id": "20706dd4-8b54-43d0-85b7-c3ae4c4aeb55",
    "sentiment": "negative",
    "statistics": {
      "charactersCount": 6,
      "transactionsCount": 1
    },
    "confidenceScores": {
      "positive": 0.01,
      "neutral": 0.02,
      "negative": 0.97
    },
    "sentences": [
      {
        "sentiment": "negative",
        "confidenceScores": {
          "positive": 0.01,
          "neutral": 0.02,
          "negative": 0.97
        },
        "offset": 0,
        "length": 6,
        "text": "働きたくない"
      }
    ],
    "warnings": []
  }
]

働きたくない!!!!!

[
  {
    "id": "055e1791-68a3-44f1-8512-4d27f7f96179",
    "sentiment": "negative",
    "statistics": {
      "charactersCount": 11,
      "transactionsCount": 1
    },
    "confidenceScores": {
      "positive": 0,
      "neutral": 0.03,
      "negative": 0.97
    },
    "sentences": [
      {
        "sentiment": "negative",
        "confidenceScores": {
          "positive": 0,
          "neutral": 0.03,
          "negative": 0.97
        },
        "offset": 0,
        "length": 11,
        "text": "働きたくない!!!!!"
      }
    ],
    "warnings": []
  }
]

どちらもちゃんとネガティブ判定されていますね!

おわりに

これだけの簡単な操作でテキスト解析ができてしまいます。

Azure Cognitive Serviceにはまだまだたくさんの機能がありますので、これらを活用することでよりアプリ/フローの作成の幅が広がりますね!


スポンサードリンク