chiark / gitweb /
Seems to be able to do the whole thing
[ypp-sc-tools.web-live.git] / pctb / ocr.c
index fd4fcf4f8de99642291fb3e9c5410a2884c642fb..04aecc0d48a0431642d73c85f8b65144fa74b491 100644 (file)
@@ -19,7 +19,7 @@ typedef struct DatabaseNode {
 static const char *context_names[]= {
   "Lower",
   "Upper",
-/*  "Digit"*/
+  "Digit"
 };
 
 #define NCONTEXTS (sizeof(context_names)/sizeof(context_names[0]))
@@ -144,7 +144,19 @@ static void readdb(OcrReader *rd) {
   }
   eassert(!ferror(db));
   eassert(!fclose(db));
-}      
+}
+
+static void cu_pr_ctxmap(unsigned ctxmap) {
+  fprintf(resolver,"{");
+  const char *spc="";
+  int ctxi;
+  for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
+    if (!(ctxmap & (1u << ctxi))) continue;
+    fprintf(resolver,"%s%s",spc,context_names[ctxi]);
+    spc=" ";
+  }
+  fprintf(resolver,"}");
+}
 
 static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
                            int unk_l, int unk_r, unsigned unk_ctxmap) {
@@ -175,18 +187,13 @@ static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
     resolver= fdopen(jobpipe[1],"w"); eassert(resolver);
     resolver_done= donepipe[0];
   }
-  fprintf(resolver,"%d %d {",unk_l,unk_r);
-  const char *spc="";
-  int ctxi;
-  for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
-    if (!(unk_ctxmap & (1u << ctxi))) continue;
-    fprintf(resolver,"%s%s",spc,context_names[ctxi]);
-    spc=" ";
-  }
-  fprintf(resolver,"}");
+  fprintf(resolver,"%d %d ",unk_l,unk_r);
+  cu_pr_ctxmap(unk_ctxmap);
   for (i=0, s=rd->results; i<rd->nresults; i++, s++) {
     if (!strcmp(s->s," ")) continue;
-    fprintf(resolver," %d %d %s ",s->l,s->r,context_names[s->ctx]);
+    fprintf(resolver," %d %d ",s->l,s->r);
+    cu_pr_ctxmap(s->ctxmap);
+    fprintf(resolver," ");
     for (p=s->s; (c= *p); p++) {
       if (c=='\\') fprintf(resolver,"\\%c",c);
       else if (c>=33 && c<=126) fputc(c,resolver);
@@ -247,7 +254,8 @@ static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
   readdb(rd);
 }
 
-static void add_result(OcrReader *rd, const char *s, int l, int r, int ctx) {
+static void add_result(OcrReader *rd, const char *s, int l, int r,
+                      unsigned ctxmap) {
   if (rd->nresults >= rd->aresults) {
     rd->aresults++; rd->aresults<<=1;
     rd->results= realloc(rd->results,sizeof(*rd->results)*rd->aresults);
@@ -256,23 +264,37 @@ static void add_result(OcrReader *rd, const char *s, int l, int r, int ctx) {
   rd->results[rd->nresults].s= s;
   rd->results[rd->nresults].l= l;
   rd->results[rd->nresults].r= r;
-  rd->results[rd->nresults].ctx= ctx;
+  rd->results[rd->nresults].ctxmap= ctxmap;
   rd->nresults++;
 }
 
-OcrResultGlyph *ocr(OcrReader *rd, int w, Pixcol cols[]) {
-  int nspaces=-w;
-  unsigned ctxmap=2; /* uppercase */
+struct OcrCellTypeInfo {
+  unsigned initial, nextword, midword;
+};
+const struct OcrCellTypeInfo ocr_celltype_number= {
+  4,4,4
+};
+const struct OcrCellTypeInfo ocr_celltype_text= {
+  .initial=2 /* Uppercase */,
+  .nextword=3 /* Either */,
+  .midword=1 /* Lower only */
+};
+
+OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
+  int nspaces;
+  unsigned ctxmap;
   int ctxi, i, x;
 
-  rd->nresults=0;
+ restart:
 
+  nspaces=- w;
+  ctxmap= ct->initial;
+  rd->nresults=0;
   fprintf(debug,"OCR h=%d w=%d",rd->h,w);
   for (x=0; x<w; x++) fprintf(debug," %"PSPIXCOL(PRIx),cols[x]);
   fprintf(debug,"\n");
   debug_flush();
 
- restart:
   x=0;
   for (;;) {
     debug_flush();
@@ -286,7 +308,7 @@ OcrResultGlyph *ocr(OcrReader *rd, int w, Pixcol cols[]) {
       if (nspaces==3) {
        fprintf(debug,"OCR  x=%x nspaces=%d space\n",x,nspaces);
        add_result(rd," ",x-nspaces,x+1,0);
-       ctxmap=3; /* either */
+       ctxmap= ct->nextword;
       }
       continue;
     }
@@ -296,7 +318,7 @@ OcrResultGlyph *ocr(OcrReader *rd, int w, Pixcol cols[]) {
     int lx=x;
 
     DatabaseNode *uniquematch= 0;
-    int uniquematch_rx=-1, uniquematch_ctxi=-1;
+    int uniquematch_rx=-1;
     
     fprintf(debug,"OCR  lx=%d ctxmap=%x  ",lx,ctxmap);
 
@@ -336,22 +358,21 @@ OcrResultGlyph *ocr(OcrReader *rd, int w, Pixcol cols[]) {
       }
       
       if (bestmatch) {
-       if (uniquematch) {
+       if (uniquematch && strcmp(bestmatch->s, uniquematch->s)) {
          fprintf(debug, " ambiguous");
          uniquematch= 0;
          break;
        }
        uniquematch= bestmatch;
        uniquematch_rx= bestmatch_rx;
-       uniquematch_ctxi= ctxi;
       }
     }
 
     if (uniquematch) {
       fprintf(debug," || YES\n");
-      add_result(rd, uniquematch->s, lx, uniquematch_rx, uniquematch_ctxi);
+      add_result(rd, uniquematch->s, lx, uniquematch_rx, ctxmap);
       x= uniquematch_rx+1;
-      ctxmap= 1; /* Lower only */
+      ctxmap= ct->midword;
     } else {
       int rx;
       fprintf(debug," || UNKNOWN");