适用于 Python 的 Azure 文本翻译客户端库 您所在的位置:网站首页 count造句并翻译 适用于 Python 的 Azure 文本翻译客户端库

适用于 Python 的 Azure 文本翻译客户端库

#适用于 Python 的 Azure 文本翻译客户端库 | 来源: 网络整理| 查看: 265

你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。

适用于 Python 的 Azure 文本翻译客户端库 - 版本 1.0.0b1 项目 04/27/2023

文本翻译是翻译器服务的基于云的 REST API 功能,它使用神经机器翻译技术,在所有受支持的语言中实时实现快速准确的源到目标文本翻译。

使用适用于 Python 的文本翻译客户端库可以:

返回 Translate、Transliterate 和 Dictionary 操作支持的语言列表。

使用单个请求将单个源语言文本呈现为多个目标语言文本。

将源语言的文本转换为不同脚本的字母。

返回目标语言中源术语的等效字词。

返回源术语和目标术语对的语法结构和上下文示例。

源代码 | 包 (PyPI) | API 参考文档 | 产品文档 | 样品

入门 先决条件 使用此包需要 Python 3.7 或更高版本。 现有的翻译器服务或认知服务资源。 安装包

使用 pip 安装适用于 Python 的 Azure 文本翻译客户端库:

pip install azure-ai-translation-text 创建翻译器服务资源

可以在创建翻译器资源后 创建翻译器资源。

验证客户端

使用客户端库与服务的交互首先创建 TextTranslationClient 类的实例。 需要 一个 API 密钥 或 TokenCredential 来实例化客户端对象。 有关使用认知服务进行身份验证的详细信息,请参阅 对翻译器服务的请求进行身份验证。

获取 API 密钥

可以从 Azure 门户中的endpoint认知服务资源或翻译器服务资源信息获取 、 API key 和 Region 。

或者,使用以下 Azure CLI 代码片段从翻译器服务资源获取 API 密钥。

az cognitiveservices account keys list --resource-group --name TextTranslationClient使用 API 密钥和区域凭据创建

获得 API 密钥和区域的值后,请 TranslatorCredential创建 。 这样,无需创建新客户端即可更新 API 密钥。

使用 终结点 TranslatorCredential 的值和 , Region可以创建 TextTranslationClient:

text_translator = TextTranslationClient(credential = TranslatorCredential("", "")); 关键概念 TextTranslationClient

是 TextTranslationClient 使用文本翻译客户端库的开发人员的主要接口。 它提供同步和异步操作来访问文本翻译的特定用途,例如获取支持的语言检测或文本翻译。

输入

文本元素 (string) ,是翻译器服务中的翻译模型要处理的单个输入单元。 对 TextTranslationClient 的操作可能采用单个文本元素或文本元素集合。 有关文本元素长度限制、最大请求大小和支持的文本编码,请参阅 此处。

示例

以下部分提供了使用client上面创建的 多个代码片段,并介绍了此客户端库中存在的main功能。 尽管下面的大多数代码片段都使用同步服务调用,但请记住,Python 文本翻译库包支持同步和异步 API。

获取支持的语言

获取翻译器的其他操作当前支持的语言集。

try: response = text_translator.get_languages() print(f"Number of supported languages for translate operation: {len(response.translation) if response.translation is not None else 0}") print(f"Number of supported languages for transliterate operation: {len(response.transliteration) if response.transliteration is not None else 0}") print(f"Number of supported languages for dictionary operations: {len(response.dictionary) if response.dictionary is not None else 0}") if response.translation is not None: print("Translation Languages:") for key, value in response.translation.items(): print(f"{key} -- name: {value.name} ({value.native_name})") if response.transliteration is not None: print("Transliteration Languages:") for key, value in response.transliteration.items(): print(f"{key} -- name: {value.name}, supported script count: {len(value.scripts)}") if response.dictionary is not None: print("Dictionary Languages:") for key, value in response.dictionary.items(): print(f"{key} -- name: {value.name}, supported target languages count: {len(value.translations)}") except HttpResponseError as exception: print(f"Error Code: {exception.error.code}") print(f"Message: {exception.error.message}")

有关使用终结点的示例, languages 请参阅 此处的更多示例。

有关 语言的概念性讨论,请参阅服务文档。

Translate

使用单个请求将单个源语言文本呈现为多个目标语言文本。

try: source_language = "en" target_languages = ["cs"] input_text_elements = [ InputTextItem(text = "This is a test") ] response = text_translator.translate(content = input_text_elements, to = target_languages, from_parameter = source_language) translation = response[0] if response else None if translation: for translated_text in translation.translations: print(f"Text was translated to: '{translated_text.to}' and the result is: '{translated_text.text}'.") except HttpResponseError as exception: print(f"Error Code: {exception.error.code}") print(f"Message: {exception.error.message}")

有关使用终结点的示例, translate 请参阅 此处的更多示例。

有关 翻译的概念性讨论,请参阅服务文档。

Transliterate

将源语言的字符或字母转换为目标语言的对应字符或字母。

try: language = "zh-Hans" from_script = "Hans" to_script = "Latn" input_text_elements = [ InputTextItem(text = "这是个测试。") ] response = text_translator.transliterate(content = input_text_elements, language = language, from_script = from_script, to_script = to_script) transliteration = response[0] if response else None if transliteration: print(f"Input text was transliterated to '{transliteration.script}' script. Transliterated text: '{transliteration.text}'.") except HttpResponseError as exception: print(f"Error Code: {exception.error.code}") print(f"Message: {exception.error.message}")

有关使用终结点的示例, transliterate 请参阅 此处的更多示例。

有关 transliterate 的概念性讨论,请参阅服务文档。

断句

标识文本段中的句子边界的位置。

try: source_language = "zh-Hans" source_script = "Latn" input_text_elements = [ InputTextItem(text = "zhè shì gè cè shì。") ] response = text_translator.find_sentence_boundaries(content = input_text_elements, language = source_language, script = source_script) sentence_boundaries = response[0] if response else None if sentence_boundaries: detected_language = sentence_boundaries.detected_language if detected_language: print(f"Detected languages of the input text: {detected_language.language} with score: {detected_language.score}.") print(f"The detected sentence boundaries:") for boundary in sentence_boundaries.sent_len: print(boundary) except HttpResponseError as exception: print(f"Error Code: {exception.error.code}") print(f"Message: {exception.error.message}")

有关使用终结点的示例, break sentence 请参阅 此处的更多示例。

有关 断句的概念性讨论,请参阅服务文档。

字典查找

返回源术语在目标语言中的等效字词。

try: source_language = "en" target_language = "es" input_text_elements = [ InputTextItem(text = "fly") ] response = text_translator.lookup_dictionary_entries(content = input_text_elements, from_parameter = source_language, to = target_language) dictionary_entry = response[0] if response else None if dictionary_entry: print(f"For the given input {len(dictionary_entry.translations)} entries were found in the dictionary.") print(f"First entry: '{dictionary_entry.translations[0].display_target}', confidence: {dictionary_entry.translations[0].confidence}.") except HttpResponseError as exception: print(f"Error Code: {exception.error.code}") print(f"Message: {exception.error.message}")

有关使用终结点的示例, dictionary lookup 请参阅 此处的更多示例。

有关 字典查找的概念性讨论,请参阅服务文档。

字典示例

返回源术语和目标术语对的语法结构和上下文示例。

from azure.ai.translation.text.models import DictionaryExampleTextItem try: source_language = "en" target_language = "es" input_text_elements = [ DictionaryExampleTextItem(text = "fly", translation = "volar") ] response = text_translator.lookup_dictionary_examples(content = input_text_elements, from_parameter = source_language, to = target_language) dictionary_entry = response[0] if response else None if dictionary_entry: print(f"For the given input {len(dictionary_entry.examples)} entries were found in the dictionary.") print(f"First example: '{dictionary_entry.examples[0].target_prefix}{dictionary_entry.examples[0].target_term}{dictionary_entry.examples[0].target_suffix}'.") except HttpResponseError as exception: print(f"Error Code: {exception.error.code}") print(f"Message: {exception.error.message}")

有关使用终结点的示例, dictionary examples 请参阅 此处的更多示例。

有关 字典示例的概念性讨论,请参阅服务文档。

疑难解答

使用 TextTranslator 客户端库与翻译器服务交互时,翻译器服务返回的错误对应于为 REST API 请求返回的相同 HTTP 状态代码。

例如,如果提交没有目标翻译语言的翻译请求,则会返回一个 400 错误,指示“错误请求”。

可以在服务 文档中找到服务返回的不同错误代码。

提供反馈

如果遇到任何 bug 或有建议,请在项目的“ 问题 ”部分中提出问题。

后续步骤

可以在示例目录下找到更多 示例 。

贡献

本项目欢迎贡献和建议。 大多数贡献要求你同意贡献者许可协议 (CLA),并声明你有权(并且确实有权)授予我们使用你的贡献的权利。 有关详细信息,请访问 cla.microsoft.com。

提交拉取请求时,CLA 机器人将自动确定你是否需要提供 CLA,并相应地修饰 PR(例如标签、注释)。 直接按机器人提供的说明操作。 只需使用 CLA 对所有存储库执行一次这样的操作。

此项目采用了 Microsoft 开放源代码行为准则。 有关详细信息,请参阅行为准则常见问题解答,或如果有任何其他问题或意见,请与 联系。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有