-
Notifications
You must be signed in to change notification settings - Fork 11
/
german_phonetics.dart
321 lines (303 loc) · 9.06 KB
/
german_phonetics.dart
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import 'package:flutter/material.dart';
/// Copyright (C) 2020 Google Inc. All Rights Reserved.
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// Phonetik für die deutsche Sprache nach dem Kölner Verfahren
/// <p>
/// Die Kölner Phonetik (auch Kölner Verfahren) ist ein phonetischer Algorithmus,
/// der Wörtern nach ihrem Sprachklang eine Zeichenfolge zuordnet, den phonetischen
/// Code. Ziel dieses Verfahrens ist es, gleich klingenden Wörtern den selben Code
/// zuzuordnen, um bei Suchfunktionen eine Ähnlichkeitssuche zu implementieren. Damit
/// ist es beispielsweise möglich, in einer Namensliste Einträge wie "Meier" auch unter
/// anderen Schreibweisen, wie "Maier", "Mayer" oder "Mayr", zu finden.
/// <p>
/// Die Kölner Phonetik ist, im Vergleich zum bekannteren Russell-Soundex-Verfahren,
/// besser auf die deutsche Sprache abgestimmt. Sie wurde 1969 von Postel veröffentlicht.
/// <p>
/// Infos: http://www.uni-koeln.de/phil-fak/phonetik/Lehre/MA-Arbeiten/magister_wilz.pdf
/// <p>
/// Die Umwandlung eines Wortes erfolgt in drei Schritten:
/// <p>
/// 1. buchstabenweise Codierung von links nach rechts entsprechend der Umwandlungstabelle
/// 2. entfernen aller mehrfachen Codes
/// 3. entfernen aller Codes "0" ausser am Anfang
/// <p>
/// Beispiel Der Name "Müller-Lüdenscheidt" wird folgendermaßen kodiert:
/// <p>
/// 1. buchstabenweise Codierung: 60550750206880022
/// 2. entfernen aller mehrfachen Codes: 6050750206802
/// 3. entfernen aller Codes "0": 65752682
/// <p>
/// Umwandlungstabelle:
/// ============================================
/// Buchstabe Kontext Code
/// ------------- ----------------------- ----
/// Rule01 A,E,I,J,O,U,Y 0
/// Rule02 H -
/// Rule03 B 1
/// Rule04 P nicht vor H 1
/// Rule05 D,T nicht vor C,S,Z 2
/// Rule06 F,V,W 3
/// Rule07 P vor H 3
/// Rule08 G,K,Q 4
/// Rule09 C im Wortanfang
/// vor A,H,K,L,O,Q,R,U,X 4
/// Rule10 C vor A,H,K,O,Q,U,X
/// ausser nach S,Z 4
/// Rule11 X nicht nach C,K,Q 48
/// Rule12 L 5
/// Rule13 M,N 6
/// Rule14 R 7
/// Rule15 S,Z 8
/// Rule16 C nach S,Z 8
/// Rule17 C im Wortanfang ausser vor
/// A,H,K,L,O,Q,R,U,X 8
/// Rule18 C nicht vor A,H,K,O,Q,U,X 8
/// Rule19 D,T vor C,S,Z 8
/// Rule20 X nach C,K,Q 8
/// --------------------------------------------
///*
/// ---------------------------------------------------------------------
/// Support/Info/Download: https://github.com/deezaster/germanphonetic
/// ---------------------------------------------------------------------
///
/// @package germanphonetic
/// @version 1.0
/// @author Steffen Halstrick <steffenhalstrick@t-online.de>
/// @copyright /
/// @license /
const String TAG = "PhoneticConverter";
List<Characters> phoneticCode(String input) {
var string = input.trim();
print("$TAG input: $input");
var code = List<Characters>.empty();
if (string.isEmpty) {
return List<Characters>.empty();
}
var word = string.toLowerCase();
word = changeUnnecessary(word);
print("$TAG first optimization: $word");
// removing all special character
word.replaceAll(RegExp("[^A-Za-z0-9 ]"), "");
print("$TAG second optimization: $word");
// don't need to convert String to CharArray
var i;
// Specials at word beginning
if (word[0] == 'c') {
if (word.length == 1) {
code[0] = 8 as Characters;
} else {
// before a,h,k,l,o,q,r,u,x
switch (word[1]) {
case 'a':
case 'h':
case 'k':
case 'l':
case 'o':
case 'q':
case 'r':
case 'u':
case 'x':
code[0] = 4 as Characters;
break;
default:
code[0] = 8 as Characters;
}
}
i = 1;
} else {
i = 0;
}
while (i < word.length) {
switch (word[i]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
code[i] = 0 as Characters;
break;
case 'b':
case 'p':
code[i] = 1 as Characters;
break;
case 'd':
if (i + 1 < word.length) {
switch (word[i + 1]) {
case 'c':
case 's':
case 'z':
code[i] = 8 as Characters;
break;
default:
code[i] = 2 as Characters;
break;
}
} else {
code[i] = 2 as Characters;
}
break;
case 't':
if (i + 1 < word.length) {
switch (word[i + 1]) {
case 'c':
case 's':
case 'z':
code[i] = 8 as Characters;
break;
default:
code[i] = 2 as Characters;
break;
}
} else {
code[i] = 2 as Characters;
}
break;
case 'f':
code[i] = 3 as Characters;
break;
case 'g':
case 'k':
case 'q':
code[i] = 4 as Characters;
break;
case 'c':
if (i + 1 < word.length) {
switch (word[i + 1]) {
case 'a':
case 'h':
case 'k':
case 'o':
case 'q':
case 'u':
case 'x':
switch (word[i - 1]) {
case 's':
case 'z':
code[i] = 8 as Characters;
break;
default:
code[i] = 4 as Characters;
break;
}
break;
default:
code[i] = 8 as Characters;
break;
}
} else {
code[i] = 4 as Characters;
}
break;
case 'x':
if (i > 0) {
switch (word[i - 1]) {
case 'c':
case 'k':
case 'q':
code[i] = 8 as Characters;
break;
default:
code[i] = 48 as Characters;
break;
}
} else {
code[i] = 48 as Characters;
}
break;
case 'l':
code[i] = 5 as Characters;
break;
case 'm':
case 'n':
code[i] = 6 as Characters;
break;
case 'r':
code[i] = 7 as Characters;
break;
case 's':
case 'z':
code[i] = 8 as Characters;
break;
}
i++;
}
print("$TAG unoptimized code = ${code.join()}");
// delete multiple codes
// use regex "(.)(?=.*)(\\1)", "\\1" to find the duplicates and delete the first of its group
// both method are possible
var codeLength = code.join().length;
var list = List<Characters>.empty();
i = 0;
while (i < codeLength) {
if (i + 1 < codeLength) {
if (code[i] == code[i + 1]) {
if (code[i] != 0 as Characters && code[i] != 0) {
list.add(code[i]);
}
i++;
} else {
if (code[i] != 0 as Characters && code[i] != '0') {
list.add(code[i]);
}
}
}
i++;
}
print("$TAG code finished = ${list.join()}");
return list;
}
String changeUnnecessary(String original) {
// Conversion: v->f, w->f, j->i, y->i, ph->f, ä->a, ö->o, ü->u, ß->ss, é->e, è->e, ê->e, à->a, á->a, â->a, ë->e
var text = original;
var regexOne = <String>[];
regexOne.add("ç");
regexOne.add("v");
regexOne.add("w");
regexOne.add("j");
regexOne.add("y");
regexOne.add("ph");
regexOne.add("ä");
regexOne.add("ö");
regexOne.add("ü");
regexOne.add("ß");
regexOne.add("é");
regexOne.add("è");
regexOne.add("ê");
regexOne.add("à");
regexOne.add("á");
regexOne.add("â");
regexOne.add("ë");
var regexTwo = <String>[];
regexTwo.add("c");
regexTwo.add("f");
regexTwo.add("f");
regexTwo.add("i");
regexTwo.add("i");
regexTwo.add("f");
regexTwo.add("a");
regexTwo.add("o");
regexTwo.add("u");
regexTwo.add("ss");
regexTwo.add("e");
regexTwo.add("e");
regexTwo.add("e");
regexTwo.add("a");
regexTwo.add("a");
regexTwo.add("a");
regexTwo.add("e");
for (var i = 0; i <= 16; i++) {
text = original.replaceAll(regexOne[i], regexTwo[i]);
}
return text;
}