【问题标题】:Google Cloud Translation API: Creating glossary errorGoogle Cloud Translation API:创建词汇表错误
【发布时间】:2020-06-25 15:59:38
【问题描述】:

我尝试使用词汇表测试 Cloud Translation API。 因此,我创建了一个示例词汇表文件 (.csv) 并将其上传到 Cloud Storage。 但是,当我运行我的测试代码 (copying sample code from official documentation) 时,发生了错误。我的示例词汇表文件似乎有问题,但我找不到。

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

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

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

我想去加利福尼亚。 >>> 나는 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

【问题讨论】:

    标签: google-api google-translate google-api-python-client glossary


    【解决方案1】:

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

    【讨论】:

    • @Drake 请说明您如何以及在何处(在哪个代码行)将词汇表文件的编码更改为 UTF-8?
    • @Spark 它不在代码中,您需要在上传之前检查 CSV 文件的编码(例如使用 Notepad++)。我的是 UTF-8-BOM。错误是不同的,它找不到 de 语言。
    猜你喜欢
    • 2020-11-28
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 2014-02-08
    • 2018-03-24
    • 2018-11-07
    相关资源
    最近更新 更多