Skip to content

Instantly share code, notes, and snippets.

@jadeallenx
Created June 26, 2017 22:41
Show Gist options
  • Save jadeallenx/186896d4ad837da989915430ca282b30 to your computer and use it in GitHub Desktop.
Save jadeallenx/186896d4ad837da989915430ca282b30 to your computer and use it in GitHub Desktop.
ngram builder for Erlang
-module(ngram).
-compile([export_all]).
ngram(N, Str) ->
make_ngram(N, Str, 1, orddict:new()).
make_ngram(_N, Str, Pos, Acc) when Pos >= length(Str) -> Acc;
make_ngram(N, Str, Pos, Acc) ->
make_ngram(N, Str, Pos+1, orddict:update_counter(lists:sublist(Str, Pos, N), 1, Acc)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment