python - 如何在Python中将阿拉伯字母映射到音素?

标签 python unicode arabic phonetics grapheme

我想制作一个简单的 Python 脚本,将每个阿拉伯字母映射到音素声音符号。我有一个文件,其中包含一堆单词,脚本将读取这些单词以将它们转换为音素,并且我的代码中有以下字典:

我的 .txt 文件中的内容:

السلام عليكم
السلام عليكم و رحمة الله
السلام عليكم و رحمة الله و بركاته
الحمد لله
كيف حالك
كيف الحال

我的代码中的字典:

ar_let_phon_maplist = {u'ﺍ':'A:', u'ﺏ':'B', u'ﺕ':'T', u'ﺙ':'TH', u'ﺝ':'J', u'ﺡ':'H', u'ﺥ':'KH', u'ﻩ':'H', u'ﻉ':'(ayn) ’', u'ﻍ':'GH', u'ﻑ':'F', u'ﻕ':'q', u'ﺹ':u'ṣ', u'ﺽ':u'ḍ', u'ﺩ':'D', u'ﺫ':'DH', u'ﻁ':u'ṭ', u'ﻙ':'K', u'ﻡ':'M', u'ﻥ':'N', u'ﻝ':'L', u'ﻱ':'Y', u'ﺱ':'S', u'ﺵ':'SH', u'ﻅ':u'ẓ', u'ﺯ':'Z', u'ﻭ':'W', u'ﺭ':'R'}

我有一个嵌套循环,我在其中读取每一行,转换每个字符:

with codecs.open(sys.argv[1], 'r', encoding='utf-8') as file:
        lines = file.readlines()

line_counter = 0

for line in lines:
        print "Phonetics In Line " + str(line_counter)
        print line + " ",
        for word in line:
                for character in word:
                        if character == '\n':
                                print ""
                        elif character == ' ':
                                print "  "
                        else:
                                print ar_let_phon_maplist[character] + " ",
line_counter +=1

这是我收到的错误:

Phonetics In Line 0
السلام عليكم

Traceback (most recent call last):
  File "grapheme2phoneme.py", line 25, in <module>
    print ar_let_phon_maplist[character] + " ",
KeyError: u'\u0627'

然后我使用 Linux 命令检查文件类型是否为 UTF-8:

file words.txt

我得到的输出:

words.txt: UTF-8 Unicode text

此问题的任何解决方案,为什么它不映射到字典中的 Unicode 对象,因为我在 ar_let_phon_maplist[character] 行中用作键的字符也是 Unicode? 我的代码有问题吗?

最佳答案

首先引起注意的是KeyError。所以你的字典根本不知道文件中遇到的一些符号。展望 future ,它不知道任何提交的字符,而不仅仅是第一个字符。

我们可以用它做什么?好的,我们可以将 unicode 表的阿拉伯语段中的所有符号添加到我们的字典中。简单的?是的。清除?没有。

如果您想真正了解这种“奇怪”行为的原因,您应该更多地了解 Unicode。简而言之,有很多看起来相似但序数不同的字母。而且,同一个字母有时可以以多种形式呈现。所以comparing unicode charactersnot a trivial task .

因此,如果允许我使用 Python 3.3+,我将按如下方式解决该任务。首先,我将规范化 ar_let_phon_maplist 字典中的键:

ar_let_phon_maplist = {unicodedata.normalize('NFKD', k): v 
                            for k, v in ar_let_phon_maplist.items()}

然后我们将迭代文件中的行、行中的单词和单词中的字符,如下所示:

for index, line in enumerate(lines):
    print('Phonetics in line {0}, total {1} symbols'.format(index, len(line)))
    unknown = []  # Here will be stored symbols that we haven't found in dict
    words = line.split()
    for word in words:
        print(word, ': ', sep='', end='')
        for character in word:
            c = unicodedata.normalize('NFKD', character).casefold()
            try:                
                print(ar_let_phon_maplist[c], sep='', end='')
            except KeyError:
                print('_', sep='', end='')
                if c not in unknown:
                    unknown.append(c)
        print()
    if unknown:
        print('Unrecognized symbols: {0}, total {1} symbols'.format(', '.join(unknown), 
                                                                    len(unknown)))

脚本会产生类似的东西:

Phonetics in line 4, total 9 symbols
كيف: KYF
حالك: HA:LK

关于python - 如何在Python中将阿拉伯字母映射到音素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34538388/

相关文章:

python - 为什么在使用 python 和 numpy 时 sin(180) 不为零?

javascript - 重音字符(变音符号)的具体 JavaScript 正则表达式

unicode - 未读取 golang unicode/norm 迭代器的最后一个 rune

java - itext 中的阿拉伯语

Excel 将阿拉伯语英语混合文本导出到 CSV 文件时包含问号

python - 如何从 GitHub 安装 Python 包?

python - 使用类作为装饰器时,为什么 'self' 不在 args 中?

python - 无法访问 http ://0. 0.0.0:8000

objective-c - 来自 Unicode 的 NSString

cmd - 我如何在cmd中显示阿拉伯字母