MediaWiki  master
KkConverter.php
Go to the documentation of this file.
1 <?php
24 define( 'KK_C_UC', 'АӘБВГҒДЕЁЖЗИЙКҚЛМНҢОӨПРСТУҰҮФХҺЦЧШЩЪЫІЬЭЮЯ' ); # Kazakh Cyrillic uppercase
25 define( 'KK_C_LC', 'аәбвгғдеёжзийкқлмнңоөпрстуұүфхһцчшщъыіьэюя' ); # Kazakh Cyrillic lowercase
26 define( 'KK_L_UC', 'AÄBCÇDEÉFGĞHIİÏJKLMNÑOÖPQRSŞTUÜVWXYÝZ' ); # Kazakh Latin uppercase
27 define( 'KK_L_LC', 'aäbcçdeéfgğhıiïjklmnñoöpqrsştuüvwxyýz' ); # Kazakh Latin lowercase
28 // define( 'KK_A', 'ٴابپتجحدرزسشعفقكلمنڭەوۇۋۆىيچھ' ); # Kazakh Arabic
29 define( 'H_HAMZA', 'ٴ' ); # U+0674 ARABIC LETTER HIGH HAMZA
30 // define( 'ZWNJ', '‌' ); # U+200C ZERO WIDTH NON-JOINER
31 
38 
40 
44  public function __construct( $langobj ) {
45  $variants = [ 'kk', 'kk-cyrl', 'kk-latn', 'kk-arab', 'kk-kz', 'kk-tr', 'kk-cn' ];
46  $variantfallbacks = [
47  'kk' => 'kk-cyrl',
48  'kk-cyrl' => 'kk',
49  'kk-latn' => 'kk',
50  'kk-arab' => 'kk',
51  'kk-kz' => 'kk-cyrl',
52  'kk-tr' => 'kk-latn',
53  'kk-cn' => 'kk-arab'
54  ];
55 
56  parent::__construct( $langobj, 'kk',
57  $variants, $variantfallbacks, [] );
58 
59  // No point delaying this since they're in code.
60  // Waiting until loadDefaultTables() means they never get loaded
61  // when the tables themselves are loaded from cache.
62  $this->loadRegs();
63  }
64 
65  protected function loadDefaultTables() {
66  // require __DIR__."/../../includes/KkConversion.php";
67  // Placeholder for future implementing. Remove variables declarations
68  // after generating KkConversion.php
69  $kk2Cyrl = [];
70  $kk2Latn = [];
71  $kk2Arab = [];
72  $kk2KZ = [];
73  $kk2TR = [];
74  $kk2CN = [];
75 
76  $this->mTables = [
77  'kk-cyrl' => new ReplacementArray( $kk2Cyrl ),
78  'kk-latn' => new ReplacementArray( $kk2Latn ),
79  'kk-arab' => new ReplacementArray( $kk2Arab ),
80  'kk-kz' => new ReplacementArray( array_merge( $kk2Cyrl, $kk2KZ ) ),
81  'kk-tr' => new ReplacementArray( array_merge( $kk2Latn, $kk2TR ) ),
82  'kk-cn' => new ReplacementArray( array_merge( $kk2Arab, $kk2CN ) ),
83  'kk' => new ReplacementArray()
84  ];
85  }
86 
87  protected function postLoadTables() {
88  $this->mTables['kk-kz']->merge( $this->mTables['kk-cyrl'] );
89  $this->mTables['kk-tr']->merge( $this->mTables['kk-latn'] );
90  $this->mTables['kk-cn']->merge( $this->mTables['kk-arab'] );
91  }
92 
93  private function loadRegs() {
94  $this->mCyrl2Latn = [
95  # # Punctuation
96  '/№/u' => 'No.',
97  # # Е after vowels
98  '/([АӘЕЁИОӨҰҮЭЮЯЪЬ])Е/u' => '$1YE',
99  '/([АӘЕЁИОӨҰҮЭЮЯЪЬ])е/ui' => '$1ye',
100  # # leading ЁЮЯЩ
101  '/^Ё([' . KK_C_UC . ']|$)/u' => 'YO$1', '/^Ё([' . KK_C_LC . ']|$)/u' => 'Yo$1',
102  '/^Ю([' . KK_C_UC . ']|$)/u' => 'YU$1', '/^Ю([' . KK_C_LC . ']|$)/u' => 'Yu$1',
103  '/^Я([' . KK_C_UC . ']|$)/u' => 'YA$1', '/^Я([' . KK_C_LC . ']|$)/u' => 'Ya$1',
104  '/^Щ([' . KK_C_UC . ']|$)/u' => 'ŞÇ$1', '/^Щ([' . KK_C_LC . ']|$)/u' => 'Şç$1',
105  # # other ЁЮЯ
106  '/Ё/u' => 'YO', '/ё/u' => 'yo',
107  '/Ю/u' => 'YU', '/ю/u' => 'yu',
108  '/Я/u' => 'YA', '/я/u' => 'ya',
109  '/Щ/u' => 'ŞÇ', '/щ/u' => 'şç',
110  # # soft and hard signs
111  '/[ъЪ]/u' => 'ʺ', '/[ьЬ]/u' => 'ʹ',
112  # # other characters
113  '/А/u' => 'A', '/а/u' => 'a', '/Ә/u' => 'Ä', '/ә/u' => 'ä',
114  '/Б/u' => 'B', '/б/u' => 'b', '/В/u' => 'V', '/в/u' => 'v',
115  '/Г/u' => 'G', '/г/u' => 'g', '/Ғ/u' => 'Ğ', '/ғ/u' => 'ğ',
116  '/Д/u' => 'D', '/д/u' => 'd', '/Е/u' => 'E', '/е/u' => 'e',
117  '/Ж/u' => 'J', '/ж/u' => 'j', '/З/u' => 'Z', '/з/u' => 'z',
118  '/И/u' => 'Ï', '/и/u' => 'ï', '/Й/u' => 'Ý', '/й/u' => 'ý',
119  '/К/u' => 'K', '/к/u' => 'k', '/Қ/u' => 'Q', '/қ/u' => 'q',
120  '/Л/u' => 'L', '/л/u' => 'l', '/М/u' => 'M', '/м/u' => 'm',
121  '/Н/u' => 'N', '/н/u' => 'n', '/Ң/u' => 'Ñ', '/ң/u' => 'ñ',
122  '/О/u' => 'O', '/о/u' => 'o', '/Ө/u' => 'Ö', '/ө/u' => 'ö',
123  '/П/u' => 'P', '/п/u' => 'p', '/Р/u' => 'R', '/р/u' => 'r',
124  '/С/u' => 'S', '/с/u' => 's', '/Т/u' => 'T', '/т/u' => 't',
125  '/У/u' => 'W', '/у/u' => 'w', '/Ұ/u' => 'U', '/ұ/u' => 'u',
126  '/Ү/u' => 'Ü', '/ү/u' => 'ü', '/Ф/u' => 'F', '/ф/u' => 'f',
127  '/Х/u' => 'X', '/х/u' => 'x', '/Һ/u' => 'H', '/һ/u' => 'h',
128  '/Ц/u' => 'C', '/ц/u' => 'c', '/Ч/u' => 'Ç', '/ч/u' => 'ç',
129  '/Ш/u' => 'Ş', '/ш/u' => 'ş', '/Ы/u' => 'I', '/ы/u' => 'ı',
130  '/І/u' => 'İ', '/і/u' => 'i', '/Э/u' => 'É', '/э/u' => 'é',
131  ];
132 
133  $this->mLatn2Cyrl = [
134  # # Punctuation
135  '/#|No\./' => '№',
136  # # Şç
137  '/ŞÇʹ/u' => 'ЩЬ', '/Şçʹ/u' => 'Щь',
138  '/Ş[Çç]/u' => 'Щ', '/şç/u' => 'щ',
139  # # soft and hard signs
140  '/([' . KK_L_UC . '])ʺ([' . KK_L_UC . '])/u' => '$1Ъ$2',
141  '/ʺ([' . KK_L_LC . '])/u' => 'ъ$1',
142  '/([' . KK_L_UC . '])ʹ([' . KK_L_UC . '])/u' => '$1Ь$2',
143  '/ʹ([' . KK_L_LC . '])/u' => 'ь$1',
144  '/ʺ/u' => 'ъ',
145  '/ʹ/u' => 'ь',
146  # # Ye Yo Yu Ya.
147  '/Y[Ee]/u' => 'Е', '/ye/u' => 'е',
148  '/Y[Oo]/u' => 'Ё', '/yo/u' => 'ё',
149  '/Y[UWuw]/u' => 'Ю', '/y[uw]/u' => 'ю',
150  '/Y[Aa]/u' => 'Я', '/ya/u' => 'я',
151  # # other characters
152  '/A/u' => 'А', '/a/u' => 'а', '/Ä/u' => 'Ә', '/ä/u' => 'ә',
153  '/B/u' => 'Б', '/b/u' => 'б', '/C/u' => 'Ц', '/c/u' => 'ц',
154  '/Ç/u' => 'Ч', '/ç/u' => 'ч', '/D/u' => 'Д', '/d/u' => 'д',
155  '/E/u' => 'Е', '/e/u' => 'е', '/É/u' => 'Э', '/é/u' => 'э',
156  '/F/u' => 'Ф', '/f/u' => 'ф', '/G/u' => 'Г', '/g/u' => 'г',
157  '/Ğ/u' => 'Ғ', '/ğ/u' => 'ғ', '/H/u' => 'Һ', '/h/u' => 'һ',
158  '/I/u' => 'Ы', '/ı/u' => 'ы', '/İ/u' => 'І', '/i/u' => 'і',
159  '/Ï/u' => 'И', '/ï/u' => 'и', '/J/u' => 'Ж', '/j/u' => 'ж',
160  '/K/u' => 'К', '/k/u' => 'к', '/L/u' => 'Л', '/l/u' => 'л',
161  '/M/u' => 'М', '/m/u' => 'м', '/N/u' => 'Н', '/n/u' => 'н',
162  '/Ñ/u' => 'Ң', '/ñ/u' => 'ң', '/O/u' => 'О', '/o/u' => 'о',
163  '/Ö/u' => 'Ө', '/ö/u' => 'ө', '/P/u' => 'П', '/p/u' => 'п',
164  '/Q/u' => 'Қ', '/q/u' => 'қ', '/R/u' => 'Р', '/r/u' => 'р',
165  '/S/u' => 'С', '/s/u' => 'с', '/Ş/u' => 'Ш', '/ş/u' => 'ш',
166  '/T/u' => 'Т', '/t/u' => 'т', '/U/u' => 'Ұ', '/u/u' => 'ұ',
167  '/Ü/u' => 'Ү', '/ü/u' => 'ү', '/V/u' => 'В', '/v/u' => 'в',
168  '/W/u' => 'У', '/w/u' => 'у', '/Ý/u' => 'Й', '/ý/u' => 'й',
169  '/X/u' => 'Х', '/x/u' => 'х', '/Z/u' => 'З', '/z/u' => 'з',
170  ];
171 
172  $this->mCyLa2Arab = [
173  # # Punctuation -> Arabic
174  '/#|№|No\./u' => '؀', # U+0600
175  '/\,/' => '،', # U+060C
176  '/;/' => '؛', # U+061B
177  '/\?/' => '؟', # U+061F
178  '/%/' => '٪', # U+066A
179  '/\*/' => '٭', # U+066D
180  # # Digits -> Arabic
181  '/0/' => '۰', # U+06F0
182  '/1/' => '۱', # U+06F1
183  '/2/' => '۲', # U+06F2
184  '/3/' => '۳', # U+06F3
185  '/4/' => '۴', # U+06F4
186  '/5/' => '۵', # U+06F5
187  '/6/' => '۶', # U+06F6
188  '/7/' => '۷', # U+06F7
189  '/8/' => '۸', # U+06F8
190  '/9/' => '۹', # U+06F9
191  # # Cyrillic -> Arabic
192  '/Аллаһ/ui' => 'ﷲ',
193  '/([АӘЕЁИОӨҰҮЭЮЯЪЬ])е/ui' => '$1يە',
194  '/[еэ]/ui' => 'ە', '/[ъь]/ui' => '',
195  '/[аә]/ui' => 'ا', '/[оө]/ui' => 'و', '/[ұү]/ui' => 'ۇ', '/[ыі]/ui' => 'ى',
196  '/[и]/ui' => 'ىي', '/ё/ui' => 'يو', '/ю/ui' => 'يۋ', '/я/ui' => 'يا', '/[й]/ui' => 'ي',
197  '/ц/ui' => 'تس', '/щ/ui' => 'شش',
198  '/һ/ui' => 'ح', '/ч/ui' => 'تش',
199  # '/һ/ui' => 'ھ', '/ч/ui' => 'چ',
200  '/б/ui' => 'ب', '/в/ui' => 'ۆ', '/г/ui' => 'گ', '/ғ/ui' => 'ع',
201  '/д/ui' => 'د', '/ж/ui' => 'ج', '/з/ui' => 'ز', '/к/ui' => 'ك',
202  '/қ/ui' => 'ق', '/л/ui' => 'ل', '/м/ui' => 'م', '/н/ui' => 'ن',
203  '/ң/ui' => 'ڭ', '/п/ui' => 'پ', '/р/ui' => 'ر', '/с/ui' => 'س',
204  '/т/ui' => 'ت', '/у/ui' => 'ۋ', '/ф/ui' => 'ف', '/х/ui' => 'ح',
205  '/ш/ui' => 'ش',
206  # # Latin -> Arabic // commented for now...
207  /*'/Allah/ui' => 'ﷲ',
208  '/[eé]/ui' => 'ە', '/[yý]/ui' => 'ي', '/[ʺʹ]/ui' => '',
209  '/[aä]/ui' => 'ا', '/[oö]/ui' => 'و', '/[uü]/ui' => 'ۇ',
210  '/[ï]/ui' => 'ىي', '/[ıIiİ]/u' => 'ى',
211  '/c/ui' => 'تس',
212  '/ç/ui' => 'تش', '/h/ui' => 'ح',
213  #'/ç/ui' => 'چ', '/h/ui' => 'ھ',
214  '/b/ui' => 'ب','/d/ui' => 'د',
215  '/f/ui' => 'ف', '/g/ui' => 'گ', '/ğ/ui' => 'ع',
216  '/j/ui' => 'ج', '/k/ui' => 'ك', '/l/ui' => 'ل', '/m/ui' => 'م',
217  '/n/ui' => 'ن', '/ñ/ui' => 'ڭ', '/p/ui' => 'پ', '/q/ui' => 'ق',
218  '/r/ui' => 'ر', '/s/ui' => 'س', '/ş/ui' => 'ش', '/t/ui' => 'ت',
219  '/v/ui' => 'ۆ', '/w/ui' => 'ۋ', '/x/ui' => 'ح', '/z/ui' => 'ز',*/
220  ];
221  }
222 
231  public function translate( $text, $toVariant ) {
232  $text = parent::translate( $text, $toVariant );
233 
234  switch ( $toVariant ) {
235  case 'kk-cyrl':
236  case 'kk-kz':
237  $letters = KK_L_UC . KK_L_LC . 'ʺʹ#0123456789';
238  break;
239  case 'kk-latn':
240  case 'kk-tr':
241  $letters = KK_C_UC . KK_C_LC . '№0123456789';
242  break;
243  case 'kk-arab':
244  case 'kk-cn':
245  $letters = KK_C_UC . KK_C_LC . /*KK_L_UC.KK_L_LC.'ʺʹ'.*/',;\?%\*№0123456789';
246  break;
247  default:
248  return $text;
249  }
250  // disable conversion variables like $1, $2...
251  $varsfix = '\$[0-9]';
252 
253  $matches = preg_split(
254  '/' . $varsfix . '[^' . $letters . ']+/u',
255  $text,
256  -1,
257  PREG_SPLIT_OFFSET_CAPTURE
258  );
259 
260  $mstart = 0;
261  $ret = '';
262 
263  foreach ( $matches as $m ) {
264  $ret .= substr( $text, $mstart, $m[1] - $mstart );
265  $ret .= $this->regsConverter( $m[0], $toVariant );
266  $mstart = $m[1] + strlen( $m[0] );
267  }
268 
269  return $ret;
270  }
271 
277  private function regsConverter( $text, $toVariant ) {
278  if ( $text == '' ) {
279  return $text;
280  }
281 
282  switch ( $toVariant ) {
283  case 'kk-arab':
284  case 'kk-cn':
285  $letters = KK_C_LC . KK_C_UC; /*.KK_L_LC.KK_L_UC*/
286  $front = 'әөүіӘӨҮІ'; /*.'äöüiÄÖÜİ'*/
287  $excludes = 'еэгғкқЕЭГҒКҚ'; /*.'eégğkqEÉGĞKQ'*/
288  // split text to words
289  $matches = preg_split( '/[\b\s\-\.:]+/', $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
290  $mstart = 0;
291  $ret = '';
292  foreach ( $matches as $m ) {
293  $ret .= substr( $text, $mstart, $m[1] - $mstart );
294  // is matched the word to front vowels?
295  // exclude a words matched to е, э, г, к, к, қ,
296  // them should be without hamza
297  if ( preg_match( '/[' . $front . ']/u', $m[0] ) &&
298  !preg_match( '/[' . $excludes . ']/u', $m[0] )
299  ) {
300  $ret .= preg_replace( '/[' . $letters . ']+/u', H_HAMZA . '$0', $m[0] );
301  } else {
302  $ret .= $m[0];
303  }
304  $mstart = $m[1] + strlen( $m[0] );
305  }
306  $text =& $ret;
307  foreach ( $this->mCyLa2Arab as $pat => $rep ) {
308  $text = preg_replace( $pat, $rep, $text );
309  }
310  return $text;
311  case 'kk-latn':
312  case 'kk-tr':
313  foreach ( $this->mCyrl2Latn as $pat => $rep ) {
314  $text = preg_replace( $pat, $rep, $text );
315  }
316  return $text;
317  case 'kk-cyrl':
318  case 'kk-kz':
319  foreach ( $this->mLatn2Cyrl as $pat => $rep ) {
320  $text = preg_replace( $pat, $rep, $text );
321  }
322  return $text;
323  default:
324  return $text;
325  }
326  }
327 
332  public function convertCategoryKey( $key ) {
333  return $this->autoConvert( $key, 'kk' );
334  }
335 }
KkConverter\loadRegs
loadRegs()
Definition: KkConverter.php:93
KkConverter\$mLatn2Cyrl
$mLatn2Cyrl
Definition: KkConverter.php:39
KkConverter\convertCategoryKey
convertCategoryKey( $key)
Definition: KkConverter.php:332
KkConverter\postLoadTables
postLoadTables()
Hook for post processing after conversion tables are loaded.
Definition: KkConverter.php:87
KK_L_LC
const KK_L_LC
Definition: KkConverter.php:27
KkConverter
Kazakh (Қазақша) converter routines.
Definition: KkConverter.php:37
$matches
$matches
Definition: NoLocalSettings.php:24
KkConverter\translate
translate( $text, $toVariant)
It translates text into variant.
Definition: KkConverter.php:231
KK_C_LC
const KK_C_LC
Definition: KkConverter.php:25
KkConverter\$mCyrl2Latn
$mCyrl2Latn
Definition: KkConverter.php:39
ReplacementArray
Wrapper around strtr() that holds replacements.
Definition: ReplacementArray.php:24
KkConverter\__construct
__construct( $langobj)
Definition: KkConverter.php:44
KK_L_UC
const KK_L_UC
Definition: KkConverter.php:26
LanguageConverter\autoConvert
autoConvert( $text, $toVariant=false)
Dictionary-based conversion.
Definition: LanguageConverter.php:398
H_HAMZA
const H_HAMZA
Definition: KkConverter.php:29
LanguageConverterSpecific
A class that extends LanguageConverter with specific behaviour.
Definition: LanguageConverterSpecific.php:26
KkConverter\regsConverter
regsConverter( $text, $toVariant)
Definition: KkConverter.php:277
KK_C_UC
const KK_C_UC
Definition: KkConverter.php:24
KkConverter\loadDefaultTables
loadDefaultTables()
Load default conversion tables.
Definition: KkConverter.php:65
KkConverter\$mCyLa2Arab
$mCyLa2Arab
Definition: KkConverter.php:39