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 )

Tagged: , ,

§ One Response to Dice’s Coefficient in F#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Dice’s Coefficient in F# at Lucid Musings.

meta

Follow

Get every new post delivered to your Inbox.