LoginSignup
2
0

More than 1 year has passed since last update.

Azure Cognitive Services の Text Analytics を日本語でやってみた

Posted at

背景と目的

Azure Cognitive Services の Text Analytics のクイックスタートの例が英語なので、日本語で試してみました。

前提条件

検証コマンドの実施環境は、Mac + Azure CLI です。

bash
$ sw_vers
ProductName:    macOS
ProductVersion: 11.4
BuildVersion:   20F71

$ az version
{
  "azure-cli": "2.25.0",
  "azure-cli-core": "2.25.0",
  "azure-cli-telemetry": "1.0.6",
  "extensions": {}
}

実施内容

bash
# 環境変数を設定します
region=japaneast
prefix=mnrcogta

# リソースグループを作成します
az group create \
  --name ${prefix}-rg \
  --location $region

# コグニティブサービスを作成します
az cognitiveservices account create \
  --name ${prefix}-ta \
  --resource-group ${prefix}-rg \
  --kind TextAnalytics \
  --sku F0 \
  --location $region \
  --yes

# エンドポイントを取得します
endpoint=$(az cognitiveservices account show \
  --name ${prefix}-ta \
  --resource-group ${prefix}-rg \
  --query properties.endpoint \
  --output tsv)

# リソースキーを取得します
key=$(az cognitiveservices account keys list \
  --name ${prefix}-ta \
  --resource-group ${prefix}-rg \
  --query key1 \
  --output tsv)

実施結果

感情分析: Sentiment Analysis, 言語検出: Language detection, 名前付きエンティティの認識: Named Entity Recognition (NER), エンティティリンク設定: Entity linking, キーフレーズの抽出: Key phrase extraction を日本語で試してみました。

bash
# 感情分析を試します
curl -s ${endpoint}text/analytics/v3.0/sentiment/ \
  -H "Content-Type: application/json" \
  -H "Ocp-Apim-Subscription-Key: $key" \
  -d '{ documents: [{ "language": "ja", "id": "1", "text": "私は人生で最高の日を過ごしました。あなたが私と一緒にいたらいいのにと思います。"}]}' \
  | jq .
{
  "documents": [
    {
      "id": "1",
      "sentiment": "positive",
      "confidenceScores": {
        "positive": 0.61,
        "neutral": 0.37,
        "negative": 0.02
      },
      "sentences": [
        {
          "sentiment": "positive",
          "confidenceScores": {
            "positive": 0.61,
            "neutral": 0.39,
            "negative": 0
          },
          "offset": 0,
          "length": 17,
          "text": "私は人生で最高の日を過ごしました。"
        },
        {
          "sentiment": "positive",
          "confidenceScores": {
            "positive": 0.6,
            "neutral": 0.36,
            "negative": 0.04
          },
          "offset": 17,
          "length": 22,
          "text": "あなたが私と一緒にいたらいいのにと思います。"
        }
      ],
      "warnings": []
    }
  ],
  "errors": [],
  "modelVersion": "2020-04-01"
}

# 言語検出を試します
curl -s ${endpoint}text/analytics/v3.0/languages/ \
  -H "Content-Type: application/json" \
  -H "Ocp-Apim-Subscription-Key: $key" \
  -d '{ documents: [{ "id": "1", "text": "これは日本語で書かれた文書です。"}]}' \
  | jq .
{
  "documents": [
    {
      "id": "1",
      "detectedLanguage": {
        "name": "Japanese",
        "iso6391Name": "ja",
        "confidenceScore": 1
      },
      "warnings": []
    }
  ],
  "errors": [],
  "modelVersion": "2021-01-05"
}

# 名前付きエンティティの認識 (NER) を試します
curl -s ${endpoint}text/analytics/v3.0/entities/recognition/general \
  -H "Content-Type: application/json" \
  -H "Ocp-Apim-Subscription-Key: $key" \
  -d '{ documents: [{ "id": "1", "language": "ja", "text": "私は先週シアトルに素晴らしい旅行をしました。"}]}' \
  | jq .
{
  "documents": [
    {
      "id": "1",
      "entities": [
        {
          "text": "週",
          "category": "Quantity",
          "subcategory": "Age",
          "offset": 3,
          "length": 1,
          "confidenceScore": 0.8
        },
        {
          "text": "シアトル",
          "category": "Location",
          "subcategory": "GPE",
          "offset": 4,
          "length": 4,
          "confidenceScore": 0.99
        },
        {
          "text": "旅行",
          "category": "Event",
          "offset": 14,
          "length": 2,
          "confidenceScore": 0.71
        }
      ],
      "warnings": []
    }
  ],
  "errors": [],
  "modelVersion": "2021-06-01"
}

# エンティティリンク設定を試します(現時点では日本語はサポートされていないようです)
curl -s ${endpoint}text/analytics/v3.0/entities/linking \
  -H "Content-Type: application/json" \
  -H "Ocp-Apim-Subscription-Key: $key" \
  -d '{ documents: [{ "id": "1", "language": "ja", "text": "マイクロソフトは、1975年4月4日にビルゲイツとポールアレンによって設立されました。"}]}' \
  | jq .
{
  "documents": [],
  "errors": [
    {
      "id": "1",
      "error": {
        "code": "InvalidArgument",
        "message": "Invalid Language Code.",
        "innererror": {
          "code": "UnsupportedLanguageCode",
          "message": "Invalid language code. Supported languages: en,es. For additional details see https://aka.ms/text-analytics/language-support?tabs=named-entity-recognition"
        }
      }
    }
  ],
  "modelVersion": "2021-06-01"
}

# キーフレーズの抽出を試します
curl -s ${endpoint}text/analytics/v3.0/keyPhrases \
  -H "Content-Type: application/json" \
  -H "Ocp-Apim-Subscription-Key: $key" \
  -d '{ documents: [{ "id": "1", "language": "ja", "text": "私は先週シアトルに素晴らしい旅行をしました。"}]}' \
  | jq .
{
  "documents": [
    {
      "id": "1",
      "keyPhrases": [
        "先週シアトル",
        "素晴らしい旅行"
      ],
      "warnings": []
    }
  ],
  "errors": [],
  "modelVersion": "2021-06-01"
}

参考

bash
# 同じリソース名でコグニティブサービスを作成したい場合は明示的に削除します
az resource delete \
  --ids /subscriptions/$(az account show --query id --output tsv)/providers/Microsoft.CognitiveServices/locations/$region/resourceGroups/${prefix}-rg/deletedAccounts/${prefix}-ta

# リソースグループを削除します
az group delete \
  --name ${prefix}-rg

クイックスタート: Azure コマンド ライン インターフェイス (CLI) を使用して Cognitive Services リソースを作成する

クイックスタート: Text Analytics クライアント ライブラリおよび REST API を使用する

Text Analytics API (v3.0)

Text Analytics API v3 language support

2
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
2
0