Google Cloud Translation API: Creating glossary error

荒凉一梦 提交于 2021-01-28 22:07:36

问题


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?

  • Ex) Translation English to Korean

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

Sample Code)

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")

Error Message)

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 = []}

Glossary File)

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


回答1:


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.



来源:https://stackoverflow.com/questions/60673798/google-cloud-translation-api-creating-glossary-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!