X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=pctb%2Focr.c;h=04aecc0d48a0431642d73c85f8b65144fa74b491;hb=5a2c03e2e4f52b8329f45cf67afc3edec1f2c65b;hp=fd4fcf4f8de99642291fb3e9c5410a2884c642fb;hpb=8787ee59f6840de63bac432b516a30d0dfe22c84;p=ypp-sc-tools.web-live.git diff --git a/pctb/ocr.c b/pctb/ocr.c index fd4fcf4..04aecc0 100644 --- a/pctb/ocr.c +++ b/pctb/ocr.c @@ -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; ctxiresults; inresults; 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; xnextword; } 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");