package ocr
import (
"strings"
)
// ocrSolver is a 3 line chunk of digits
var ocrSolver = map[string]string{
" _ | ||_|": "0",
" | |": "1",
" _ _||_ ": "2",
" _ _| _|": "3",
" |_| |": "4",
" _ |_ _|": "5",
" _ |_ |_|": "6",
" _ | |": "7",
" _ |_||_|": "8",
" _ |_| _|": "9",
}
// Recognize recognizes ocr strings in input and converts to regular ones.
func Recognize(in string) []string {
lines := strings.Split(in, "\n")
lines = lines[1:] // drop first line, just a '\n'
// separate input lines to 4 line long chunks to handle multiple ocr lines
digitLines := make([][]string, (len(lines))/4)
for i := 0; i < len(digitLines); i++ {
digitLines[i] = make([]string, 4)
for j := 0; j < 4; j++ {
digitLines[i][j] = lines[4*i+j]
}
}
var ret = make([]string, len(digitLines))
for dl, dline := range digitLines {
var ln = strings.Builder{}
for i := 0; i < len(dline[0])/3; i++ {
var digit strings.Builder
// gather a digit from 3 lines pieces
for lNo, line := range dline {
if lNo == len(dline)-1 {
// last line is useless
continue
}
digit.WriteString(line[i*3 : i*3+3])
}
ln.WriteString(recognizeDigit(digit.String()))
}
ret[dl] = ln.String()
}
return ret
}
func recognizeDigit(d string) string {
ret, ok := ocrSolver[d]
if !ok {
return "?"
}
return ret
}