1 people like it.

Bank OCR Parser

bank OCR code parse from London's ProgF# 2013

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
81: 
82: 
#time

// from codingdojo.org/cgi-bin/wiki.pl?KataBankOCR

// fax gives client number in the form
// 3 x 27 characters either _ or |

let numbers = @"
 _     _  _       _   _  _   _   _ 
| | |  _| _| |_| |_  |_   | |_| |_|
|_| | |_  _|   |  _| |_|  | |_|  _|
"

let numArray2D =
    numbers.Split('\n').[1..3]
    |> Array.map (fun str -> str.ToCharArray())
    |> array2D

let numArray =
    [|
        numArray2D.[*,0..2]
        numArray2D.[*,4..4]
        numArray2D.[*,6..8]
        numArray2D.[*,10..11]
        numArray2D.[*,13..15]
        numArray2D.[*,17..19]
        numArray2D.[*,21..23]
        numArray2D.[*,25..26]
        numArray2D.[*,28..30]
        numArray2D.[*,32..34]
    |]

let numMap =
    numArray
    |> Array.mapi ( fun i num -> num , i )
    |> dict

numMap.TryGetValue( numArray.[3] )

let blank =
    [[' ']
     [' ']
     [' ']] |> array2D


let chars2nums (chars:string) =

    let chars2D =
        chars.Split('\n').[1..3]
        |> Array.map (fun str -> str.ToCharArray())
        |> array2D
    
    let width = 
        chars2D |> Array2D.length2
    
    [ 0 .. width-1 ]
        |> List.fold (fun (cursor : int , result : int list) newCursor ->
            //printfn "at cursor %i and newCursor %i" cursor newCursor
            let block = chars2D.[*,cursor .. newCursor]
            let spaceAfterBlock = if newCursor = width-1 then blank else chars2D.[*, newCursor+1 .. newCursor+1 ]
            match (block = blank) with
            | true -> ( newCursor+1 , result )
            | _ ->
                match ( numMap.TryGetValue(block) , spaceAfterBlock = blank ) with
                | (true,x) , true ->
                    //printfn "   I found a number!!! it's : %i" x
                    (newCursor+1 , x :: result )
                | _ -> ( cursor , result )
            )
            ( 0 , [] )
        |> snd
        |> List.rev



let chars = @"
   _  _       _   _  _   _   _ 
|  _| _| |_| |_  |_   | |_| |_|
| |_  _|   |  _| |_|  | |_|  _|
" // first line is just empty, from line 1 to 3 is where the numbers are

chars |> chars2nums
val numbers : string

Full name: Script.numbers
val numArray2D : char [,]

Full name: Script.numArray2D
System.String.Split([<System.ParamArray>] separator: char []) : string []
System.String.Split(separator: string [], options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], count: int) : string []
System.String.Split(separator: string [], count: int, options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], count: int, options: System.StringSplitOptions) : string []
module Array

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val str : string
System.String.ToCharArray() : char []
System.String.ToCharArray(startIndex: int, length: int) : char []
val array2D : rows:seq<#seq<'T>> -> 'T [,]

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.array2D
val numArray : char [,] []

Full name: Script.numArray
val numMap : System.Collections.Generic.IDictionary<char [,],int>

Full name: Script.numMap
val mapi : mapping:(int -> 'T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.mapi
val i : int
val num : char [,]
val dict : keyValuePairs:seq<'Key * 'Value> -> System.Collections.Generic.IDictionary<'Key,'Value> (requires equality)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.dict
System.Collections.Generic.IDictionary.TryGetValue(key: char [,], value: byref<int>) : bool
val blank : char [,]

Full name: Script.blank
val chars2nums : chars:string -> int list

Full name: Script.chars2nums
val chars : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val chars2D : char [,]
val width : int
module Array2D

from Microsoft.FSharp.Collections
val length2 : array:'T [,] -> int

Full name: Microsoft.FSharp.Collections.Array2D.length2
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val fold : folder:('State -> 'T -> 'State) -> state:'State -> list:'T list -> 'State

Full name: Microsoft.FSharp.Collections.List.fold
val cursor : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val result : int list
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val newCursor : int
val block : char [,]
val spaceAfterBlock : char [,]
val x : int
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
val rev : list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.rev
val chars : string

Full name: Script.chars

More information

Link:http://fssnip.net/kK
Posted:10 years ago
Author:Daniel Kovi
Tags: progf# , ocr