Dice’s Coefficient in F#
November 29th, 2010 § 1 Comment
A simple short implementation of Dice’s Coefficient in F#. Array.map and Array.fold could be replaced by one function* but I feel its clearer as is.
*Array.fold (fun fset arr -> Set.union fset (bigrams arr)) Set.empty
let bigrams (s:string) =
s.ToLower().ToCharArray().[0..s.Length-2]
|> Array.fold (fun (set:Set,i) c -> set.Add(c.ToString()
+ s.[i+1].ToString()) , i + 1) (Set.empty, 0)
|> fst
let stringSimilarityDice (w1:string) (w2:string) =
let f a = a |> Array.map (fun str -> bigrams str)
|> Array.fold (Set.union) Set.empty
let s1 = w1.Split(' ') |> f
let s2 = w2.Split(' ') |> f
2. * float((Set.intersect s1 s2).Count) / float(s1.Count + s2.Count )
[...] Deen’s Dice’s Coefficient in F# “A simple short implementation of Dice’s Coefficient in F#. Array.map and Array.fold could be replaced by one function* but I feel its clearer as is.” [...]