2 people like it.

KataBankOCR

See: http://codingdojo.org/cgi-bin/wiki.pl?KataBankOCR Prog. F# 2013 - London

 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: 
open System

let numbers = "
 _     _ __     _  _  _  _  _ 
| |  | _|__||_||_ |_   ||_||_|
|_|  ||_ __|  | _||_|  ||_| _|
"

let buildGroups(numbers : string) =
    let xss =
        numbers.Split([|'\n';'\r'|], StringSplitOptions.RemoveEmptyEntries)
        |> Array.map (fun cs -> cs.ToCharArray())
    [|
        for i = 0 to (xss.[0].Length - 1) / 3 do
            yield [|
                for j = 0 to 2 do
                    for k = 0 to 2 do  
                        yield xss.[j].[k + i * 3]
            |]
    |]
    
let lookup =
    buildGroups numbers |> Array.mapi (fun i xs ->
        xs, i) |> Map.ofArray

let classify =
    buildGroups >> Array.map (fun xs -> lookup.[xs])

let testCase = """
 _     _ __ 
| |  | _|__|
|_|  ||_ __|
"""

classify testCase
namespace System
val numbers : string

Full name: Script.numbers
val buildGroups : numbers:string -> char [] []

Full name: Script.buildGroups
val numbers : string
Multiple items
val string : value:'T -> string

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

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val xss : char [] []
String.Split([<ParamArray>] separator: char []) : string []
String.Split(separator: string [], options: StringSplitOptions) : string []
String.Split(separator: char [], options: StringSplitOptions) : string []
String.Split(separator: char [], count: int) : string []
String.Split(separator: string [], count: int, options: StringSplitOptions) : string []
String.Split(separator: char [], count: int, options: StringSplitOptions) : string []
type StringSplitOptions =
  | None = 0
  | RemoveEmptyEntries = 1

Full name: System.StringSplitOptions
field StringSplitOptions.RemoveEmptyEntries = 1
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val cs : string
String.ToCharArray() : char []
String.ToCharArray(startIndex: int, length: int) : char []
val i : int
val j : int
val k : int
val lookup : Map<char [],int>

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

Full name: Microsoft.FSharp.Collections.Array.mapi
val xs : char []
Multiple items
module Map

from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> =
  interface IEnumerable
  interface IComparable
  interface IEnumerable<KeyValuePair<'Key,'Value>>
  interface ICollection<KeyValuePair<'Key,'Value>>
  interface IDictionary<'Key,'Value>
  new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
  member Add : key:'Key * value:'Value -> Map<'Key,'Value>
  member ContainsKey : key:'Key -> bool
  override Equals : obj -> bool
  member Remove : key:'Key -> Map<'Key,'Value>
  ...

Full name: Microsoft.FSharp.Collections.Map<_,_>

--------------------
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
val ofArray : elements:('Key * 'T) [] -> Map<'Key,'T> (requires comparison)

Full name: Microsoft.FSharp.Collections.Map.ofArray
val classify : (string -> int [])

Full name: Script.classify
val testCase : string

Full name: Script.testCase
Raw view Test code New version

More information

Link:http://fssnip.net/kD
Posted:10 years ago
Author:Zach Bray
Tags: ocr , kata , parsing