summaryrefslogtreecommitdiff
path: root/navit/linguistics.c
blob: 67b789482f7d4ef842e1b6070677187936afd1ed (plain)
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#include <string.h>
#include <stdio.h>
#include <glib.h>
#include "debug.h"
#include "linguistics.h"

/* To have linguistics_casefold(linguistics_expand_special(s,i)) equal to linguistics_expand_special(linguistics_casefold(s),i),
 * please always specify here lower case expansions for special letters not having case variants (like german ß).*/
static const char *special[][3]= {
    /* Capital Diacritics */
    /* ¨ Diaresis */
    {"Ä","A","AE"},
    {"Ë","E"},
    {"Ï","I"},
    {"Ö","O","OE"},
    {"Ü","U","UE"},
    {"Ÿ","Y"},
    /* ˝ Double Acute Accent */
    {"Ő","O","Ö"},
    {"Ű","U","Ü"},
    /* ´ Acute Accent */
    {"Á","A"},
    {"Ć","C"},
    {"É","E"},
    {"Í","I"},
    {"Ĺ","L"},
    {"Ń","N"},
    {"Ó","O"},
    {"Ŕ","R"},
    {"Ś","S"},
    {"Ú","U"},
    {"Ý","Y"},
    {"Ź","Z"},
    /* ˛ Ogonek (nosinė) */
    {"Ą","A"},
    {"Ę","E"},
    {"Į","I"},
    {"Ų","U"},
    /* ˙ Dot */
    {"Ċ","C"},
    {"Ė","E"},
    {"Ġ","G"},
    {"İ","I"},
    {"Ŀ","L"},
    {"Ż","Z"},
    /* – Stroke */
    {"Đ","D","DJ"}, /* Croatian Dj, not to be confused with the similar-looking Icelandic Eth */
    {"Ħ","H"},
    {"Ł","L"},
    {"Ŧ","T"},
    /* ˚ Ring */
    {"Å","A","AA"},
    {"Ů","U"},
    /* ˇ Caron (haček, paukščiukas) */
    {"Č","C"},
    {"Ď","D"},
    {"Ě","E"},
    {"Ľ","L"},
    {"Ň","N"},
    {"Ř","R"},
    {"Š","S"},
    {"Ť","T"},
    {"Ž","Z"},
    /* / Slash */
    {"Ø","O","OE"},
    /* ¯ Macron */
    {"Ā","A","AA"},
    {"Ē","E","EE"},
    {"Ī","I","II"},
    {"Ō","O","OO"},
    {"Ū","U","UU"},
    /* ˘ Brevis */
    {"Ă","A"},
    {"Ĕ","E"},
    {"Ğ","G"},
    {"Ĭ","I"},
    {"Ŏ","O"},
    {"Ŭ","U"},
    /* ^ Circumflex */
    {"Â","A"},
    {"Ĉ","C"},
    {"Ê","E"},
    {"Ĝ","G"},
    {"Ĥ","H"},
    {"Î","I"},
    {"Ĵ","J"},
    {"Ô","O"},
    {"Ŝ","S"},
    {"Û","U"},
    {"Ŵ","W"},
    {"Ŷ","Y"},
    /* ¸ Cedilla */
    {"Ç","C"},
    {"Ģ","G","GJ"},
    {"Ķ","K","KJ"},
    {"Ļ","L","LJ"},
    {"Ņ","N","NJ"},
    {"Ŗ","R"},
    {"Ş","S"},
    {"Ţ","T"},
    /* ~ Tilde */
    {"Ã","A"},
    {"Ĩ","I"},
    {"Ñ","N"},
    {"Õ","O"},
    {"Ũ","U"},
    /* ` Grave */
    {"À","A"},
    {"È","E"},
    {"Ì","I"},
    {"Ò","O"},
    {"Ù","U"},
    /* ligatures */
    {"Æ","A","AE"},
    {"IJ","IJ"},
    {"Œ","O","OE"},
    /* special letters */
    {"Ð","D","DH"}, /* Icelandic Eth, not to be confused with the similar-looking Croatian Dj */
    {"Ŋ","N","NG"},
    {"Þ","T","TH"},
    /* Small Diacritics */
    /* ¨ Diaresis */
    {"ä","a","ae"},
    {"ë","e"},
    {"ï","i"},
    {"ö","o","oe"},
    {"ü","u","ue"},
    {"ÿ","y"},
    /* ˝ Double Acute Accent */
    {"ő","o","ö"},
    {"ű","u","ü"},
    /* ´ Acute Accent */
    {"á","a"},
    {"ć","c"},
    {"é","e"},
    {"í","i"},
    {"ĺ","l"},
    {"ń","n"},
    {"ó","o"},
    {"ŕ","r"},
    {"ś","s"},
    {"ú","u"},
    {"ý","y"},
    {"ź","z"},
    /* ˛ Ogonek (nosinė) */
    {"ą","a"},
    {"ę","e"},
    {"į","i"},
    {"ų","u"},
    /* ˙ Dot (and dotless i) */
    {"ċ","c"},
    {"ė","e"},
    {"ġ","g"},
    {"ı","i"},
    {"ŀ","l"},
    {"ż","z"},
    /* – Stroke */
    {"đ","d","dj"},
    {"ħ","h"},
    {"ł","l"},
    {"ŧ","t"},
    /* ˚ Ring */
    {"å","a", "aa"},
    {"ů","u"},
    /* ˇ Caron (haček, paukščiukas) */
    {"č","c"},
    {"ď","d"},
    {"ě","e"},
    {"ľ","l"},
    {"ň","n"},
    {"ř","r"},
    {"š","s"},
    {"ť","t"},
    {"ž","z"},
    /* / Slash */
    {"ø","o", "oe"},
    /* Macron */
    {"ā","a","aa"},
    {"ē","e","ee"},
    {"ī","i","ii"},
    {"ō","o","oo"},
    {"ū","u","uu"},
    /* ˘ Brevis */
    {"ă","a"},
    {"ĕ","e"},
    {"ğ","g"},
    {"ĭ","i"},
    {"ŏ","o"},
    {"ŭ","u"},
    /* ^ Circumflex */
    {"â","a"},
    {"ĉ","c"},
    {"ê","e"},
    {"ĝ","g"},
    {"ĥ","h"},
    {"î","i"},
    {"ĵ","j"},
    {"ô","o"},
    {"ŝ","s"},
    {"û","u"},
    {"ŵ","w"},
    {"ŷ","y"},
    /* ¸ Cedilla */
    {"ç","c"},
    {"ģ","g","gj"},
    {"ķ","k","kj"},
    {"ļ","l","lj"},
    {"ņ","n","nj"},
    {"ŗ","r"},
    {"ş","s"},
    {"ţ","t"},
    /* ~ Tilde */
    {"ã","a"},
    {"ĩ","i"},
    {"õ","o"},
    {"ñ","n"},
    {"ũ","u"},
    /* ` Grave */
    {"à","a"},
    {"è","e"},
    {"ì","i"},
    {"ò","o"},
    {"ù","u"},
    /* ligatures */
    {"æ","a","ae"},
    {"ij","ij"},
    {"œ","o","oe"},
    {"ß","s","ss"},
    /* special letters */
    {"ð","d","dh"},
    {"ŋ","n","ng"},
    {"þ","t","th"},

    /* Cyrillic capital */
    {"Ё","Е"},
    {"І","I"},
    {"Ї","I"},
    {"Ў","У"},
    {"Є","Е","Э"},
    {"Ґ","Г"},
    {"Ѓ","Г"},
    {"Ђ","Д"},
    {"Ќ","К"},
    {"Љ","Л","ЛЬ"},
    {"Њ","Н","НЬ"},
    {"Џ","Ц"},

    /* Cyrillic small */
    {"ё","е"},
    {"і","i"},
    {"ї","i"},
    {"ў","у"},
    {"є","е","э"},
    {"ґ","г"},
    {"ѓ","г"},
    {"ђ","д"},
    {"ќ","к"},
    {"љ","л","ль"},
    {"њ","н","нь"},
    {"џ","ц"},

};

/* Array of strings for case conversion
 * Even elements of array are strings of upper-case letters
 * Odd elements of array are strings of lower-case letters, in the order corresponding to directly preceeding even element.
 * Last element of array should be NULL.
 */
static const char *upperlower[]= {
    /*Latin diacritics*/
    "ÄËÏÖÜŸŐŰÁĆÉÍĹŃÓŔŚÚÝŹĄĘĮŲĊĖĠİĿŻĐĦŁŦÅŮČĎĚĽŇŘŠŤŽØĀĒĪŌŪĂĔĞĬŎŬÂĈÊĜĤÎĴÔŜÛŴŶÇĢĶĻŅŖŞŢÃĨÑÕŨÀÈÌÒÙÆIJŒÐŊÞ",
    "äëïöüÿőűáćéíĺńóŕśúýźąęįųċėġıŀżđħłŧåůčďěľňřšťžøāēīōūăĕğĭŏŭâĉêĝĥîĵôŝûŵŷçģķļņŗşţãĩõñũàèìòùæijœðŋþ",
    /*Cyrillic*/
    "АБВГҐЃДЂЕЄЁЖЗИЙКЌЛЉМНЊОПРСТУФХЦЏЧШЩЪЫЬЭЮЯІЇЎ",
    "абвгґѓдђеєёжзийкќлљмнњопрстуфхцџчшщъыьэюяіїў",

    NULL
};

static GHashTable *casefold_hash, *special_hash;


/*
 * @brief Prepare an utf-8 string for case insensitive comparison.
 * @param in String to prepeare.
 * @return String prepared for case insensitive search. Result shoud be g_free()d after use.
 */
char* linguistics_casefold(const char *in) {
    int len=strlen(in);
    const char *src=in;
    char *ret=g_new(char,len+1);
    char *dest=ret;
    char buf[10];
    while(*src && dest-ret<len) {
        if(*src>='A' && *src<='Z') {
            *dest++=*src++ - 'A' + 'a';
        } else if (!(*src&128)) {
            *dest++=*src++;
        } else {
            int charlen;
            char *tmp, *folded;
            tmp=g_utf8_find_next_char(src,NULL);
            charlen=tmp-src+1;
            g_strlcpy(buf,src,charlen>10?10:charlen);
            folded=g_hash_table_lookup(casefold_hash,buf);
            if(folded) {
                while(*folded && dest-ret<len)
                    *dest++=*folded++;
                src=tmp;
            } else {
                while(src<tmp && dest-ret<len)
                    *dest++=*src++;
            }
        }
    }
    *dest=0;
    if(*src)
        dbg(lvl_error,"Casefolded string for '%s' needs extra space, result is truncated to '%s'.",in,ret);
    return ret;
}

static char** linguistics_get_special(const char *str, const char *end) {
    char *buf;
    int len;
    if(!end)
        end=g_utf8_find_next_char(str,NULL);
    len=end-str+1;
    buf=g_alloca(len);
    g_strlcpy(buf,str,len);
    return g_hash_table_lookup(special_hash,buf);
}

/**
 * @brief Compare two strings, trying to replace special characters (e.g. umlauts) in first string with plain letters.
 *
 * @param s1 First string to process, for example, an item name from the map. Will be linguistics_casefold()ed before comparison.
 * @param s2 Second string to process, usually user supplied search string. Should be linguistics_casefold()ed before calling this function.
 * @param mode set to composition of linguistics_cmp_mode flags to have s1 linguistics_expand_special()ed, allow matches shorter than whole s1, or
 * @param let matches start from any word boundary within s1
 * @returns 0 when strings are equal
 */
int linguistics_compare(const char *s1, const char *s2, enum linguistics_cmp_mode mode) {
    int ret=0;
    int i;
    int s2len=strlen(s2);
    char *s1f;
    /* Calling linguistics_casefold() before linguistics_expand_special() requires that result is independent of calling order. This seems
       to be true at the time of writing this comment. */
    s1f=linguistics_casefold(s1);
    for(i=0; i<3; i++) {
        char *s, *word;
        if(i>0)
            s=linguistics_expand_special(s1f,i);
        else
            s=s1f;
        word=s;
        while(word) {
            if(mode & linguistics_cmp_partial)
                ret=strncmp(word,s2,s2len);
            else
                ret=strcmp(word,s2);
            if(!ret || !(mode & linguistics_cmp_words))
                break;
            word=linguistics_next_word(word);
        }
        if(i>0)
            g_free(s);
        if(!ret || !(mode & linguistics_cmp_expand))
            break;
    }
    g_free(s1f);
    return ret;
}

/**
 * @brief Replace special characters in string (e.g. umlauts) with plain letters.
 * This is useful e.g. to canonicalize a string for comparison.
 *
 * @param str string to process
 * @param mode Replacement mode. 0=do nothing, 1=replace with single
 * UTF character, 2=replace with multiple letters if the commonly used
 * replacement has multitple letter (e.g. a-umlaut -> ae)
 * @returns copy of string, with characters replaced
 */
char *linguistics_expand_special(const char *str, int mode) {
    const char *in=str;
    char *out,*ret;
    int found=0;
    int ret_len=strlen(str);
    int in_rest=ret_len;
    out=ret=g_strdup(str);
    if (!mode)
        return ret;
    while (*in) {
        char *next=g_utf8_find_next_char(in, NULL);
        int len;
        int match=0;

        if(next)
            len=next-in;
        else
            len=strlen(in);

        in_rest-=len;

        if (len > 1) {
            char **spc=linguistics_get_special(in, next);
            if (spc) {
                const char *replace=spc[mode];
                if (replace) {
                    int replace_len=strlen(replace);
                    if(out-ret+replace_len+in_rest>ret_len) {
                        char *new_ret;
                        ret_len+=(replace_len-len)*10;
                        new_ret=g_realloc(ret,ret_len+1);
                        out=new_ret+(out-ret);
                        ret=new_ret;
                    }
                    dbg(lvl_debug,"found %s %s %d %s %d",in,spc[0],len,replace,replace_len);
                    strcpy(out, replace);
                    out+=replace_len;
                    match=1;
                }
            }
        }
        if (match) {
            found=1;
            in+=len;
        } else {
            while (len-- > 0)
                *out++=*in++;
        }
    }
    *out++='\0';
    if (!found) {
        g_free(ret);
        ret=NULL;
    }
    return ret;
}

char *linguistics_next_word(char *str) {
    int len=strcspn(str, LINGUISTICS_WORD_SEPARATORS_ASCII);
    if (!str[len] || !str[len+1])
        return NULL;
    return str+len+1;
}

int linguistics_search(const char *str) {
    if (!g_ascii_strcasecmp(str,"str"))
        return 0;
    if (!g_ascii_strcasecmp(str,"str."))
        return 0;
    if (!g_ascii_strcasecmp(str,"strasse"))
        return 0;
    if (!g_ascii_strcasecmp(str,"weg"))
        return 0;
    return 1;
}

/**
 * @brief Copy one utf8 encoded char to newly allocated buffer.
 *
 * @param s pointer to the beginning of the char.
 * @return  newly allocated nul-terminated string containing one utf8 encoded character.
 */
static char
*linguistics_dup_utf8_char(const char *s) {
    char *ret, *next;
    next=g_utf8_find_next_char(s,NULL);
    ret=g_new(char, next-s+1);
    g_strlcpy(ret,s,next-s+1);
    return ret;
}

void linguistics_init(void) {
    int i;

    casefold_hash=g_hash_table_new_full(g_str_hash, g_str_equal,g_free,g_free);

    for (i = 0 ; upperlower[i]; i+=2) {
        int j,k;
        for(j=0,k=0; upperlower[i][j] && upperlower[i+1][k];) {
            char *s1=linguistics_dup_utf8_char(upperlower[i]+j);
            char *s2=linguistics_dup_utf8_char(upperlower[i+1]+k);
            g_hash_table_insert(casefold_hash,s1,s2);
            j+=strlen(s1);
            k+=strlen(s2);
        }
    }

    special_hash=g_hash_table_new(g_str_hash, g_str_equal);
    for (i = 0 ; i < sizeof(special)/sizeof(special[0]); i++)
        g_hash_table_insert(special_hash,(gpointer)special[i][0],special[i]);

}

void linguistics_free(void) {
    g_hash_table_destroy(casefold_hash);
    g_hash_table_destroy(special_hash);
    casefold_hash=NULL;
    special_hash=NULL;
}