问题:我目前正在使用 azure 翻译 API 将我的波兰语文本翻译成英语,并且我们正在使用 s1 实例。当我尝试将翻译后的文本保存在本地计算机中时,它工作正常,但是当我尝试将其存储在 azure blob 存储中时,我收到了不需要的内容,例如示例 翻译文本:“标题”:“位于 K\u0142odawa 公社 Rgielew 的建筑财产” 所需/正确文本:“标题”:“位于克洛达瓦公社 Rgielew 的建筑财产”或“标题”:“位于克洛达瓦公社 Rgielew 的建筑财产”
我在代码中使用了我在网络上找到的基本模板,所以我在下面分享它
import requests
import uuid
import json
import os
def translate_dict(input_dict, source_language, target_languages):
# Convert dictionary to JSON text
json_text = json.dumps(input_dict, ensure_ascii=False)
# Add your key and endpoint
key = os.getenv("translation_key")
endpoint = os.getenv("translation_endpoint")
# location, also known as region.
# required if you're using a multi-service or regional (not global) resource.
location = os.getenv("translation_location")
path = '/translate'
constructed_url = endpoint + path
params = {
'api-version': '3.0',
'from': source_language,
'to': target_languages
}
headers = {
'Ocp-Apim-Subscription-Key': key,
# location required if you're using a multi-service or regional (not global) resource.
'Ocp-Apim-Subscription-Region': location,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
# Create body for translation
body = [{'text': json_text}]
# Make translation request
request = requests.post(constructed_url, params=params, headers=headers, json=body)
response = request.json()
# Extract translated text from the response
translated_json_text = response[0]['translations'][0]['text']
# Convert the translated text back to a dictionary
translated_dict = json.loads(translated_json_text)
return translated_dict
如果有人已经使用过 Azure Translator API,可以让我知道如何处理这种情况。谢谢!
我尝试在网络中浏览以查找是否有人遇到类似的问题,当我在本地保存 Json 文件时它工作正常,但当我将其存储在 azure blob 存储中时,出现了主要问题。
当我尝试将其存储在天蓝色的 blob 存储中时,我收到了不需要的内容,例如示例翻译文本:“title”:“Built-up property located in Rgielew, K\u0142odawa commune”所需/正确的文本:“title”:“位于克洛达瓦公社 Rgielew 的建成房产”或“标题”:“位于克洛达瓦公社 Rgielew 的建成房产”
您可以使用以下修改后的代码,使用 Python 从
Polish
文本到 English
文本获取所需的输出。
代码:
import requests
import uuid
import json
from azure.storage.blob import BlobServiceClient
def translate_dict():
input_dict = {"title": "Nieruchomość zabudowana położona w Rgielewie, gmina Kłodawa"}
source_language = "pl"
target_languages = "en"
json_text = json.dumps(input_dict, ensure_ascii=False)
key = os.getenv("translation_key")
endpoint = os.getenv("translation_endpoint")
location = os.getenv("translation_location")
path = '/translate'
constructed_url = endpoint + path
params = {
'api-version': '3.0',
'from': source_language,
'to': target_languages
}
headers = {
'Ocp-Apim-Subscription-Key': key,
'Ocp-Apim-Subscription-Region': location,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
body = [{'text': json_text}]
request = requests.post(constructed_url, params=params, headers=headers, json=body)
response = request.json()
translated_json_text = response[0]['translations'][0]['text']
translated_dict = json.loads(translated_json_text)
encoded_text = json.dumps(translated_dict, ensure_ascii=False)
connection_string = "<Storage connection string>"
container_name = "test"
blob_name = "sample.json"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(container_name)
blob_client = container_client.get_blob_client(blob_name)
blob_client.upload_blob(encoded_text, overwrite=True)
translate_dict()
上述代码使用 Azure 认知服务翻译器 API 将字典从一种语言(波兰语)翻译为另一种语言(英语),并将翻译后的字典作为 JSON 文件保存在 Azure Blob 存储容器中。
传送门:
所需的输出是“位于 Rgielew, Kłodawa 公社的建筑房产”。
参考: