检测语言(高级版)

本文档介绍了如何使用 Cloud Translation 高级版检测字符串的语言。

准备工作

在开始使用 Cloud Translation API 之前,您必须有一个启用了 Cloud Translation API 的项目,以及相应的凭据。您还可以安装常用编程语言的客户端库,以便调用 API。

如需了解详情,请参阅设置页面。

检测文本字符串的语言

要检测文本字符串的语言,使用以下格式的网址发送 HTTP 请求即可:

https://translation.googleapis.com/v3/projects/project-number-or-id/locations/global:detectLanguage

检测单个字符串的语言

REST 和命令行

要检测部分文本的语言,请发出 POST 请求并提供相应的请求正文。下面显示了使用 curl 和 PowerShell 的 POST 请求的示例。该示例使用通过 Google Cloud Cloud SDK 为项目设置的服务帐号的访问令牌。如需了解有关安装 Cloud SDK、使用服务帐号设置项目以及获取访问令牌的说明,请参阅设置页面。

在使用下面的请求数据之前,请先进行以下替换:

  • project-number-or-id:您的 Google Cloud 项目编号或 ID

HTTP 方法和网址:

POST https://translation.googleapis.com/v3/projects/project-number-or-id/locations/global:detectLanguage

请求 JSON 正文:

{
   "content":"Доктор Ватсон, иди сюда!"
}

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
https://translation.googleapis.com/v3/projects/project-number-or-id/locations/global:detectLanguage

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://translation.googleapis.com/v3/projects/project-number-or-id/locations/global:detectLanguage " | Select-Object -Expand Content

您应该收到类似以下内容的 JSON 响应:

{
  "languages": [
    {
      "languageCode": "ru",
      "confidence": 1
    }
  ]
}
在响应中,languageCode 提供检测到的语言的代码。confidence 的范围从 0 到 1。1 表示置信度为 100%。

C#

在试用此示例之前,请按照《Translation 快速入门:使用客户端库》中的 C# 设置说明进行操作。如需了解详情,请参阅 Translation C# API 参考文档


using Google.Api.Gax.ResourceNames;
using Google.Cloud.Translate.V3;
using System;

namespace GoogleCloudSamples
{
    public static class DetectLanguage
    {
        /// <summary>
        /// Detects the language of a given text.
        /// </summary>
        /// <param name="text">The text string for performing language detection</param>
        /// <param name="projectId">Your Google Cloud Project ID.</param>
        public static void DetectLanguageSample(string text = "[TEXT_STRING_FOR_DETECTION]",
            string projectId = "[Google Cloud Project ID]")
        {
            TranslationServiceClient translationServiceClient = TranslationServiceClient.Create();
            DetectLanguageRequest request = new DetectLanguageRequest
            {
                ParentAsLocationName = new LocationName(projectId, "global"),
                Content = text,
                MimeType = "text/plain",
            };
            DetectLanguageResponse response = translationServiceClient.DetectLanguage(request);
            // Display list of detected languages sorted by detection confidence.
            // The most probable language is first.
            foreach (DetectedLanguage language in response.Languages)
            {
                // The language detected
                Console.WriteLine($"Language code: {language.LanguageCode}");
                // Confidence of detection result for this language
                Console.WriteLine($"Confidence: {language.Confidence}");
            }
        }
    }

Go

在试用此示例之前,请按照《Translation 快速入门:使用客户端库》中的 Go 设置说明进行操作。如需了解详情,请参阅 Translation Go API 参考文档

import (
	"context"
	"fmt"
	"io"

	translate "cloud.google.com/go/translate/apiv3"
	translatepb "google.golang.org/genproto/googleapis/cloud/translate/v3"
)

// detectLanguage detects the language of a text string.
func detectLanguage(w io.Writer, projectID string, text string) error {
	// projectID := "my-project-id"
	// text := "Hello, world!"

	ctx := context.Background()
	client, err := translate.NewTranslationClient(ctx)
	if err != nil {
		return fmt.Errorf("NewTranslationClient: %v", err)
	}
	defer client.Close()

	req := &translatepb.DetectLanguageRequest{
		Parent:   fmt.Sprintf("projects/%s/locations/global", projectID),
		MimeType: "text/plain", // Mime types: "text/plain", "text/html"
		Source: &translatepb.DetectLanguageRequest_Content{
			Content: text,
		},
	}

	resp, err := client.DetectLanguage(ctx, req)
	if err != nil {
		return fmt.Errorf("DetectLanguage: %v", err)
	}

	// Display list of detected languages sorted by detection confidence.
	// The most probable language is first.
	for _, language := range resp.GetLanguages() {
		// The language detected.
		fmt.Fprintf(w, "Language code: %v\n", language.GetLanguageCode())
		// Confidence of detection result for this language.
		fmt.Fprintf(w, "Confidence: %v\n", language.GetConfidence())
	}

	return nil
}

Java

在试用此示例之前,请按照 《Translation 快速入门:使用客户端库》 中的 Java 设置说明进行操作。如需了解详情,请参阅 Translation Java API 参考文档

import com.google.cloud.translate.v3.DetectLanguageRequest;
import com.google.cloud.translate.v3.DetectLanguageResponse;
import com.google.cloud.translate.v3.DetectedLanguage;
import com.google.cloud.translate.v3.LocationName;
import com.google.cloud.translate.v3.TranslationServiceClient;
import java.io.IOException;

public class DetectLanguage {

  public static void detectLanguage() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "YOUR-PROJECT-ID";
    String text = "your-text";

    detectLanguage(projectId, text);
  }

  // Detecting the language of a text string
  public static void detectLanguage(String projectId, String text) throws IOException {

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TranslationServiceClient client = TranslationServiceClient.create()) {
      // Supported Locations: `global`, [glossary location], or [model location]
      // Glossaries must be hosted in `us-central1`
      // Custom Models must use the same location as your model. (us-central1)
      LocationName parent = LocationName.of(projectId, "global");

      // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
      DetectLanguageRequest request =
          DetectLanguageRequest.newBuilder()
              .setParent(parent.toString())
              .setMimeType("text/plain")
              .setContent(text)
              .build();

      DetectLanguageResponse response = client.detectLanguage(request);

      // Display list of detected languages sorted by detection confidence.
      // The most probable language is first.
      for (DetectedLanguage language : response.getLanguagesList()) {
        // The language detected
        System.out.printf("Language code: %s\n", language.getLanguageCode());
        // Confidence of detection result for this language
        System.out.printf("Confidence: %s\n", language.getConfidence());
      }
    }
  }
}

Node.js

在试用此示例之前,请按照《Translation 快速入门:使用客户端库》中的 Node.js 设置说明进行操作。如需了解详情,请参阅 Translation Node.js API 参考文档

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'global';
// const text = 'text to translate';

// Imports the Google Cloud Translation library
const {TranslationServiceClient} = require('@google-cloud/translate');

// Instantiates a client
const translationClient = new TranslationServiceClient();

async function detectLanguage() {
  // Construct request
  const request = {
    parent: `projects/${projectId}/locations/${location}`,
    content: text,
  };

  try {
    // Run request
    const [response] = await translationClient.detectLanguage(request);

    console.log('Detected Languages:');
    for (const language of response.languages) {
      console.log(`Language Code: ${language.languageCode}`);
      console.log(`Confidence: ${language.confidence}`);
    }
  } catch (error) {
    console.error(error.details);
  }
}

detectLanguage();

PHP

在试用此示例之前,请按照《Translation 快速入门:使用客户端库》中的 PHP 设置说明进行操作。如需了解详情,请参阅 Translation PHP API 参考文档

use Google\Cloud\Translate\V3\TranslationServiceClient;

$translationServiceClient = new TranslationServiceClient();

/** Uncomment and populate these variables in your code */
// $text = 'Hello, world!';
// $projectId = '[Google Cloud Project ID]';
$formattedParent = $translationServiceClient->locationName($projectId, 'global');

// Optional. Can be "text/plain" or "text/html".
$mimeType = 'text/plain';

try {
    $response = $translationServiceClient->detectLanguage(
        $formattedParent,
        [
            'content' => $text,
            'mimeType' => $mimeType
        ]
    );
    // Display list of detected languages sorted by detection confidence.
    // The most probable language is first.
    foreach ($response->getLanguages() as $language) {
        // The language detected
        printf('Language code: %s' . PHP_EOL, $language->getLanguageCode());
        // Confidence of detection result for this language
        printf('Confidence: %s' . PHP_EOL, $language->getConfidence());
    }
} finally {
    $translationServiceClient->close();
}

Python

在试用此示例之前,请按照《Translation 快速入门:使用客户端库》中的 Python 设置说明进行操作。如需了解详情,请参阅 Translation Python API 参考文档

from google.cloud import translate

def detect_language(project_id="YOUR_PROJECT_ID"):
    """Detecting the language of a text string."""

    client = translate.TranslationServiceClient()

    location = "global"

    parent = f"projects/{project_id}/locations/{location}"

    # Detail on supported types can be found here:
    # https://cloud.google.com/translate/docs/supported-formats
    response = client.detect_language(
        content="Hello, world!",
        parent=parent,
        mime_type="text/plain",  # mime types: text/plain, text/html
    )

    # Display list of detected languages sorted by detection confidence.
    # The most probable language is first.
    for language in response.languages:
        # The language detected
        print("Language code: {}".format(language.language_code))
        # Confidence of detection result for this language
        print("Confidence: {}".format(language.confidence))

Ruby

在试用此示例之前,请按照《Translation 快速入门:使用客户端库》中的 Ruby 设置说明进行操作。如需了解详情,请参阅 Translation Ruby API 参考文档

require "google/cloud/translate"

# project_id = "[Google Cloud Project ID]"
# location_id = "[LOCATION ID]"

client = Google::Cloud::Translate.translation_service

# The text string for performing language detection
content = "Hello, world!"
# Optional. Can be "text/plain" or "text/html".
mime_type = "text/plain"

parent = client.location_path project: project_id, location: location_id

response = client.detect_language parent:    parent,
                                  content:   content,
                                  mime_type: mime_type

# Display list of detected languages sorted by detection confidence.
# The most probable language is first.
response.languages.each do |language|
  # The language detected
  puts "Language Code: #{language.language_code}"
  # Confidence of detection result for this language
  puts "Confidence: #{language.confidence}"
end