static const char *context_names[]= {
"Lower",
"Upper",
-/* "Digit"*/
+ "Digit"
};
#define NCONTEXTS (sizeof(context_names)/sizeof(context_names[0]))
+#define SPACE_SPACES 3
+
struct OcrReader {
int h;
DatabaseNode contexts[NCONTEXTS];
}
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) {
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);
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);
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();
if (!cols[x]) {
nspaces++;
x++;
- if (nspaces==3) {
+ if (nspaces==SPACE_SPACES) {
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;
}
+
+ /* something here, so we need to add the spaces */
+ if (nspaces>=SPACE_SPACES)
+ add_result(rd," ",x-nspaces,x+1,0);
nspaces=0;
/* find character */
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);
}
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");