Skip to content

Instantly share code, notes, and snippets.

@klimisa
Created March 2, 2017 20:25
Show Gist options
  • Save klimisa/136ea8118d670ac9209c1c8c45d77576 to your computer and use it in GitHub Desktop.
Save klimisa/136ea8118d670ac9209c1c8c45d77576 to your computer and use it in GitHub Desktop.
-module(index).
-export([get_file_contents/1,show_file_contents/1
,split/1
]).
% Used to read a file into a list of lines.
% Example files available in:
% gettysburg-address.txt (short)
% dickens-christmas.txt (long)
% Get the contents of a text file into a list of lines.
% Each line has its trailing newline removed.
get_file_contents(Name) ->
{ok,File} = file:open(Name,[read]),
Rev = get_all_lines(File,[]),
lists:reverse(Rev).
% Auxiliary function for get_file_contents.
% Not exported.
get_all_lines(File,Partial) ->
case io:get_line(File,"") of
eof -> file:close(File),
Partial;
Line -> {Strip,_} = lists:split(length(Line)-1,Line),
get_all_lines(File,[Strip|Partial])
end.
% Show the contents of a list of strings.
% Can be used to check the results of calling get_file_contents.
show_file_contents([L|Ls]) ->
io:format("~s~n",[L]),
show_file_contents(Ls);
show_file_contents([]) ->
ok.
split(Name) ->
Ls = get_file_contents(Name),
split_lines(Ls).
split_lines(Ls) ->
split_lines(1,noemptylines(nopunct(Ls))).
split_lines(_,[]) ->
[];
split_lines(N,[L|Ls]) ->
[{N,string:tokens(L," ")}|split_lines(N+1,Ls)].
noemptylines([]) ->
[];
noemptylines([L|Ls]) ->
case L of
[] -> noemptylines(Ls);
_ -> [L|noemptylines(Ls)]
end.
nopunct([]) ->
[];
nopunct([X|Xs]) ->
case lists:member(X,".,;:\t\n\'\"") of
true ->
nopunct(Xs);
false ->
[ X | nopunct(Xs) ]
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment