Sha256: 826748d12d3013e81efc44327d8ba15dd806c815304283708029a4a5dddb4e45

Contents?: true

Size: 1.94 KB

Versions: 121

Compression:

Stored size: 1.94 KB

Contents

interface Patterns {
    [key: number]: string[]
}

const PATTERNS: Patterns = {
    0: [' _ ',
        '| |',
        '|_|',
        '   '],
    1: ['   ',
        '  |',
        '  |',
        '   '],
    2: [' _ ',
        ' _|',
        '|_ ',
        '   '],
    3: [' _ ',
        ' _|',
        ' _|',
        '   '],
    4: ['   ',
        '|_|',
        '  |',
        '   '],
    5: [' _ ',
        '|_ ',
        ' _|',
        '   '],
    6: [' _ ',
        '|_ ',
        '|_|',
        '   '],
    7: [' _ ',
        '  |',
        '  |',
        '   '],
    8: [' _ ',
        '|_|',
        '|_|',
        '   '],
    9: [' _ ',
        '|_|',
        ' _|',
        '   '],
}

export default class OcrParser {
    static convert(text: string) {
        return OcrParser.splitIntoRows(text).map(OcrParser.valuesInRow).join(',')
    }

    static valuesInRow(row: string) {
        return OcrParser.splitIntoDigits(row).map(OcrParser.getDigit).join('')
    }

    static splitIntoRows(text: string) {
        const rows = []
        const lines = text.split('\n')
        for (let rowNumber = 0; rowNumber < lines.length; rowNumber += 4) {
            let row = ''
            for (let rowLine = 0; rowLine < 4; rowLine++) {
                row += `${lines[rowNumber + rowLine]}\n`
            }
            rows.push(row.slice(0, -1))
        }
        return rows
    }

    static splitIntoDigits(row: string) {
        const digits = [],
            rows = row.split('\n')
        for (let digitNumber = 0; digitNumber < rows[0].length; digitNumber += 3) {
            let digit = ''
            for (const row of rows) {
                digit += row.substr(digitNumber, 3)
            }
            digits.push(digit)
        }
        return digits
    }

    static getDigit(text: string) {
        for (const digit in PATTERNS) {
            if (PATTERNS[digit].join('') === text) {
                return digit
            }
        }
        return '?'
    }

}

Version data entries

121 entries across 121 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.138 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.137 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.136 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.135 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.134 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.133 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.132 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.131 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.130 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.129 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.128 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.127 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.126 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.125 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.124 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.123 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.122 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.121 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts
trackler-2.2.1.120 tracks/typescript/exercises/ocr-numbers/ocr-numbers.example.ts