• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

CHANGELOGH A D19-Aug-2016437 2313

PKG-INFOH A D19-Aug-20166.7 KiB179140

README.rstH A D19-Aug-20164.7 KiB159121

setup.cfgH A D19-Aug-201625 32

setup.pyH A D19-Aug-20161 KiB3829

trans.pyH A D19-Aug-201610.9 KiB274215

README.rst

1====================
2The **trans** module
3====================
4
5This module translates national characters into similar sounding
6latin characters (transliteration).
7At the moment, Czech, Greek, Latvian, Polish, Turkish, Russian, Ukrainian,
8Kazakh and Farsi alphabets are supported (it covers 99% of needs).
9
10.. contents::
11
12Simple usage
13------------
14It's very easy to use
15~~~~~~~~~~~~~~~~~~~~~
16
17Python 3:
18
19  >>> from trans import trans
20  >>> trans('Привет, Мир!')
21
22Python 2:
23
24  >>> import trans
25  >>> u'Привет, Мир!'.encode('trans')
26  u'Privet, Mir!'
27  >>> trans.trans(u'Привет, Мир!')
28  u'Privet, Mir!'
29
30Work only with unicode strings
31~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32  >>> 'Hello World!'.encode('trans')
33  Traceback (most recent call last):
34      ...
35  TypeError: trans codec support only unicode string, <type 'str'> given.
36
37This is readability
38~~~~~~~~~~~~~~~~~~~
39  >>> s = u'''\
40  ...    -- Раскудрить твою через коромысло в бога душу мать
41  ...             триста тысяч раз едрену вошь тебе в крыло
42  ...             и кактус в глотку! -- взревел разъяренный Никодим.
43  ...    -- Аминь, -- робко добавил из склепа папа Пий.
44  ...                 (c) Г. Л. Олди, "Сказки дедушки вампира".'''
45  >>>
46  >>> print s.encode('trans')
47     -- Raskudrit tvoyu cherez koromyslo v boga dushu mat
48              trista tysyach raz edrenu vosh tebe v krylo
49              i kaktus v glotku! -- vzrevel razyarennyy Nikodim.
50     -- Amin, -- robko dobavil iz sklepa papa Piy.
51                  (c) G. L. Oldi, "Skazki dedushki vampira".
52
53Table "**slug**"
54~~~~~~~~~~~~~~~~
55Use the table "slug", leaving only the Latin characters, digits and underscores:
56
57  >>> print u'1 2 3 4 5 \n6 7 8 9 0'.encode('trans')
58  1 2 3 4 5
59  6 7 8 9 0
60  >>> print u'1 2 3 4 5 \n6 7 8 9 0'.encode('trans/slug')
61  1_2_3_4_5__6_7_8_9_0
62  >>> s.encode('trans/slug')[-42:-1]
63  u'_c__G__L__Oldi___Skazki_dedushki_vampira_'
64
65Table "**id**"
66~~~~~~~~~~~~~~
67Table **id** is deprecated and renamed to **slug**.
68Old name also available, but not recommended.
69
70Define user tables
71------------------
72Simple variant
73~~~~~~~~~~~~~~
74  >>> u'1 2 3 4 5 6 7 8 9 0'.encode('trans/my')
75  Traceback (most recent call last):
76      ...
77  ValueError: Table "my" not found in tables!
78  >>> trans.tables['my'] = {u'1': u'A', u'2': u'B'};
79  >>> u'1 2 3 4 5 6 7 8 9 0'.encode('trans/my')
80  u'A_B________________'
81  >>>
82
83A little harder
84~~~~~~~~~~~~~~~
85Table can consist of two parts - the map of diphthongs and the map of characters.
86Diphthongs are processed first by simple replacement in the substring.
87Then each character of the received string is replaced according to the map of
88characters. If character is absent in the map of characters, key **None** are checked.
89If key **None** is not present, the default character **u'_'** is used.
90
91
92  >>> diphthongs = {u'11': u'AA', u'22': u'BB'}
93  >>> characters = {u'a': u'z', u'b': u'y', u'c': u'x', None: u'-',
94  ...               u'A': u'A', u'B': u'B'}  # See below...
95  >>> trans.tables['test'] = (diphthongs, characters)
96  >>> u'11abc22cbaCC'.encode('trans/test')
97  u'AAzyxBBxyz--'
98
99**The characters are created by processing of diphthongs also processed
100by the map of the symbols:**
101
102  >>> diphthongs = {u'11': u'AA', u'22': u'BB'}
103  >>> characters = {u'a': u'z', u'b': u'y', u'c': u'x', None: u'-'}
104  >>> trans.tables['test'] = (diphthongs, characters)
105  >>> u'11abc22cbaCC'.encode('trans/test')
106  u'--zyx--xyz--'
107
108Without the diphthongs
109~~~~~~~~~~~~~~~~~~~~~~
110These two tables are equivalent:
111
112  >>> characters = {u'a': u'z', u'b': u'y', u'c': u'x', None: u'-'}
113  >>> trans.tables['t1'] = characters
114  >>> trans.tables['t2'] = ({}, characters)
115  >>> u'11abc22cbaCC'.encode('trans/t1') == u'11abc22cbaCC'.encode('trans/t2')
116  True
117
118ChangeLog
119---------
120
1212.1 2016-09-19
122
123    * Add Farsi alphabet (thx rodgar-nvkz)
124    * Use pytest
125    * Some code style refactoring
126
127
1282.0 2013-04-01
129
130    * Python 3 support
131    * class Trans for create different tables spaces
132
1331.5 2012-09-12
134
135    * Add support of kazakh alphabet.
136
1371.4 2011-11-29
138
139    * Change license to BSD.
140
1411.3 2010-05-18
142
143    * Table "id" renamed to "slug". Old name also available.
144    * Some speed optimizations (thx to AndyLegkiy <andy.legkiy at gmail.com>).
145
1461.2 2010-01-10
147
148    * First public release.
149    * Translate documentation to English.
150
151
152
153Finally
154-------
155+ *Special thanks to Yuri Yurevich aka j2a for the kick in the right direction.*
156    - http://python.su/forum/viewtopic.php?pid=28965
157    - http://code.djangoproject.com/browser/django/trunk/django/contrib/admin/media/js/urlify.js
158+ *I ask forgiveness for my bad English. I promise to be corrected.*
159