Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
GrammarKsh
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 1
930
0.00% covered (danger)
0.00%
0 / 1
 process
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 1
930
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 * GrammarKsh
13 *
14 * Implements grammar transformations for Colognian (ksh).
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 GrammarKsh implements IGrammarTransformer {
25
26    /**
27     * Do not add male wiki families, since that's the default.
28     * No need to add neuter to wikis having names ending in "-wiki".
29     */
30    private const FAMILYGENDER = [
31        'wikipedia' => 'f',
32        'wikiversity' => 'f',
33        'wiktionary' => 'n',
34        'wikibooks' => 'n',
35        'wikiquote' => 'n',
36        'wikisource' => 'n',
37        'wikitravel' => 'n',
38        'wikia' => 'f',
39        'translatewiki.net' => 'n',
40    ];
41
42    /**
43     * Applies Colognian-specific grammatical transformations.
44     *
45     * $case is a sequence of words, each of which is case insensitive.
46     * There must be at least one space character between words.
47     * Only the 1st character of each word is considered.
48     * Word order is irrelevant.
49     *
50     * Possible values specifying the grammatical case are:
51     *    1, Nominative
52     *    2, Genitive
53     *    3, Dative
54     *    4, Accusative, -omitted-
55     *
56     * Possible values specifying the article type are:
57     *    Betoont               focussed or stressed article
58     *    -omitted-             unstressed or unfocused article
59     *
60     * Possible values for the type of genitive are:
61     *    Sing, Iehr            prepositioned genitive = possessive dative
62     *    Vun, Fon, -omitted-   postpositioned genitive = preposition "vun" with dative
63     *
64     * Values of case overrides & prepositions, in the order of precedence:
65     *    Sing, Iehr            possessive dative = prepositioned genitive
66     *    Vun, Fon              preposition "vun" with dative = post positioned genitive
67     *    En, em                preposition "en" with dative
68     *
69     * Values for object gender specifiers of the possessive dative, or
70     * prepositioned genitive, evaluated with "Sing, Iehr" of above only:
71     *    Male                  a singular male object follows
72     *    -omitted-             a non-male or plural object follows
73     *
74     * We currently handle definite articles of the singular only.
75     * There is a full set of test cases at:
76     * http://translatewiki.net/wiki/Portal:Ksh#GRAMMAR_Pr%C3%B6%C3%B6fe
77     * Contents of the leftmost table column can be copied and pasted as
78     * "case" values.
79     *
80     * @param string $word The word to process.
81     * @param string $case The grammatical case.
82     *
83     * @since 1.45
84     * @return string The processed word.
85     */
86    public function process( string $word, string $case ): string {
87        $lord = strtolower( $word );
88        // Nuutnaarel // default
89        $gender = 'm';
90        if ( str_ends_with( $lord, 'wiki' ) ) {
91            // Dat xyz-wiki
92            $gender = 'n';
93        }
94        if ( isset( self::FAMILYGENDER[$lord] ) ) {
95            $gender = self::FAMILYGENDER[$lord];
96        }
97
98        $isGenderFemale = $gender === 'f';
99
100        $case = ' ' . strtolower( $case );
101        if ( preg_match( '/ [is]/', $case ) ) {
102            # däm WikiMaatplaz singe, dä Wikipeedija iere, däm Wikiwööterbooch singe
103            # dem/em WikiMaatplaz singe, de Wikipeedija iere, dem/em Wikiwööterbooch singe
104            # däm WikiMaatplaz sing, dä Wikipeedija ier, däm Wikiwööterbooch sing
105            # dem/em WikiMaatplaz sing, de Wikipeedija ier, dem/em Wikiwööterbooch sing
106            if ( str_contains( $case, ' b' ) ) {
107                if ( $isGenderFemale ) {
108                    $prefix = 'dä';
109                } else {
110                    $prefix = 'däm';
111                }
112            } elseif ( $isGenderFemale ) {
113                $prefix = 'de';
114            } else {
115                $prefix = 'dem';
116            }
117
118            $possessive = $isGenderFemale ? 'ier' : 'sing';
119            $suffix = str_contains( $case, ' m' ) ? 'e' : '';
120
121            $word = $prefix . ' ' . $word . ' ' . $possessive . $suffix . ( str_contains( $case, ' m' ) ? 'e' : '' );
122        } elseif ( str_contains( $case, ' e' ) ) {
123            # en dämm WikiMaatPlaz, en dä Wikipeedija, en dämm Wikiwööterbooch
124            # em WikiMaatplaz, en de Wikipeedija, em Wikiwööterbooch
125            if ( str_contains( $case, ' b' ) ) {
126                $word = 'en ' . ( $isGenderFemale ? 'dä' : 'däm' ) . ' ' . $word;
127            } else {
128                $word = ( $isGenderFemale ? 'en de' : 'em' ) . ' ' . $word;
129            }
130        } elseif ( preg_match( '/ [fv]/', $case ) || preg_match( '/ [2jg]/', $case ) ) {
131            # vun däm WikiMaatplaz, vun dä Wikipeedija, vun däm Wikiwööterbooch
132            # vum WikiMaatplaz, vun de Wikipeedija, vum Wikiwööterbooch
133            if ( str_contains( $case, ' b' ) ) {
134                $word = 'vun ' . ( $isGenderFemale ? 'dä' : 'däm' ) . ' ' . $word;
135            } else {
136                $word = ( $isGenderFemale ? 'vun de' : 'vum' ) . ' ' . $word;
137            }
138        } elseif ( preg_match( '/ [3d]/', $case ) ) {
139            # dämm WikiMaatPlaz, dä Wikipeedija, dämm Wikiwööterbooch
140            # dem/em WikiMaatplaz, de Wikipeedija, dem/em Wikiwööterbooch
141            if ( str_contains( $case, ' b' ) ) {
142                $word = ( $isGenderFemale ? 'dää' : 'dämm' ) . ' ' . $word;
143            } else {
144                $word = ( $isGenderFemale ? 'de' : 'dem' ) . ' ' . $word;
145            }
146        } else {
147            # dä WikiMaatPlaz, di Wikipeedija, dat Wikiwööterbooch
148            # der WikiMaatplaz, de Wikipeedija, et Wikiwööterbooch
149            if ( str_contains( $case, ' b' ) ) {
150                switch ( $gender ) {
151                    case 'm':
152                        $lord = 'dä';
153                        break;
154                    case 'f':
155                        $lord = 'di';
156                        break;
157                    default:
158                        $lord = 'dat';
159                }
160            } else {
161                switch ( $gender ) {
162                    case 'm':
163                        $lord = 'der';
164                        break;
165                    case 'f':
166                        $lord = 'de';
167                        break;
168                    default:
169                        $lord = 'et';
170                }
171            }
172            $word = $lord . ' ' . $word;
173        }
174
175        return $word;
176    }
177}