我怎样才能用Python将阿拉伯语翻译成英语?

问题描述 投票:0回答:2

translate_list1 = input("输入阿拉伯语短语:") translate_list2 = "英文单词"

def 翻译(str): 翻译=“” 对于 str 中的字母: if 字母在translate_list1中: 翻译 = 翻译 + 翻译列表2 别的: 翻译 = 翻译列表2 if 字母在translate_list2中: 翻译 = 翻译 + 翻译列表1 别的: 翻译=无 返回翻译 print("英文单词")

python-3.x
2个回答
0
投票

我有点困惑。该代码是否旨在将阿拉伯语翻译成英语?在这种情况下,用户应该在哪里输入阿拉伯语才能将其翻译成英语。请帮忙。


-1
投票

以下是使用 Azure Translator API 的示例:

# -*- coding: utf-8 -*-

# This simple app uses the '/translate' resource to translate text from
# one language to another.

# This sample runs on Python 2.7.x and Python 3.x.
# You may need to install requests and uuid.
# Run: pip install requests uuid

import os, requests, uuid, json

key_var_name = 'TRANSLATOR_TEXT_SUBSCRIPTION_KEY'
if not key_var_name in os.environ:
    raise Exception('Please set/export the environment variable: {}'.format(key_var_name))
subscription_key = os.environ[key_var_name]

region_var_name = 'TRANSLATOR_TEXT_REGION'
if not region_var_name in os.environ:
    raise Exception('Please set/export the environment variable: {}'.format(region_var_name))
region = os.environ[region_var_name]

endpoint_var_name = 'TRANSLATOR_TEXT_ENDPOINT'
if not endpoint_var_name in os.environ:
    raise Exception('Please set/export the environment variable: {}'.format(endpoint_var_name))
endpoint = os.environ[endpoint_var_name]

# If you encounter any issues with the base_url or path, make sure
# that you are using the latest endpoint: https://learn.microsoft.com/azure/cognitive-services/translator/reference/v3-0-translate
path = '/translate?api-version=3.0'
params = '&from=ar&to=en'
constructed_url = endpoint + path + params

headers = {
    'Ocp-Apim-Subscription-Key': subscription_key,
    'Ocp-Apim-Subscription-Region': region,
    'Content-type': 'application/json',
    'X-ClientTraceId': str(uuid.uuid4())
}

# You can pass more than one object in body.
body = [{
    'text' : 'Hello World!'
}]
request = requests.post(constructed_url, headers=headers, json=body)
response = request.json()

print(json.dumps(response, sort_keys=True, indent=4, ensure_ascii=False, separators=(',', ': ')))
© www.soinside.com 2019 - 2024. All rights reserved.