问题描述
限时送ChatGPT账号..我正在使用 Python(3.6) 和 Flask 处理一个 Python 项目,我必须在其中返回阿拉伯语文本.当我在控制台中打印文本时,它运行良好,但是当我将其作为响应返回时,它的顺序更改为从右到左.
I'm working on a python project with Python(3.6) and Flask in which I have to return a text in Arabic. When I print the text in the console it works well but when I return it as response it's order changes to right-to-left.
这是我尝试过的:
from odoa import ODOA
import arabic_reshaper
from bidi.algorithm import get_display
from flask import Flask
import json
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
@app.route('/', methods=['GET'])
def get_an_ayah():
odoa = ODOA()
surah = odoa.get_random_surah(lang='en')
text = surah.ayah.decode("utf-8")
reshaped_text = arabic_reshaper.reshape(text) # correct its shape
arabic_text = get_display(reshaped_text, base_dir='R')
print(arabic_text)
translation = str(surah.translate)
sound_file_url = str(surah.sound)
reference = str(str(surah.surah_number) + ':' + str(surah.ayah_number))
response_dict = {
'text': arabic_text,
'translation': translation,
'sound': sound_file_url,
'ref': reference
}
return response_dict
print(arabix_text
的结果是:
这是它的响应方式:
{
"ref": "94:2",
"sound": "https://raw.githubusercontent/semarketir/quranjson/master/source/audio/094/002.mp3",
"text": "ﻙﺭﺯﻭ ﻚﻨﻋ ﺎﻨﻌﺿﻭﻭ",
"translation": "And lift from you your burden."
}
我怎样才能获得阿拉伯文本的正确方向?
how can I get the correct orientation for Arabic text?
推荐答案
你真的需要 arabic_reshaper 和 python-bidi ?
Do you really need arabic_reshaper and python-bidi ?
试试下面的代码:
from odoa import ODOA
from flask import Flask
import json
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
@app.route('/', methods=['GET'])
def get_an_ayah():
odoa = ODOA()
surah = odoa.get_random_surah(lang='en')
text = surah.ayah.decode("utf-8")
translation = str(surah.translate)
sound_file_url = str(surah.sound)
reference = str(str(surah.surah_number) + ':' + str(surah.ayah_number))
response_dict = {
'text': text,
'translation': translation,
'sound': sound_file_url,
'ref': reference
}
return response_dict
输出文本将带有阿拉伯语变音符号和古兰经注释.如果要删除它们,请使用以下正则表达式替换:
The output text will be with Arabic diacritics and Quranic annotation. If you want to remove them, use the following regex replacement:
import re
PATTERN = repile(
'['
'\u0610-\u061a'
'\u064b-\u065f'
'\u0670'
'\u06d6-\u06dc'
'\u06df-\u06e8'
'\u06ea-\u06ed'
'\u08d4-\u08e1'
'\u08d4-\u08ed'
'\u08e3-\u08ff'
']',
)
letters_only_text = re.sub(PATTERN, '', text)
请参阅阿拉伯语 Unicode 图表 和 Arabic Extended-A Unicode Chart 了解有关这些替换的更多信息.
See Arabic Unicode Chart and Arabic Extended-A Unicode Chart for more info about those replacements.
这篇关于Python 阿拉伯语文本以从右到左的方向返回,而不是从左到右的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论