File: findTable.c

package info (click to toggle)
liblouis 3.0.0-3%2Bdeb9u4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 68,336 kB
  • sloc: ansic: 26,525; sh: 5,113; makefile: 739; perl: 69; python: 42
file content (648 lines) | stat: -rw-r--r-- 14,538 bytes parent folder | download
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/* liblouis Braille Translation and Back-Translation Library

Copyright (C) 2015 Bert Frees
 
This file is part of liblouis.

liblouis is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.

liblouis is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with liblouis. If not, see <http://www.gnu.org/licenses/>.

*/

/**
 * @file
 * @brief Find translation tables
 */

#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include "louis.h"
#include "findTable.h"

/* =============================== LIST =================================== */

typedef struct List
{
  void * head;
  void (* free)(void *);
  struct List * tail;
} List;

/**
 * Returns a list with the element `x' added to `list'. Returns a sorted list
 * if `cmp' is not NULL and if `list' is also sorted. New elements replace
 * existing ones if they are equal according to `cmp'. If `cmp' is NULL,
 * elements are simply prepended to the list. The function `dup' is used to
 * duplicate elements before they are added to the list. If `free' function is
 * used to free elements when they are removed from the list. The returned
 * list must be freed by the caller.
 */
static List *
list_conj(List * list,
	  void * x,
	  int (* cmp)(void *, void *),
	  void * (* dup)(void *),
	  void (* free)(void *))
{
  if (!list)
    {
      list = malloc(sizeof(List));
      list->head = dup ? dup(x) : x;
      list->free = free;
      list->tail = NULL;
      return list;
    }
  else if (!cmp)
    {
      List * l = malloc(sizeof(List));
      l->head = dup ? dup(x) : x;
      l->free = free;
      l->tail = list;
      return l;
    }
  else
    {
      List * l1 = list;
      List * l2 = NULL;
      while (l1)
	{
	  int c = cmp(l1->head, x);
	  if (c > 0)
	    break;
	  else if (c < 0)
	    {
	      l2 = l1;
	      l1 = l2->tail;
	    }
	  else
	    {
	      if (x != l1->head && !dup && free)
		free(x);
	      return list;
	    }
	}
      List * l3 = malloc(sizeof(List));
      l3->head = dup? dup(x) : x;
      l3->free = free;
      l3->tail = l1;
      if (!l2)
	list = l3;
      else
	l2->tail = l3;
      return list;
    }
}

/**
 * Free an instance of type List.
 */
static void
list_free(List * list)
{
  if (list)
    {
      if (list->free)
	list->free(list->head);
      list_free(list->tail);
      free(list);
    }
}

/**
 * Sort a list based on a comparison function.
 */
static List *
list_sort(List * list, int (* cmp)(void *, void *))
{
  List * newList = NULL;
  List * l;
  for (l = list; l; l = l->tail)
    {
      newList = list_conj(newList, l->head, cmp, NULL, l->free);
      l->free = NULL;
    }
  list_free(list);
  return newList;
}

/**
 * Get the size of a list.
 */
static int
list_size(List * list)
{
  int len = 0;
  List * l;
  for (l = list; l; l = l->tail)
    len++;
  return len;
}

/**
 * Convert a list into a NULL terminated array.
 */
static void **
list_toArray(List * list, void * (* dup)(void *))
{
  void ** array;
  List * l;
  int i;
  array = malloc((1 + list_size(list)) * sizeof(void *));
  i = 0;
  for (l = list; l; l = l->tail)
    array[i++] = dup ? dup(l->head) : l->head;
  array[i] = NULL;
  return array;
}

/* ============================== FEATURE ================================= */

typedef struct
{
  char * key;
  char * val;
} Feature;

typedef struct
{
  Feature feature;
  int importance;
} FeatureWithImportance;

typedef struct
{
  char * name;
  List * features;
} TableMeta;

/**
 * Create an instance of type Feature.
 * The `key' and `val' strings are duplicated. Leaving out the `val'
 * argument results in a value of "yes".
 */
static Feature
feature_new(const char * key, const char * val)
{
  static char * YES = "yes";
  Feature f;
  f.key = strdup(key);
  f.val = strdup(val ? val : YES);
  return f;
}

/**
 * Free an instance of type Feature
 */
static void
feature_free(Feature * f)
{
  if (f)
    {
      free(f->key);
      free(f->val);
      free(f);
    }
}

/* ======================================================================== */

/**
 * Sort features based on their keys.
 */
static int
cmpKeys(Feature * f1, Feature * f2)
{
  return strcmp(f1->key, f2->key);
}

/**
 * Compute the match quotient of the features in a query against the features in a table's metadata.
 *
 * The features are assumed to be sorted and to have no duplicate
 * keys. The query's features must be of type FeatureWithImportance.
 * How a feature contributes to the match quotient depends on its
 * importance, on whether the feature is undefined, defined with the
 * same value (positive match), or defined with a different value
 * (negative match), and on the `fuzzy' argument. If the `fuzzy'
 * argument evaluates to true, negative matches and undefined features
 * get a lower penalty.
 */
static int
matchFeatureLists(const List * query, const List * tableFeatures, int fuzzy)
{
  static int POS_MATCH = 10;
  static int NEG_MATCH = -100;
  static int UNDEFINED = -20;
  static int EXTRA = -1;
  static int POS_MATCH_FUZZY = 10;
  static int NEG_MATCH_FUZZY = -25;
  static int UNDEFINED_FUZZY = -5;
  static int EXTRA_FUZZY = -1;
  int posMatch, negMatch, undefined, extra;
  if (!fuzzy)
    {
      posMatch = POS_MATCH;
      negMatch = NEG_MATCH;
      undefined = UNDEFINED;
      extra = EXTRA;
    }
  else
    {
      posMatch = POS_MATCH_FUZZY;
      negMatch = NEG_MATCH_FUZZY;
      undefined = UNDEFINED_FUZZY;
      extra = EXTRA_FUZZY;
    }
  int quotient = 0;
  const List * l1 = query;
  const List * l2 = tableFeatures;
  while (1)
    {
      if (!l1)
	{
	  if (!l2)
	    break;
	  quotient += extra;
	  l2 = l2->tail;
	}
      else if (!l2)
	{
	  quotient += undefined;
	  l1 = l1->tail;
	}
      else
	{
	  int cmp = cmpKeys(l1->head, l2->head);
	  if (cmp < 0)
	    {
	      quotient += undefined;
	      l1 = l1->tail;
	    }
	  else if (cmp > 0)
	    {
	      quotient += extra;
	      l2 = l2->tail;
	    }
	  else
	    {
	      if (strcmp(((Feature *)l1->head)->val, ((Feature *)l2->head)->val) == 0)
		quotient += posMatch;
	      else
		quotient += negMatch;
	      l1 = l1->tail;
	      l2 = l2->tail;
	    }
	}
    }
  return quotient;
}

/**
 * Return true if a character matches [0-9A-Za-z_-]
 */
static int
isIdentChar(char c)
{
  return (c >= '0' && c <= '9')
      || (c >= 'A' && c <= 'Z')
      || (c >= 'a' && c <= 'z')
      || c == '-'
      || c == '_';
}

/**
 * Parse a table query into a list of features. Features defined first get a
 * higher importance.
 */
static List *
parseQuery(const char * query)
{
  List * features = NULL;
  const char * key = NULL;
  const char * val = NULL;
  size_t keySize = 0;
  size_t valSize = 0;
  const char * c;
  int pos = 0;
  while (1)
    {
      c = &query[pos++];
      if (*c == ' ' || *c == '\t' || *c == '\n' | *c == '\0')
	{
	  if (key)
	    {
	      char * k = strndup(key, keySize);
	      char * v = val ? strndup(val, valSize) : NULL;
	      FeatureWithImportance f = { feature_new(k, v), 0 };
	      logMessage(LOG_DEBUG, "Query has feature '%s:%s'", f.feature.key, f.feature.val);
	      features = list_conj(features, memcpy(malloc(sizeof(f)), &f, sizeof(f)),
				   NULL, NULL, (void (*)(void *))feature_free);
	      free(k);
	      free(v);
	      key = val = NULL;
	      keySize = valSize = 0;
	    }
	  if (*c == '\0')
	    break;
	}
      else if (*c == ':')
	{
	  if (!key || val)
	    goto compile_error;
	  else
	    {
	      c = &query[pos++];
	      if (isIdentChar(*c))
		{
		  val = c;
		  valSize = 1;
		}
	      else
		goto compile_error;
	    }
	}
      else if (isIdentChar(*c))
	{
	  if (val)
	    valSize++;
	  else if (key)
	    keySize++;
	  else
	    {
	      key = c;
	      keySize = 1;
	    }
	}
      else
	goto compile_error;
    }
  int k = 1;
  List * l;
  for (l = features; l; l = l->tail)
    {
      FeatureWithImportance * f = l->head;
      f->importance = k++;
    }
  return list_sort(features, (int (*)(void *, void *))cmpKeys);
 compile_error:
  logMessage(LOG_ERROR, "Unexpected character '%c' at position %d", c, pos);
  list_free(features);
  return NULL;
}

/**
 * Convert a widechar string to a normal string.
 */
static char *
widestrToStr(const widechar * str, size_t n)
{
  char * result = malloc((1 + n) * sizeof(char));
  int k;
  for (k = 0; k < n; k++)
    result[k] = str[k];
  result[k] = '\0';
  return result;
}

/**
 * Extract a list of features from a table.
 */
static List *
analyzeTable(const char * table)
{
  static char fileName[MAXSTRING];
  char ** resolved;
  List * features = NULL;
  FileInfo info;
  int k;
  resolved = resolveTable(table, NULL);
  if (resolved == NULL)
    {
      logMessage(LOG_ERROR, "Cannot resolve table '%s'", table);
      return NULL;
    }
  sprintf(fileName, "%s", *resolved);
  k = 0;
  for (k = 0; resolved[k]; k++)
    free(resolved[k]);
  free(resolved);
  if (k > 1)
    {
      logMessage(LOG_ERROR, "Table '%s' resolves to more than one file", table);
      return NULL;
    }
  info.fileName = fileName;
  info.encoding = noEncoding;
  info.status = 0;
  info.lineNumber = 0;
  if ((info.in = fopen(info.fileName, "rb")))
    {
      while (getALine(&info))
	{
	  if (info.linelen == 0);
	  else if (info.line[0] == '#')
	    {
	      if (info.linelen >= 2 && info.line[1] == '+')
		{
		  widechar * key = NULL;
		  widechar * val = NULL;
		  size_t keySize = 0;
		  size_t valSize = 0;
		  info.linepos = 2;
		  if (info.linepos < info.linelen && isIdentChar(info.line[info.linepos]))
		    {
		      key = &info.line[info.linepos];
		      keySize = 1;
		      info.linepos++;
		      while (info.linepos < info.linelen && isIdentChar(info.line[info.linepos]))
			{
			  keySize++;
			  info.linepos++;
			}
		      if (info.linepos < info.linelen && info.line[info.linepos] == ':')
			{
			  info.linepos++;
			  while (info.linepos < info.linelen
				 && (info.line[info.linepos] == ' ' || info.line[info.linepos] == '\t'))
			    info.linepos++;
			  if (info.linepos < info.linelen && isIdentChar(info.line[info.linepos]))
			    {
			      val = &info.line[info.linepos];
			      valSize = 1;
			      info.linepos++;
			      while (info.linepos < info.linelen && isIdentChar(info.line[info.linepos]))
				{
				  valSize++;
				  info.linepos++;
				}
			    }
			  else
			    goto compile_error;
			}
		      if (info.linepos == info.linelen)
			{
			  char * k = widestrToStr(key, keySize);
			  char * v = val ? widestrToStr(val, valSize) : NULL;
			  Feature f = feature_new(k, v);
			  logMessage(LOG_DEBUG, "Table has feature '%s:%s'", f.key, f.val);
			  features = list_conj(features, memcpy(malloc(sizeof(f)), &f, sizeof(f)),
					       NULL, NULL, (void (*)(void *))feature_free);
			  free(k);
			  free(v);
			}
		      else
			goto compile_error;
		    }
		  else
		    goto compile_error;
		}
	    }
	  else
	    break;
	}
      fclose(info.in);
    }
  else
    logMessage (LOG_ERROR, "Cannot open table '%s'", info.fileName);
  return list_sort(features, (int (*)(void *, void *))cmpKeys);
 compile_error:
  if (info.linepos < info.linelen)
    logMessage(LOG_ERROR, "Unexpected character '%c' on line %d, column %d",
	       info.line[info.linepos],
	       info.lineNumber,
	       info.linepos);
  else
    logMessage(LOG_ERROR, "Unexpected newline on line %d", info.lineNumber);
  list_free(features);
  return NULL;
}

static List * tableIndex = NULL;

void EXPORT_CALL
lou_indexTables(const char ** tables)
{
  const char ** table;
  list_free(tableIndex);
  tableIndex = NULL;
  for (table = tables; *table; table++)
    {
      logMessage(LOG_DEBUG, "Analyzing table %s", *table);
      List * features = analyzeTable(*table);
      if (features)
	{
	  TableMeta m = { strdup(*table), features };
	  tableIndex = list_conj(tableIndex, memcpy(malloc(sizeof(m)), &m, sizeof(m)), NULL, NULL, free);
	}
    }
  if (!tableIndex)
    logMessage(LOG_WARN, "No tables were indexed");
}

#ifdef _WIN32
#define DIR_SEP '\\'
#else
#define DIR_SEP '/'
#endif

/**
 * Returns the list of files found on searchPath, where searchPath is a
 * comma-separated list of directories.
 */
static List *
listFiles(char * searchPath)
{
  List * list = NULL;
  char * dirName;
  DIR * dir;
  struct dirent * file;
  static char fileName[MAXSTRING];
  struct stat info;
  int pos = 0;
  int n;
  while (1)
    {
      for (n = 0; searchPath[pos + n] != '\0' && searchPath[pos + n] != ','; n++);
      dirName = strndup(&searchPath[pos], n);
      if ((dir = opendir(dirName)))
	{
	  while ((file = readdir(dir)))
	    {
	      sprintf(fileName, "%s%c%s", dirName, DIR_SEP, file->d_name);
	      if (stat(fileName, &info) == 0 && !(info.st_mode & S_IFDIR))
		{
		  list = list_conj(list, strdup(fileName), NULL, NULL, free);
		}
	    }
	  closedir(dir);
	}
      else
	{
	  logMessage(LOG_WARN, "%s is not a directory", dirName);
	}
      free(dirName);
      pos += n;
      if (searchPath[pos] == '\0')
	break;
      else
	pos++;
    }
  return list;
}

char * EXPORT_CALL
lou_findTable(const char * query)
{
  if (!tableIndex)
    {
      char * searchPath;
      List * tables;
      const char ** tablesArray;
      logMessage(LOG_WARN, "Tables have not been indexed yet. Indexing LOUIS_TABLEPATH.");
      searchPath = getTablePath();
      tables = listFiles(searchPath);
      tablesArray = (const char **)list_toArray(tables, NULL);
      lou_indexTables(tablesArray);
      free(searchPath);
      list_free(tables);
      free(tablesArray);
    }
  List * queryFeatures = parseQuery(query);
  int bestQuotient = 0;
  char * bestMatch = NULL;
  List * l;
  for (l = tableIndex; l; l = l->tail)
    {
      TableMeta * table = l->head;
      int q = matchFeatureLists(queryFeatures, table->features, 0);
      if (q > bestQuotient)
	{
	  bestQuotient = q;
	  bestMatch = strdup(table->name);
	}
    }
  if (bestMatch)
     {
       logMessage(LOG_INFO, "Best match: %s (%d)", bestMatch, bestQuotient);
       return bestMatch;
     }
  else
    {
      logMessage(LOG_INFO, "No table could be found for query '%s'", query);
      return NULL;
    }
}