re.sub更改被替换的波斯语/阿拉伯语内容的方向

2024-06-26 15:21:32 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码:

import re

CUSTOMIZED_SUB_PATTERN = "\{{\{{(?:\s)*{tag_key}(?:\s)*\|(?:\s)*([^|}}]+)(?:\s)*\}}\}}"
pattern = re.compile(CUSTOMIZED_SUB_PATTERN.format(tag_key='name'))
title = "عزیز {{ name | default value 1}} سلام"
re.sub(pattern, "محمد", title)

输出:

'عزیز محمد سلام'

但我想要的是:

'سلام محمد عزیز'

因此,正如你所看到的,句子的方向随着替换而改变

问题: 如何解决此问题


Tags: key代码nameimportreformatdefaulttitle
3条回答

您可以使用^{}^{}库来相应地重塑和替换RTL文本

There is a special option in get_Display() method which is base_dir which has ‘L’ or ‘R’, override the calculated base_level.

你可以尝试:

import re
import arabic_reshaper
from bidi.algorithm import get_display

title = "عزیز {{ name | defalue value 1}} سلام"
substr = "محمد"
reshaped_text = arabic_reshaper.reshape(title) 
new_title = get_display(reshaped_text, base_dir = 'L') # 'L' option indicates the text to appear from Left to Right. By default; it is RTL for Arabic texts.       
reshaped_text2 = arabic_reshaper.reshape(substr)
new_substr = get_display(reshaped_text2, base_dir = 'L')

CUSTOMIZED_SUB_PATTERN = "\{{\{{(?:\s)*{tag_key}(?:\s)*\|(?:\s)*([^|}}]+)(?:\s)*\}}\}}"
pattern = re.compile(CUSTOMIZED_SUB_PATTERN.format(tag_key='name'))
print(re.sub(pattern, new_substr, new_title))

您可以在here.中找到上述实现的示例运行结果

通过此模块,您可以纠正文字形状的方向。只需安装PIP并使用它

# install: pip install  upgrade arabic-reshaper
import arabic_reshaper

# install: pip install python-bidi
from bidi.algorithm import get_display

text = "ذهب الطالب الى المدرسة"
reshaped_text = arabic_reshaper.reshape(text)    # correct its shape
bidi_text = get_display(reshaped_text)           # correct its direction

这个答案是不正确的

>>> x = 'walk down street'
>>> x.split(' ')
['walk', 'down', 'street']
>>> x.split(' ')[::-1]
['street', 'down', 'walk']
>>> ' '.join(x.split(' ')[::-1])
'street down walk'

希望我能帮忙

相关问题 更多 >