Commits

Christoph Schindler committed 1729814

Bank OCR successfully implemented!

-- code monkey and friends (foes?!)

  • Participants
  • Parent commits db2fea7

Comments (0)

Files changed (4)

BankOCR/accounts.txt

+ _     _  _     _  _  _  _ 
+| |  | _| _||_||_ |_   ||_|
+|_|  ||_  _|  | _||_|  ||_|
+
+    _  _     _  _  _  _  _ 
+  | _| _||_||_   ||_| _| _|
+  | _||_   ||_|  ||_||_  _|
+
+       _  _  _  _  _  _  _ 
+  ||_||_ |_ |_ |_ |_   ||_|
+  |  | _| _| _| _||_|  ||_|
+

BankOCR/bank2ocr.py

+
+lcd_digits = {
+    ' _ | ||_|': 0,
+    '     |  |': 1,
+    ' _  _||_ ': 2,
+    ' _  _| _|': 3,
+    '   |_|  |': 4,
+    ' _ |_  _|': 5,
+    ' _ |_ |_|': 6,
+    ' _   |  |': 7,
+    ' _ |_||_|': 8,
+    ' _ |_| _|': 9,
+}
+
+def digit_to_number(digit_str):
+    """Takes a string, returns a number
+    """
+    return lcd_digits[digit_str]
+
+def account_to_digits(account_block):
+    """Takes an account block, returns a list of digit strings
+    """
+    for i in range(0, 27, 3): # von, bis, step
+        #yield ''.join(account_block[x][i:i+3] for x in range(3))
+        yield (account_block[0][i:i+3] +
+               account_block[1][i:i+3] +
+               account_block[2][i:i+3])
+
+def input_to_account_blocks(lines):
+    """Takes an iterable of lines and
+       yields blocks between empty lines
+    """
+    itr = iter(lines)
+    while True:
+        yield [next(itr), next(itr), next(itr)]
+        next(itr)
+
+def account_block_to_account_number(block):
+    return ''.join(str(digit_to_number(digitstring))
+                for digitstring in account_to_digits(block))
+
+def input_to_account_numbers(iterable):
+    for account_block in input_to_account_blocks(iterable):
+        yield account_block_to_account_number(account_block)
+
+if __name__ == '__main__':
+    import fileinput
+    for account_number in input_to_account_numbers(fileinput.input()):
+        print account_number
+

BankOCR/template.txt

+ _     _  _     _  _  _  _  _
+| |  | _| _||_||_ |_   ||_||_|
+|_|  ||_  _|  | _||_|  ||_| _| 
+

BankOCR/test_bank2ocr.py

+
+# wir definieren / gehen davon aus:
+#  - eine kontonummer schaut so aus wie in "input.txt"
+#  - es ist immer eine ziffer vorhanden
+#  - die vierte zeile wird beim input file lesen entfernt
+
+# erster schritt -> input.txt parsen
+
+# Generator: yield + tupel returnen: (funktion, arg1, arg2, ...)
+# http://readthedocs.org/docs/nose/en/latest/writing_tests.html
+
+import bank2ocr
+from nose.tools import assert_equal
+import unittest
+
+test_digits = [
+    ' _ | ||_|', # = 0
+    '     |  |', # = 1
+    ' _  _||_ ', # = 2
+    ' _  _| _|', # = 3
+    '   |_|  |', # = 4
+    ' _ |_  _|', # = 5
+    ' _ |_ |_|', # = 6
+    ' _   |  |', # = 7
+    ' _ |_||_|', # = 8
+    ' _ |_| _|', # = 9
+]
+
+block_with_account = [
+    ' _     _  _     _  _  _  _ ',
+    '| |  | _| _||_||_ |_   ||_|',
+    '|_|  ||_  _|  | _||_|  ||_|',
+]
+
+input_txt = """ _     _  _     _  _  _  _ 
+| |  | _| _||_||_ |_   ||_|
+|_|  ||_  _|  | _||_|  ||_|
+
+ _     _  _     _  _  _  _ 
+| |  | _| _||_||_ |_   ||_|
+|_|  ||_  _|  | _||_|  ||_|
+
+""".split('\n')
+
+block_with_account_as_digits = [
+    ' _ | ||_|', # = 0
+    '     |  |', # = 1
+    ' _  _||_ ', # = 2
+    ' _  _| _|', # = 3
+    '   |_|  |', # = 4
+    ' _ |_  _|', # = 5
+    ' _ |_ |_|', # = 6
+    ' _   |  |', # = 7
+    ' _ |_||_|', # = 8
+]
+
+# was wir testen wollen: bekommen wir eine liste len(...) = 9?
+# von einer methode ddh: account_to_digits
+
+def test_account_to_digits_on_block_with_account_has_listlen_9():
+    assert_equal(len(list(bank2ocr.account_to_digits(
+                        block_with_account))), 9)
+
+def test_account_to_digits_on_block_with_account_has_9charstrs():
+    assert all(len(s) == 9 for s in bank2ocr.account_to_digits(
+                block_with_account))
+
+def test_account_to_digits_returns_valid_digits():
+    assert_equal(list(bank2ocr.account_to_digits(
+                    block_with_account)),
+                 block_with_account_as_digits)
+
+def test_input_to_account_blocks_returns_lists_of_length_3():
+    assert all(len(l) == 3 for l in
+                bank2ocr.input_to_account_blocks(input_txt))
+
+# keine von den zeilen, die wir zurueck geliefert kriegen eine
+# leerzeile ist - wenn der input wirklich mit einer textzeile
+# beginnt (davon gehen wir aus)
+def test_input_to_account_blocks_returns_actual_account_blocks():
+    assert all(line.strip() != '' for lineblock in
+                bank2ocr.input_to_account_blocks(input_txt)
+                for line in lineblock)
+
+def test_account_block_to_account_number():
+    assert_equal(bank2ocr.account_block_to_account_number(
+                    block_with_account), '012345678')
+
+def test_input_txt_to_account_numbers():
+    assert_equal(list(bank2ocr.input_to_account_numbers(input_txt)),
+                    ['012345678', '012345678'])
+
+def test_zero_to_number():
+    s = ''.join([' _ ', '| |', '|_|'])
+    assert_equal(bank2ocr.digit_to_number(s), 0)
+
+def test_one_to_number():
+    s = '     |  |'  
+    assert_equal(bank2ocr.digit_to_number(s), 1)
+
+def test_all_digits():
+    def check(digit_str, number):
+        assert_equal(bank2ocr.digit_to_number(digit_str),
+                        number)
+
+    for i, s in enumerate(test_digits):
+        yield check, s, i
+
+