0
0

[GCP] Natural Language AI 機能x実装

Posted at

GCPのNatural Language AIの概要

GCP(Google Cloud Platform)のNatural Language AIは、テキストデータを処理するための機能を提供するサービスです。このサービスを使用することで、テキストデータの理解、感情分析、意図解釈などが可能になります。また、言語の検出やエンティティの抽出などの自然言語処理タスクを自動化することもできます。

機能

GCPのNatural Language AIは、以下のような主な機能を提供しています。

  1. テキスト分類: テキストデータをさまざまなカテゴリに分類することができます。例えば、文書がニュース記事なのか、レビューなのか、それともスパムメッセージなのかを判定することが可能です。

  2. エンティティ抽出: テキストデータから人、組織、場所などの重要なエンティティを抽出することができます。これにより、テキスト内で言及されている重要な要素を特定することができます。

  3. 感情分析: テキストデータの感情を分析することができます。テキストがポジティブ、ネガティブ、またはニュートラルであるかを判定することができます。これにより、顧客のフィードバックやソーシャルメディアの投稿などのテキストデータの感情を把握することが可能です。

  4. 意図解釈: テキストの意図を解釈することができます。ユーザーの発言やクエリを分析し、ユーザーが求めている情報やアクションを理解することができます。この機能は、チャットボットや仮想アシスタントなどのアプリケーションで有用です。

サンプルコード

以下に、Java、Go、C#でNatural Language AIを使用するためのサンプルコードを実装します。

Java

import com.google.cloud.language.v1.Document;
import com.google.cloud.language.v1.Document.Type;
import com.google.cloud.language.v1.LanguageServiceClient;
import com.google.cloud.language.v1.AnalyzeEntitiesRequest;
import com.google.cloud.language.v1.AnalyzeEntitiesResponse;

public class NaturalLanguageAPISample {
  public static void main(String[] args) {
    try (LanguageServiceClient language = LanguageServiceClient.create()) {
      Document doc = Document.newBuilder()
          .setContent("テキストデータの例です。")
          .setType(Type.PLAIN_TEXT)
          .build();

      AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder()
          .setDocument(doc)
          .setEncodingType(EncodingType.UTF8)
          .build();

      AnalyzeEntitiesResponse response = language.analyzeEntities(request);

      for (Entity entity : response.getEntitiesList()) {
        System.out.printf("エンティティ: %s%n", entity.getName());
        System.out.printf("タイプ: %s%n", entity.getType());
        System.out.printf("重要度: %s%n", entity.getSalience());
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Go

import (
	"context"
	"fmt"

	language "cloud.google.com/go/language/apiv1"
	languagepb "google.golang.org/genproto/googleapis/cloud/language/v1"
)

func main() {
	ctx := context.Background()

	client, err := language.NewClient(ctx)
	if err != nil {
		fmt.Printf("Failed to create client: %v", err)
		return
	}

	text := "テキストデータの例です。"

	req := &languagepb.AnalyzeEntitiesRequest{
		Document: &languagepb.Document{
			Source: &languagepb.Document_Content{
				Content: text,
			},
			Type: languagepb.Document_PLAIN_TEXT,
		},
		EncodingType: languagepb.EncodingType_UTF8,
	}

	resp, err := client.AnalyzeEntities(ctx, req)
	if err != nil {
		fmt.Printf("Failed to analyze entities: %v", err)
		return
	}

	for _, entity := range resp.GetEntities() {
		fmt.Printf("エンティティ: %v\n", entity.Name)
		fmt.Printf("タイプ: %v\n", entity.Type)
		fmt.Printf("重要度: %v\n", entity.Salience)
	}
}

C#

using Google.Cloud.Language.V1;
using System;
using System.Collections.Generic;

public class NaturalLanguageAPISample
{
    public void AnalyzeEntities()
    {
        var client = LanguageServiceClient.Create();

        var text = "テキストデータの例です。";

        var response = client.AnalyzeEntities(new Document
        {
            Content = text,
            Type = Document.Types.Type.PlainText
        });

        foreach (var entity in response.Entities)
        {
            Console.WriteLine($"エンティティ: {entity.Name}");
            Console.WriteLine($"タイプ: {entity.Type}");
            Console.WriteLine($"重要度: {entity.Salience}");
        }
    }
}

以上が、GCPのNatural Language AIの概要と機能についての情報と、Java、Go、C#のサンプルコードです。

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