Search code examples
f#word-count

F#: Generating a word count summary


I am new to programming and F# is my first .NET language.

I would like to read the contents of a text file, count the number of occurrences of each word, and then return the 10 most common words and the number of times each of them appears.

My questions are: Is using a dictionary encouraged in F#? How would I write the code if I wish to use a dictionary? (I have browsed through the Dictionary class on MSDN, but I am still puzzling over how I can update the value to a key.) Do I always have to resort to using Map in functional programming?


Solution

  • While there's nothing wrong with the other answers, I'd like to point out that there's already a specialized function to get the number of unique keys in a sequence: Seq.countBy. Plumbing the relevant parts of Reed's and torbonde's answers together:

    let countWordsTopTen (s : string) =
        s.Split([|','|]) 
        |> Seq.countBy (fun s -> s.Trim())
        |> Seq.sortBy (snd >> (~-))
        |> Seq.truncate 10
    
    "one, two, one, three, four, one, two, four, five"
    |> countWordsTopTen
    |> printfn "%A" // seq [("one", 3); ("two", 2); ("four", 2); ("three", 1); ...]