File: ho_recognize_nikud.c

package info (click to toggle)
hocr 0.10.18-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,296 kB
  • sloc: ansic: 18,153; sh: 11,473; python: 1,422; makefile: 190; cpp: 19
file content (593 lines) | stat: -rw-r--r-- 14,159 bytes parent folder | download | duplicates (4)
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

/***************************************************************************
 *            ho_recognize_nikud.c
 *
 *  Fri Aug 12 20:13:33 2005
 *  Copyright  2005-2007  Yaacov Zamir
 *  <kzamir@walla.co.il>
 ****************************************************************************/

/*  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

#ifndef TRUE
#define TRUE -1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef NULL
#define NULL ((void*)0)
#endif

#include "ho_bitmap.h"
#include "ho_objmap.h"
#include "ho_segment.h"
#include "ho_font.h"

#include "ho_recognize_nikud.h"

int
ho_recognize_nikud_array_in_size ()
{
  return HO_NIKUD_ARRAY_IN_SIZE;
}

int
ho_recognize_nikud_array_out_size ()
{
  return HO_NIKUD_ARRAY_OUT_SIZE;
}

int
ho_recognize_nikud_dimentions (const ho_bitmap * m_text,
  const ho_bitmap * m_mask, double *height,
  double *width, double *top, double *bottom,
  double *top_left, double *top_mid, double *top_right,
  double *mid_left, double *mid_right,
  double *bottom_left, double *bottom_mid,
  double *bottom_right,
  double *dots_above, double *dots_below, double *dots_inside,
  double *objs_above, double *objs_below, double *objs_inside,
  double *main_obj_height, double *main_obj_width,
  double *top_dot_x, double *inside_dot_x, double *font_width_by_height)
{
  int i, x, y;
  int obj_width, obj_height;
  int obj_x, obj_y;
  int obj_w, obj_h;
  int line_start, line_end, line_height;
  int sum, font_start_x, font_end_x, font_height, font_width;
  int font_start_y, font_end_y;
  ho_objmap *o_obj = NULL;
  unsigned char is_dot, is_obj;

  /* init values to zero */
  *height = 0.0;
  *width = 0.0;
  *top = 0.0;
  *bottom = 0.0;
  *top_left = 0.0;
  *top_mid = 0.0;
  *top_right = 0.0;
  *mid_left = 0.0;
  *mid_right = 0.0;
  *bottom_left = 0.0;
  *bottom_mid = 0.0;
  *bottom_right = 0.0;
  *dots_above = 0.0;
  *dots_below = 0.0;
  *dots_inside = 0.0;
  *objs_above = 0.0;
  *objs_below = 0.0;
  *objs_inside = 0.0;
  *top_dot_x = 0.0;
  *inside_dot_x = 0.0;
  *font_width_by_height = 0.0;

  /* get line start and end */
  x = m_mask->width / 2;
  for (y = 0; y < m_mask->height && !ho_bitmap_get (m_mask, x, y); y++) ;
  line_start = y - 1;
  for (; y < m_mask->height && ho_bitmap_get (m_mask, x, y); y++) ;
  line_end = y;
  line_height = line_end - line_start;

  if (line_height < 4 || m_text->width < 4)
    return TRUE;

  *font_width_by_height = (double) m_mask->width / (double) line_height;

  /* get all the objects of nikud */
  o_obj = ho_objmap_new_from_bitmap (m_text);
  if (!o_obj)
    return TRUE;

  /* if no nikud just return */
  if (ho_objmap_get_size (o_obj) == 0)
  {
    ho_objmap_free (o_obj);

    return FALSE;
  }

  /* count points and objects above/in/below font */
  obj_x = 0;
  obj_y = 0;
  obj_w = 0;
  obj_h = 0;

  for (i = 0; i < ho_objmap_get_size (o_obj); i++)
  {
    /* is this object inside line ? */
    y = ho_objmap_get_object (o_obj, i).y;
    x = ho_objmap_get_object (o_obj, i).x;
    obj_width = ho_objmap_get_object (o_obj, i).width;
    obj_height = ho_objmap_get_object (o_obj, i).height;

    is_dot = (obj_height < line_height / 4 && obj_height > line_height / 16
      && obj_width < line_height / 4 && obj_width > line_height / 16);

    is_obj = (obj_height > line_height / 4 || obj_width > line_height / 4);

    if (is_dot)
    {
      /* count */
      if ((y + obj_height / 2) < line_start)
      {
        (*dots_above) += 0.1;
        *top_dot_x = (double) x / (double) (m_text->width);
      }
      else if ((y + obj_height / 2) > line_end)
      {
        (*dots_below) += 0.1;
      }
      else
      {
        (*dots_inside) += 0.1;
        *inside_dot_x = (double) x / (double) (m_text->width);
      }
    }
    else if (is_obj)
    {
      /* count */
      if ((y + obj_height / 2) < line_start)
      {
        (*objs_above) += 0.1;
      }
      else if ((y + obj_height / 2) > line_end)
      {
        (*objs_below) += 0.1;

        /* get metrics of bigest obj under font */
        if (obj_width > *width)
        {
          obj_x = x;
          obj_y = y;
          obj_w = obj_width;
          obj_h = obj_height;
        }
      }
      else
      {
        (*objs_inside) += 0.1;
      }
    }
  }

  /* free obj map */
  ho_objmap_free (o_obj);

  /* if nothing below font we are finished */
  if (!(*objs_below) && !(*dots_below))
    return FALSE;

  /* get nikud metrics */
  *main_obj_height = 2.0 * (double) obj_h / (double) line_height;
  *main_obj_width = 2.0 * (double) obj_w / (double) line_height;

  if (obj_y && obj_h)
  {
    *top = 2.0 * (double) (obj_y - line_end) / (double) line_height;
    *bottom =
      2.0 * (double) ((obj_y + obj_h) - line_end) / (double) line_height;
  }

  /* get nikud start and end */
  sum = 0;
  for (y = line_end + 1; y < m_text->height && sum == 0; y++)
    for (sum = 0, x = 1; x < (m_text->width - 1); x++)
      sum += ho_bitmap_get (m_text, x, y);
  font_start_y = y - 1;
  sum = 0;
  for (y = m_text->height - 1; y > font_start_y && sum == 0; y--)
    for (sum = 0, x = 1; x < (m_text->width - 1); x++)
      sum += ho_bitmap_get (m_text, x, y);
  font_end_y = y + 1;
  font_height = font_end_y - font_start_y;

  if (!font_height)
    return TRUE;

  *height = 2.0 * (double) font_height / (double) line_height;

  sum = 0;
  for (x = 2; x < m_text->width && sum == 0; x++)
    for (sum = 0, y = line_end; y < (m_text->height - 1); y++)
      sum += ho_bitmap_get (m_text, x, y);
  font_start_x = x - 1;
  sum = 0;

  for (sum = 0, x = m_text->width - 2; x > (font_start_x + 1) && sum == 0; x--)
    for (sum = 0, y = line_end; y < (m_text->height - 1); y++)
      sum += ho_bitmap_get (m_text, x, y);
  font_end_x = x + 1;
  font_width = font_end_x - font_start_x;

  if (!font_width)
    return TRUE;

  *width = 2.0 * (double) font_width / (double) line_height;

  /* get nikud egdes */
  for (y = font_start_y, x = font_start_x;
    x < font_end_x && y < (font_end_y)
    && !ho_bitmap_get (m_text, x, y); x++, y++) ;
  *top_left = (double) (x - font_start_x) / (double) (line_height / 4);

  if (*top_left > 1.0)
    *top_left = 1.0;

  for (y = font_end_y, x = font_start_x;
    x < font_end_x && y > (font_start_y)
    && !ho_bitmap_get (m_text, x, y); x++, y--) ;
  *bottom_left = (double) (x - font_start_x) / (double) (line_height / 4);

  if (*bottom_left > 1.0)
    *bottom_left = 1.0;

  for (y = font_start_y, x = font_end_x - 1;
    x > font_start_x && y < (font_end_y)
    && !ho_bitmap_get (m_text, x, y); x--, y++) ;
  *top_right = (double) (font_end_x - x) / (double) (line_height / 4);

  if (*top_right > 1.0)
    *top_right = 1.0;

  for (y = font_end_y, x = font_end_x - 1;
    x > font_start_x && y > font_start_y
    && !ho_bitmap_get (m_text, x, y); x--, y--) ;
  *bottom_right = (double) (font_end_x - x) / (double) (line_height / 4);

  if (*bottom_right > 1.0)
    *bottom_right = 1.0;

  for (y = font_start_y, x = font_start_x + font_width / 2;
    y < (font_start_y + font_height) && !ho_bitmap_get (m_text, x, y); y++) ;
  *top_mid = (double) (y - font_start_y) / (double) (line_height / 4);

  if (*top_mid > 1.0)
    *top_mid = 1.0;

  for (y = font_end_y, x = font_start_x + font_width / 2;
    y > (font_start_y + font_height) && !ho_bitmap_get (m_text, x, y); y--) ;
  *bottom_mid = (double) (font_end_y - y) / (double) (line_height / 4);

  if (*bottom_mid > 1.0)
    *bottom_mid = 1.0;

  for (y = font_start_y + font_height / 2, x = font_start_x;
    x < (font_end_x) && !ho_bitmap_get (m_text, x, y); x++) ;
  *mid_left = (double) (x - font_start_x) / (double) (line_height / 4);

  if (*mid_left > 1.0)
    *mid_left = 1.0;

  for (y = font_start_y + font_height / 2, x = font_end_x - 1;
    x > (font_start_x) && !ho_bitmap_get (m_text, x, y); x--) ;
  *mid_right = (double) (font_end_x - x) / (double) (line_height / 4);

  if (*mid_right > 1.0)
    *mid_right = 1.0;

  return FALSE;
}

int
ho_recognize_nikud_array_in (const ho_bitmap * m_nikud,
  const ho_bitmap * m_mask, double *array_in)
{

  double height;
  double width;
  double top;
  double bottom;
  double top_left;
  double top_mid;
  double top_right;
  double mid_left;
  double mid_right;
  double bottom_left;
  double bottom_mid;
  double bottom_right;
  double dots_above;
  double dots_below;
  double dots_inside;
  double objs_above;
  double objs_below;
  double objs_inside;
  double main_obj_height;
  double main_obj_width;
  double top_dot_x;
  double inside_dot_x;
  double font_width_by_height;

  ho_recognize_nikud_dimentions (m_nikud,
    m_mask, &height,
    &width, &top, &bottom,
    &top_left, &top_mid, &top_right,
    &mid_left, &mid_right,
    &bottom_left, &bottom_mid,
    &bottom_right,
    &dots_above, &dots_below, &dots_inside,
    &objs_above, &objs_below, &objs_inside, &main_obj_height, &main_obj_width,
    &top_dot_x, &inside_dot_x, &font_width_by_height);

  array_in[0] = height;
  array_in[1] = width;
  if (height)
    array_in[2] = width / height;
  else
    array_in[2] = 0.0;
  if (array_in[2] > 1.0)
    array_in[2] = 1.0;
  array_in[3] = top;
  array_in[4] = bottom;
  array_in[5] = top_left;
  array_in[6] = top_mid;
  array_in[7] = top_right;
  array_in[8] = mid_left;
  array_in[9] = mid_right;
  array_in[10] = bottom_left;
  array_in[11] = bottom_mid;
  array_in[12] = bottom_right;

  array_in[13] = dots_above;
  array_in[14] = dots_inside;
  array_in[15] = dots_below;
  array_in[16] = objs_above;
  array_in[17] = objs_inside;
  array_in[18] = objs_below;

  array_in[19] = main_obj_width;
  array_in[20] = main_obj_height;

  array_in[21] = top_dot_x;
  array_in[22] = inside_dot_x;
  array_in[23] = font_width_by_height;

  return 0;
}

int
ho_recognize_nikud_array_out (const double *array_in, double *array_out,
  int font_code)
{
  int i;

  /* set array out */
  array_out[0] = 0.1;

  for (i = 1; i < HO_NIKUD_ARRAY_OUT_SIZE; i++)
    array_out[i] = 0.0;

  /* one dot above line */
  if (array_in[13] == 0.1)
  {
    if (array_in[23] < 0.53)
    {
      /* holam */
      array_out[5] = 1.0;
    }
    else
    {
      if (array_in[21] > 0.5)
      {
        /* shin */
        array_out[14] = -1.0;
      }
      else
      {
        /* sin */
        array_out[15] = -1.0;
      }
    }
  }

  /* dot inside font */
  if (array_in[14] == 0.1)
  {
    if (array_in[23] < 0.53)
    {
      /* shorok */
      array_out[6] = 1.0;
    }
    else
    {
      /* dagesh */
      array_out[13] = -1.0;
    }
  }

  /* two dots inside */
  if (array_in[14] > 0.1 && array_in[14] < 0.3)
  {
    /* shva */
    array_out[9] = 1.0;
  }

  /* one obj below line */
  if (array_in[18] == 0.1 && array_in[15] == 0.0)
  {

    if (array_in[1] > (1.8 * array_in[0]))
    {
      if (array_in[6] > 0.25)
      {
        /* tzere */
        array_out[3] = 1.0;
      }
      else
      {
        /* patach */
        array_out[2] = 1.0;
      }
    }
    else if (array_in[0] > (1.8 * array_in[1]))
    {
      /* shva */
      array_out[9] = 1.0;
    }
    else
    {
      if (array_in[12] > 0.1)
      {
        if (array_in[6] < 0.2)
        {
          /* kamatz */
          array_out[1] = 1.0;
        }
        else
        {
          /* segul */
          array_out[4] = 1.0;
        }
      }
      else
      {
        /* kobotz */
        array_out[7] = 1.0; 
      }
    }
  }

  /* one dot below line */
  if (array_in[15] > 0.0 && array_in[15] < 0.2 && array_in[18] == 0.0)
  {
    /* hirik */
    array_out[8] = 1.0;
  }

  /* two dots */
  if (array_in[15] > 0.1 && array_in[15] < 0.3)
  {
    if (array_in[18] != 0.1)
    {
      if (array_in[1] > (1.8 * array_in[0]) && array_in[1] > 0.65)
      {
        /* tzere */
        array_out[3] = 1.0;
      }
      else
      {
        /* shva */
        array_out[9] = 1.0;
      }
    }
    else                        /* hataf */
    {
      if (array_in[19] > (2.0 * array_in[20]))
      {
        /* hataf patach */
        array_out[11] = 1.0;
      }
      else
      {
        /* hataf kamatz */
        array_out[12] = 1.0;
      }
    }
  }

  /* three dots */
  if (array_in[15] > 0.2 && array_in[15] < 0.4)
  {
    if (array_in[11] > 0.1 || array_in[7] > 0.5)
    {
      /* kobotz */
      array_out[7] = 1.0;
    }
    else
    {
      /* segul */
      array_out[4] = 1.0;
    }
  }

  /* five dots */
  if (array_in[15] > 0.4 && array_in[15] < 0.6)
  {
    /* hataf segul */
    array_out[10] = 1.0;
  }

  return FALSE;
}

const char *
ho_recognize_array_out_to_nikud (const double *array_out)
{
  int i = 0;
  int max_i = 0;

  /* find the font with hiegher score */
  for (i = 1; i < HO_NIKUD_ARRAY_OUT_SIZE; i++)
    if (array_out[i] > array_out[max_i])
      max_i = i;
  return ho_nikud_array[max_i];
}

const char *
ho_recognize_nikud (const ho_bitmap * m_nikud, const ho_bitmap * m_mask,
  int font_code, const char **dagesh, const char **shin)
{
  double array_in[HO_NIKUD_ARRAY_IN_SIZE];
  double array_out[HO_NIKUD_ARRAY_OUT_SIZE];
  const char *font;

  *dagesh = ho_nikud_array[0];
  *shin = ho_nikud_array[0];

  ho_recognize_nikud_array_in (m_nikud, m_mask, array_in);
  ho_recognize_nikud_array_out (array_in, array_out, font_code);
  font = ho_recognize_array_out_to_nikud (array_out);

  /* dagesh */
  if (array_out[13] == -1.0)
    *dagesh = ho_nikud_array[13];

  /* shin */
  if (array_out[14] == -1.0)
    *shin = ho_nikud_array[14];
  if (array_out[15] == -1.0)
    *shin = ho_nikud_array[15];

  return font;
}