!752 static Map chars; sbool debug; !include #1000522 // helper functions !include #1004653 // Lowest static S recognizeWord(BWImage iw, O infos) { chars = (Map) quickImport(get(infos, "chars")); debug = isTrue(getOpt(infos, "debug")); int w = iw.getWidth(); new StringBuilder buf; xloop: for (int x = 0; x < w; x++) { if (debug) print("Best match at " + x + ": " + structure(bestMatch(iw, x))); for (BWImage ic : keys(chars)) { if (preciseMatch(ic, iw, x)) { buf.append(chars.get(ic)); x += ic.getWidth()-1; continue xloop; } } } ret str(buf); } static Pair bestMatch(BWImage iw, int x) { new Lowest best; for (BWImage ic : keys(chars)) { double d = diff(ic, iw.clip(x, 0, ic.getWidth(), ic.getHeight())); best.update(chars.get(ic), d); } ret best.getPair(); } static bool preciseMatch(BWImage ic, BWImage iw, int x1) { int w = iw.getWidth(), h = iw.getHeight(); if (ic.getHeight() != h) false; int x2 = x1+ic.getWidth(); if (x2 > w) false; for (int x = x1; x < x2; x++) for (int y = 0; y < h; y++) if (ic.getPixel(x-x1, y) != iw.getPixel(x, y)) false; true; }