本文介绍了Google Cloud Translation API:创建词汇表错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用词汇表测试Cloud Translation API.因此,我创建了一个示例词汇表文件(.csv)并将其上传到Cloud Storage.但是,当我运行测试代码时(从官方文档中复制示例代码),发生错误.样本词汇表文件中似乎有一个问题,但找不到.

I tried to test Cloud Translation API using glossary.So I created a sample glossary file(.csv) and uploaded it on Cloud Storage.However when I ran my test code (copying sample code from official documentation), an error occurred. It seems that there is a problem in my sample glossary file, but I cannot find it.

我附上了我的代码,错误消息和词汇表文件的屏幕截图.你能告诉我如何解决吗?

I attached my code, error message, and screenshot of the glossary file.Could you please tell me how to fix it?

我可以使用术语表,以便在翻译成另一种语言时使用原始语言吗?

And can I use the glossary so that the original language is used when translated into another language?

  • 例如)将英语翻译成韩语

我想去加利福尼亚. >>>나는加利福尼亚나는싶다.

I want to visit California. >>> 나는 California에 방문하고 싶다.

示例代码

from google.cloud import translate_v3 as translate
import os

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="my_service_account_json_file_path"

def create_glossary(
    project_id="YOUR_PROJECT_ID",
    input_uri="YOUR_INPUT_URI",
    glossary_id="YOUR_GLOSSARY_ID",
):
    """
    Create a equivalent term sets glossary. Glossary can be words or
    short phrases (usually fewer than five words).
    https://cloud.google.com/translate/docs/advanced/glossary#format-glossary
    """
    client = translate.TranslationServiceClient()

    # Supported language codes: https://cloud.google.com/translate/docs/languages
    source_lang_code = "ko"
    target_lang_code = "en"
    location = "us-central1"  # The location of the glossary

    name = client.glossary_path(project_id, location, glossary_id)
    language_codes_set = translate.types.Glossary.LanguageCodesSet(
        language_codes=[source_lang_code, target_lang_code]
    )

    gcs_source = translate.types.GcsSource(input_uri=input_uri)

    input_config = translate.types.GlossaryInputConfig(gcs_source=gcs_source)

    glossary = translate.types.Glossary(
        name=name, language_codes_set=language_codes_set, input_config=input_config
    )

    parent = client.location_path(project_id, location)
    # glossary is a custom dictionary Translation API uses
    # to translate the domain-specific terminology.
    operation = client.create_glossary(parent=parent, glossary=glossary)

    result = operation.result(timeout=90)
    print("Created: {}".format(result.name))
    print("Input Uri: {}".format(result.input_config.gcs_source.input_uri))


create_glossary("my_project_id", "file_path_on_my_cloud_storage_bucket", "test_glossary")

错误消息)

Traceback (most recent call last):
  File "C:/Users/ME/py-test/translation_api_test.py", line 120, in <module>
    create_glossary("my_project_id", "file_path_on_my_cloud_storage_bucket", "test_glossary")
  File "C:/Users/ME/py-test/translation_api_test.py", line 44, in create_glossary
    result = operation.result(timeout=90)
  File "C:\Users\ME\py-test\venv\lib\site-packages\google\api_core\future\polling.py", line 127, in result
    raise self._exception
google.api_core.exceptions.GoogleAPICallError: None No glossary entries found in input files. Check your files are not empty. stats = {total_examples = 0, total_successful_examples = 0, total_errors = 3, total_ignored_errors = 3, total_source_text_bytes = 0, total_target_text_bytes = 0, total_text_bytes = 0, text_bytes_by_language_map = []}

词汇表文件

https://drive.google.com/file/d/1RaladmLjgygai3XsZv3Ez4ij5uDH5EdE/view?usp = sharing

推荐答案

我通过将词汇表文件的编码更改为UTF-8解决了我的问题.而且我还发现我可以使用词汇表,以便在翻译成另一种语言时使用原始语言.

I solved my problem by changing encoding of the glossary file to UTF-8.And I also found that I can use the glossary so that the original language is used when translated into another language.

这篇关于Google Cloud Translation API:创建词汇表错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-30 10:13