Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 133
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
GrammarTyv
0.00% covered (danger)
0.00%
0 / 133
0.00% covered (danger)
0.00%
0 / 1
4290
0.00% covered (danger)
0.00%
0 / 1
 process
0.00% covered (danger)
0.00%
0 / 133
0.00% covered (danger)
0.00%
0 / 1
4290
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace Wikimedia\Leximorph\Handler\Overrides\Grammar;
8
9use Wikimedia\Leximorph\Handler\Overrides\IGrammarTransformer;
10
11/**
12 * GrammarTyv
13 *
14 * Implements grammar transformations for Tuvan (tyv).
15 *
16 * These rules don't cover the whole grammar of the language.
17 * This logic was originally taken from MediaWiki Core.
18 * Thanks to all contributors.
19 *
20 * @since     1.45
21 * @author    Doğu Abaris (abaris@null.net)
22 * @license   https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later
23 */
24class GrammarTyv implements IGrammarTransformer {
25    /**
26     * Applies Tuvan-specific grammatical transformations.
27     *
28     * @param string $word The word to process.
29     * @param string $case The grammatical case.
30     *
31     * @since 1.45
32     * @return string The processed word.
33     */
34    public function process( string $word, string $case ): string {
35        // Set up some constants...
36        $allVowels = [ "е", "и", "э", "ө", "ү", "а", "ё", "о", "у", "ы", "ю", "я", ];
37        $frontVowels = [ "е", "и", "э", "ө", "ү", ];
38        $backVowels = [ "а", "ё", "о", "у", "ы", "ю", "я", ];
39        $unroundFrontVowels = [ "е", "и", "э", ];
40        $roundFrontVowels = [ "ө", "ү", ];
41        $unroundBackVowels = [ "а", "ы", "я", ];
42        $roundBackVowels = [ "ё", "о", "у", "ю", ];
43        $unvoicedPhonemes = [ "т", "п", "с", "ш", "к", "ч", "х", ];
44        $directiveUnvoicedStems = [ "т", "п", "с", "ш", "к", "ч", "х", "л", "м", "н", "ң", ];
45        $directiveVoicedStems = [ "д", "б", "з", "ж", "г", "р", "й", ];
46
47        // Put the word in a form we can play with since we're using UTF-8
48        $ar = mb_str_split( $word, 1 );
49
50        // Here's the last letter in the word
51        $wordEnding = end( $ar );
52
53        // Find the last vowel in the word
54        $wordLastVowel = null;
55        for ( $i = count( $ar ); $i--; ) {
56            if ( in_array( $ar[$i], $allVowels, true ) ) {
57                $wordLastVowel = $ar[$i];
58                break;
59            }
60        }
61
62        // Now convert the word
63        switch ( $case ) {
64            case "genitive":
65                if ( in_array( $wordEnding, $unvoicedPhonemes ) ) {
66                    if ( in_array( $wordLastVowel, $roundFrontVowels ) ) {
67                        $word .= "түң";
68                    } elseif ( in_array( $wordLastVowel, $unroundFrontVowels ) ) {
69                        $word .= "тиң";
70                    } elseif ( in_array( $wordLastVowel, $roundBackVowels ) ) {
71                        $word .= "туң";
72                    } elseif ( in_array( $wordLastVowel, $unroundBackVowels ) ) {
73                        $word .= "тың";
74                    }
75                } elseif ( $wordEnding === "л" ) {
76                    if ( in_array( $wordLastVowel, $roundFrontVowels ) ) {
77                        $word .= "дүң";
78                    } elseif ( in_array( $wordLastVowel, $unroundFrontVowels ) ) {
79                        $word .= "диң";
80                    } elseif ( in_array( $wordLastVowel, $roundBackVowels ) ) {
81                        $word .= "дуң";
82                    } elseif ( in_array( $wordLastVowel, $unroundBackVowels ) ) {
83                        $word .= "дың";
84                    }
85                } else {
86                    if ( in_array( $wordLastVowel, $roundFrontVowels ) ) {
87                        $word .= "нүң";
88                    } elseif ( in_array( $wordLastVowel, $unroundFrontVowels ) ) {
89                        $word .= "ниң";
90                    } elseif ( in_array( $wordLastVowel, $roundBackVowels ) ) {
91                        $word .= "нуң";
92                    } elseif ( in_array( $wordLastVowel, $unroundBackVowels ) ) {
93                        $word .= "ның";
94                    }
95                }
96                break;
97
98            case "dative":
99                if ( in_array( $wordEnding, $unvoicedPhonemes ) ) {
100                    if ( in_array( $wordLastVowel, $frontVowels ) ) {
101                        $word .= "ке";
102                    } elseif ( in_array( $wordLastVowel, $backVowels ) ) {
103                        $word .= "ка";
104                    }
105                } else {
106                    if ( in_array( $wordLastVowel, $frontVowels ) ) {
107                        $word .= "ге";
108                    } elseif ( in_array( $wordLastVowel, $backVowels ) ) {
109                        $word .= "га";
110                    }
111                }
112                break;
113
114            case "accusative":
115                if ( in_array( $wordEnding, $unvoicedPhonemes ) ) {
116                    if ( in_array( $wordLastVowel, $roundFrontVowels ) ) {
117                        $word .= "тү";
118                    } elseif ( in_array( $wordLastVowel, $unroundFrontVowels ) ) {
119                        $word .= "ти";
120                    } elseif ( in_array( $wordLastVowel, $roundBackVowels ) ) {
121                        $word .= "ту";
122                    } elseif ( in_array( $wordLastVowel, $unroundBackVowels ) ) {
123                        $word .= "ты";
124                    }
125                } elseif ( $wordEnding === "л" ) {
126                    if ( in_array( $wordLastVowel, $roundFrontVowels ) ) {
127                        $word .= "дү";
128                    } elseif ( in_array( $wordLastVowel, $unroundFrontVowels ) ) {
129                        $word .= "ди";
130                    } elseif ( in_array( $wordLastVowel, $roundBackVowels ) ) {
131                        $word .= "ду";
132                    } elseif ( in_array( $wordLastVowel, $unroundBackVowels ) ) {
133                        $word .= "ды";
134                    }
135                } else {
136                    if ( in_array( $wordLastVowel, $roundFrontVowels ) ) {
137                        $word .= "нү";
138                    } elseif ( in_array( $wordLastVowel, $unroundFrontVowels ) ) {
139                        $word .= "ни";
140                    } elseif ( in_array( $wordLastVowel, $roundBackVowels ) ) {
141                        $word .= "ну";
142                    } elseif ( in_array( $wordLastVowel, $unroundBackVowels ) ) {
143                        $word .= "ны";
144                    }
145                }
146                break;
147
148            case "locative":
149                if ( in_array( $wordEnding, $unvoicedPhonemes ) ) {
150                    if ( in_array( $wordLastVowel, $frontVowels ) ) {
151                        $word .= "те";
152                    } elseif ( in_array( $wordLastVowel, $backVowels ) ) {
153                        $word .= "та";
154                    }
155                } else {
156                    if ( in_array( $wordLastVowel, $frontVowels ) ) {
157                        $word .= "де";
158                    } elseif ( in_array( $wordLastVowel, $backVowels ) ) {
159                        $word .= "да";
160                    }
161                }
162                break;
163
164            case "ablative":
165                if ( in_array( $wordEnding, $unvoicedPhonemes ) ) {
166                    if ( in_array( $wordLastVowel, $frontVowels ) ) {
167                        $word .= "тен";
168                    } elseif ( in_array( $wordLastVowel, $backVowels ) ) {
169                        $word .= "тан";
170                    }
171                } else {
172                    if ( in_array( $wordLastVowel, $frontVowels ) ) {
173                        $word .= "ден";
174                    } elseif ( in_array( $wordLastVowel, $backVowels ) ) {
175                        $word .= "дан";
176                    }
177                }
178                break;
179
180            case "directive1":
181                if ( in_array( $wordEnding, $directiveVoicedStems ) ) {
182                    $word .= "же";
183                } elseif ( in_array( $wordEnding, $directiveUnvoicedStems ) ) {
184                    $word .= "че";
185                }
186                break;
187
188            case "directive2":
189                if ( in_array( $wordEnding, $unvoicedPhonemes ) ) {
190                    if ( in_array( $wordLastVowel, $roundFrontVowels ) ) {
191                        $word .= "түве";
192                    } elseif ( in_array( $wordLastVowel, $unroundFrontVowels ) ) {
193                        $word .= "тиве";
194                    } elseif ( in_array( $wordLastVowel, $roundBackVowels ) ) {
195                        $word .= "туве";
196                    } elseif ( in_array( $wordLastVowel, $unroundBackVowels ) ) {
197                        $word .= "тыве";
198                    }
199                } else {
200                    if ( in_array( $wordLastVowel, $roundFrontVowels ) ) {
201                        $word .= "дүве";
202                    } elseif ( in_array( $wordLastVowel, $unroundFrontVowels ) ) {
203                        $word .= "диве";
204                    } elseif ( in_array( $wordLastVowel, $roundBackVowels ) ) {
205                        $word .= "дуве";
206                    } elseif ( in_array( $wordLastVowel, $unroundBackVowels ) ) {
207                        $word .= "дыве";
208                    }
209                }
210                break;
211
212            default:
213                break;
214        }
215
216        return $word;
217    }
218}