/
deklination.js
157 lines (157 loc) · 3.61 KB
/
deklination.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var suffixe = {
nominativ : {
maskulinum : '',
neutrum : '',
femininum : 'e',
plural : 'e'
},
akkusativ : {
maskulinum : 'en',
neutrum : '',
femininum : 'e',
plural : 'e'
},
dativ : {
maskulinum : 'em',
neutrum : 'em',
femininum : 'er',
plural : 'en'
},
genitiv : {
maskulinum : 'es',
neutrum : 'es',
femininum : 'er',
plural : 'er'
}
};
var bestimmt_suffixe = {
nominativ : {
maskulinum : 'er',
neutrum : 'es'
},
akkusativ : {
neutrum : 'es'
}
};
var exceptions = {
bestimmt : {
nominativ : {
neutrum : 'as'
},
akkusativ : {
neutrum : 'as'
}
},
relativ : {
nominativ : {
neutrum : 'as'
},
akkusativ : {
neutrum : 'as'
},
dativ : {
plural : 'enen'
},
genitiv : {
maskulinum : 'essen',
neutrum : 'essen',
femininum : 'eren', // derer?
plural : 'eren' // derer?
}
},
gemischte : {
nominativ : {
maskulinum: 'er',
femininum: 'e',
neutrum: 'es'
},
akkusativ: {
femininum: 'e',
neutrum: 'es'
}
},
schwache : {
nominativ : {
maskulinum: 'e',
femininum: 'e',
neutrum: 'e'
},
akkusativ: {
femininum: 'e',
neutrum: 'e'
}
}
};
// We use 'kase' because 'case' is a keyword in JavaScript
function find_suffix(group, gloss, kase, gender) {
if (exceptions[gloss] && exceptions[gloss][kase] && exceptions[gloss][kase][gender]) {
return exceptions[gloss][kase][gender];
}
if (group == 'bestimmt' || (group == 'adjektive' && gloss == 'starke')) {
return (bestimmt_suffixe[kase] || {})[gender] || suffixe[kase][gender];
}
if (group == 'adjektive') {
return 'en';
}
return suffixe[kase][gender];
}
function decline(group, gloss, stem, kase, gender, suffix) {
if (stem == 'ein' && gender == 'plural') {
return '';
} else if (stem == 'eur' && suffix === '') {
return 'eu<b>e</b>r';
} else {
if (gloss == 'bestimmt' || gloss == 'relativ') {
stem = 'd';
if (suffix == 'e') {
return 'die';
}
} else if (group == 'adjektive') {
stem = '';
}
return (stem ? stem : (suffix ? '-' : '')) + suffix;
}
}
function render() {
var menu = $('#stem');
var selection = menu.find('option:selected');
var group = selection.parent('optgroup').attr('label');
var gloss = menu.val();
var stem = selection.text().replace('-', '');
for (var kase in suffixe) {
for (var gender in suffixe[kase]) {
var suffix = find_suffix(group, gloss, kase, gender);
var form = decline(group, gloss, stem, kase, gender, suffix);
if (! form.length || form.slice(-suffix.length) != suffix) {
suffix = '';
}
$('table#bestimmungswoertern tr.' + kase + ' > td.' + gender).html(form).removeClass('e er en em es as eren enen essen').addClass(suffix);
}
}
};
function hashchange() {
var selected = document.location.hash.replace('#', '');
if (selected) {
try {
if ($('#stem').find('option:[value='+selected+']').length) {
$('#stem').val(selected);
} else {
document.location.hash = '';
}
} catch (error) {
document.location.hash = '';
}
} else {
$($('#stem option')[0]).attr('selected', true);
}
render();
};
function menuchange() {
var stem = $('#stem').val();
document.location.hash = stem;
};
$(document).ready(function () {
$('#stem').change(menuchange);
$(window).bind('hashchange', hashchange);
$(window).trigger('hashchange');
});