Advertisement
Guest User

blackbeard

a guest
Jan 18th, 2011
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 1.30 KB | None | 0 0
  1. -module(markov).
  2. -compile(export_all).
  3. -author({jha, abhinav}).
  4. -define(INITPOINT, 50).
  5. -define(ARITY, 3).
  6. start() -> start("corpus.txt").
  7. start(Corpus) ->
  8.     {ok, Bin} = file:read_file(Corpus),
  9.     Words = string:tokens(binary_to_list(Bin), "\n "),
  10.     Tab = ets:new(words, [bag]),
  11.     form_dictionary(Words, Tab, ?ARITY),
  12.     Start = mrandom(?INITPOINT),
  13.     State0 = lists:sublist(Words, Start, ?ARITY - 1),
  14.     Sentence = spew(Tab, State0, []),
  15.     ets:delete(Tab),
  16.     io:format("~p~n", [string:join(Sentence, " ")]).
  17.  
  18.  
  19. spew(Tab, [_|L]=State, Acc)->
  20.     case (length(Acc) >= 20) and (string:rchr(lists:last(L), $.) =:= length(lists:last(L))) of
  21.         true -> lists:reverse(Acc);
  22.         false ->
  23.             Objects = ets:lookup(Tab, list_to_tuple(State)),
  24.             {_,Snew} = lists:nth(mrandom(length(Objects)), Objects),
  25.             spew(Tab, L ++ [Snew], [Snew | Acc])
  26.     end.
  27.  
  28.  
  29. mrandom(Max) ->
  30.     {A, B, C} = now(),
  31.     random:seed(A,B,C),
  32.     random:uniform(Max).
  33.  
  34.  
  35. form_dictionary([], _, _) -> void;
  36. form_dictionary([H|T], Tab, N)->
  37.     ngrams([H|T], Tab, N, []),
  38.     form_dictionary(T, Tab, N).
  39.  
  40. ngrams([], _, _, _) -> void;
  41. ngrams([H|_], Tab, 1, Acc)-> ets:insert(Tab, {list_to_tuple(lists:reverse(Acc)), H});
  42. ngrams([H|T], Tab, X, Acc)-> ngrams(T, Tab, X-1, [H|Acc]).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement