This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Probability = Map<string , Map<string,float> * float> | |
let mapAddGeneric m a f i = | |
if m |> Map.containsKey a then | |
Map.add a (f m.[a]) m | |
else Map.add a i m | |
let inline pairAdd (a,b) (c,d) = a + c , b + d | |
let inline div (a,b) = a / b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let third (a,b,c) = c | |
let fst3 (a,b,c) = a | |
let split op c (data: 'a []) = | |
data.[1..data.Length - 1] |> Array.fold (fun (bset, curTransform, i) curbit -> | |
if i = c then | |
bset |> Set.add (hash curTransform), curbit, 1 | |
else bset, op curTransform curbit, i + 1) (Set.empty , data.[0], 1) | |
let minhash vset = vset |> Set.minElement |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//based on http://mwh.geek.nz/2009/04/26/python-damerau-levenshtein-distance/ | |
let damerauLevenshteinDistance (arr1:'a []) (arr2:'a []) = | |
let wrap j k = if j = k then arr2.Length else j - 1 - k | |
let rec outer (oneback:int[]) (twoback:int[]) s = function | |
| i when i = arr1.Length || arr2.Length = 0 -> s | |
| i -> let thisrow = Array.zeroCreate (arr2.Length+1) in thisrow.[thisrow.Length - 1] <- i + 1 | |
for j in 0..arr2.Length - 1 do | |
let delcost, addcost, subcost = oneback.[j] + 1, thisrow.[wrap j 0] + 1, | |
oneback.[wrap j 0] + if arr1.[i] <> arr2.[j] then 1 else 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let foldRow2D, foldCol2D = 1, 0 | |
let inline (@@) (m: 'a [,]) index = Array.Parallel.init (m.GetLength(1)) (fun i -> m.[index, i]) | |
let inline (@@@) (m: 'a [,]) index = Array.Parallel.init (m.GetLength(0)) (fun i -> m.[i, index]) | |
let inline cIndex ind k i (m:'a [,]) = if ind = 1 then m.[k,i] else m.[i,k] | |
let arr2DFoldAt r k (m : 'a [,]) f seed = | |
let top = m.GetLength(r) | |
let rec fold state = function | |
| i when i = top -> state | |
| i -> fold (f state (cIndex r k i m)) (i + 1) |