chiark / gitweb /
made its first tsv!
[ypp-sc-tools.web-live.git] / pctb / ocr.c
index 436aa8dc16078ae830b54de0e1e7468c71bcb4fd..712f90df77c21e239b8f47c256564aae512acaf4 100644 (file)
@@ -8,11 +8,12 @@ typedef struct {
   struct DatabaseNode *then;
 } DatabaseLink;
 
   struct DatabaseNode *then;
 } DatabaseLink;
 
-#define MAXGLYPHCHRS 3
+#define MAXGLYPHCHRS 7
 
 typedef struct DatabaseNode {
   char s[MAXGLYPHCHRS+1]; /* null-terminated; "" means no match here */
   int nlinks, alinks;
 
 typedef struct DatabaseNode {
   char s[MAXGLYPHCHRS+1]; /* null-terminated; "" means no match here */
   int nlinks, alinks;
+  unsigned endsword:1;
   DatabaseLink *links;
 } DatabaseNode;
 
   DatabaseLink *links;
 } DatabaseNode;
 
@@ -37,6 +38,8 @@ static FILE *resolver;
 static pid_t resolver_pid;
 static int resolver_done;
 
 static pid_t resolver_pid;
 static int resolver_done;
 
+DEBUG_DEFINE_DEBUGF(ocr)
+
 static void fgetsline(FILE *f, char *lbuf, size_t lbufsz) {
   char *s= fgets(lbuf,lbufsz,f);
   eassert(s);
 static void fgetsline(FILE *f, char *lbuf, size_t lbufsz) {
   char *s= fgets(lbuf,lbufsz,f);
   eassert(s);
@@ -59,7 +62,7 @@ static void readdb(OcrReader *rd) {
   char chrs[MAXGLYPHCHRS+1];
   Pixcol cv;
   int r,j,ctxi;
   char chrs[MAXGLYPHCHRS+1];
   Pixcol cv;
   int r,j,ctxi;
-  int h;
+  int h, endsword;
   char lbuf[100];
   FILE *db;
 
   char lbuf[100];
   FILE *db;
 
@@ -110,6 +113,11 @@ static void readdb(OcrReader *rd) {
       }
       chrs[nchrs++]= c;
     }
       }
       chrs[nchrs++]= c;
     }
+    endsword= 0;
+    if (nchrs>1 && chrs[nchrs-1]==' ') {
+      endsword= 1;
+      nchrs--;
+    }
     chrs[nchrs]= 0;
 
     current= &rd->contexts[ctxi];
     chrs[nchrs]= 0;
 
     current= &rd->contexts[ctxi];
@@ -147,6 +155,7 @@ static void readdb(OcrReader *rd) {
 
     eassert(!current->s[0]);
     strcpy(current->s, chrs);
 
     eassert(!current->s[0]);
     strcpy(current->s, chrs);
+    current->endsword= endsword;
   }
   eassert(!ferror(db));
   eassert(!fclose(db));
   }
   eassert(!ferror(db));
   eassert(!fclose(db));
@@ -288,14 +297,6 @@ const struct OcrCellTypeInfo ocr_celltype_text= {
   .midword=1 /* Lower only */
 };
 
   .midword=1 /* Lower only */
 };
 
-static void vdebugf(const char *fmt, va_list al) {
-  if (DEBUGP(ocr))
-    vfprintf(debug,fmt,al);
-}
-static void debugf(const char *fmt, ...) {
-  va_list al;  va_start(al,fmt);  vdebugf(fmt,al);  va_end(al);
-}
-
 OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
   int nspaces;
   unsigned ctxmap;
 OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
   int nspaces;
   unsigned ctxmap;
@@ -366,7 +367,7 @@ OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
       found:
        current= current->links[i].then;
        if (current->s[0]) {
       found:
        current= current->links[i].then;
        if (current->s[0]) {
-         debugf(" \"%s\"",current->s);
+         debugf(" \"%s\"%s",current->s,current->endsword?"_":"");
          bestmatch= current;
          bestmatch_rx= x;
        } else {
          bestmatch= current;
          bestmatch_rx= x;
        } else {
@@ -388,10 +389,17 @@ OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
     }
 
     if (uniquematch) {
     }
 
     if (uniquematch) {
-      debugf(" || YES\n");
+      debugf(" || YES");
       add_result(rd, uniquematch->s, lx, uniquematch_rx, ctxmap);
       x= uniquematch_rx+1;
       add_result(rd, uniquematch->s, lx, uniquematch_rx, ctxmap);
       x= uniquematch_rx+1;
-      ctxmap= ct->midword;
+      if (uniquematch->s[0]) ctxmap= ct->midword;
+      else debugf(" (empty)");
+      if (uniquematch->endsword) {
+       nspaces= SPACE_SPACES;
+       debugf("_");
+       ctxmap= ct->nextword;
+      }
+      debugf("\n");
     } else {
       int rx;
       debugf(" || UNKNOWN");
     } else {
       int rx;
       debugf(" || UNKNOWN");