Skip to content

Instantly share code, notes, and snippets.

@agustingianni
Created May 30, 2018 14:57
Show Gist options
  • Save agustingianni/3392cfa9cde34fbe881b9990c7e37041 to your computer and use it in GitHub Desktop.
Save agustingianni/3392cfa9cde34fbe881b9990c7e37041 to your computer and use it in GitHub Desktop.
open List
open String
(* Give a string return a dictionary of the words and the amount of times they are used *)
let add_word_count table word =
let count = match Hashtbl.find_opt table word with
| None -> 0
| Some x -> x + 1
in
Hashtbl.replace table word count
let get_word_frequency string =
let words = String.split_on_char ' ' string in
let freq = Hashtbl.create 4096 in
List.iter (fun word -> add_word_count freq word) words;
freq
let print_word (word, frequency) =
Printf.printf "word = %s frequency = %d\n" word frequency
let _ =
get_word_frequency "HOLA DRVINK"
|> Hashtbl.iter
|> List.iter print_word
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment