国家字符转录模块。
trans的Python项目详细描述
本单元将民族文字翻译成类似的发音 拉丁字符(音译)。 目前,捷克语、希腊语、拉脱维亚语、波兰语、土耳其语、俄语、乌克兰语, 支持哈萨克和波斯语字母(它覆盖了99%的需求)。
内容
Simple usage
It’s very easy to use
Python3:
>>> from trans import trans >>> trans('Привет, Мир!')
Python2:
>>> import trans >>> u'Привет, Мир!'.encode('trans') u'Privet, Mir!' >>> trans.trans(u'Привет, Мир!') u'Privet, Mir!'
Work only with unicode strings
>>> 'Hello World!'.encode('trans') Traceback (most recent call last): ... TypeError: trans codec support only unicode string, <type 'str'> given.
This is readability
>>> s = u'''\ ... -- Раскудрить твою через коромысло в бога душу мать ... триста тысяч раз едрену вошь тебе в крыло ... и кактус в глотку! -- взревел разъяренный Никодим. ... -- Аминь, -- робко добавил из склепа папа Пий. ... (c) Г. Л. Олди, "Сказки дедушки вампира".''' >>> >>> print s.encode('trans') -- Raskudrit tvoyu cherez koromyslo v boga dushu mat trista tysyach raz edrenu vosh tebe v krylo i kaktus v glotku! -- vzrevel razyarennyy Nikodim. -- Amin, -- robko dobavil iz sklepa papa Piy. (c) G. L. Oldi, "Skazki dedushki vampira".
Table “slug”
使用“slug”表,只留下拉丁字符、数字和下划线:
>>> print u'1 2 3 4 5 \n6 7 8 9 0'.encode('trans') 1 2 3 4 5 6 7 8 9 0 >>> print u'1 2 3 4 5 \n6 7 8 9 0'.encode('trans/slug') 1_2_3_4_5__6_7_8_9_0 >>> s.encode('trans/slug')[-42:-1] u'_c__G__L__Oldi___Skazki_dedushki_vampira_'
Table “id”
表id已弃用并重命名为slug。 旧名称也可用,但不推荐使用。
Define user tables
Simple variant
>>> u'1 2 3 4 5 6 7 8 9 0'.encode('trans/my') Traceback (most recent call last): ... ValueError: Table "my" not found in tables! >>> trans.tables['my'] = {u'1': u'A', u'2': u'B'}; >>> u'1 2 3 4 5 6 7 8 9 0'.encode('trans/my') u'A_B________________' >>>
A little harder
表格可以由两部分组成:双元音图和字符图。 双光子首先通过子串中的简单替换进行处理。 然后根据 角色。如果字符映射中缺少字符,则选中键none。 如果键none不存在,则使用默认字符u'。
>>> diphthongs = {u'11': u'AA', u'22': u'BB'} >>> characters = {u'a': u'z', u'b': u'y', u'c': u'x', None: u'-', ... u'A': u'A', u'B': u'B'} # See below... >>> trans.tables['test'] = (diphthongs, characters) >>> u'11abc22cbaCC'.encode('trans/test') u'AAzyxBBxyz--'
字符是通过同时处理双元音来创建的 通过符号的映射:
>>> diphthongs = {u'11': u'AA', u'22': u'BB'} >>> characters = {u'a': u'z', u'b': u'y', u'c': u'x', None: u'-'} >>> trans.tables['test'] = (diphthongs, characters) >>> u'11abc22cbaCC'.encode('trans/test') u'--zyx--xyz--'
Without the diphthongs
这两张表相当:
>>> characters = {u'a': u'z', u'b': u'y', u'c': u'x', None: u'-'} >>> trans.tables['t1'] = characters >>> trans.tables['t2'] = ({}, characters) >>> u'11abc22cbaCC'.encode('trans/t1') == u'11abc22cbaCC'.encode('trans/t2') True
ChangeLog
2.1 2016年9月19日
- Add Farsi alphabet (thx rodgar-nvkz)
- Use pytest
- Some code style refactoring
2.0 2013年4月1日
- Python 3 support
- class Trans for create different tables spaces
2012年9月12日1.5
- Add support of kazakh alphabet.
1.4 2011年11月29日
- Change license to BSD.
1.3 2010年5月18日
- Table “id” renamed to “slug”. Old name also available.
- Some speed optimizations (thx to AndyLegkiy <andy.legkiy at gmail.com>).
1.2 2010年1月10日
- First public release.
- Translate documentation to English.
Finally
- 特别感谢尤里·尤雷维奇(Yuri Yurevich,又名J2A)朝着正确的方向踢了一脚。
- 我请求原谅我糟糕的英语。我保证会改正的。