Advertisement
Guest User

index

a guest
Mar 13th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 1.97 KB | None | 0 0
  1. -module(index).
  2. -export([index_file/1]).
  3.  
  4. readlines(Name) ->
  5.     {ok,File} = file:open(Name,[read]),
  6.     extract_content(File,[]).
  7.  
  8. extract_content(File,Partial) ->
  9.     case io:get_line(File,"") of
  10.         eof -> file:close(File), Partial;
  11.         Line -> {Strip,_} = lists:split(length(Line)-1,Line), extract_content(File,Partial ++ [Strip])
  12.     end.
  13.  
  14. show_file_contents([L|Ls]) ->
  15.     io:format("~s~n",[L]),
  16.     show_file_contents(Ls);
  17.  
  18. show_file_contents([]) -> ok.
  19.  
  20. nopunct([]) -> [];
  21. nopunct([X|Xs]) ->
  22.     case lists:member(X,".,;:\t\n\'\"") of
  23.         true -> nopunct(Xs);
  24.         false -> [X|nopunct(Xs)]
  25.     end.
  26.  
  27. dedup([],Y) -> Y;
  28. dedup([X|Xs],Y) ->
  29.     case lists:member(X,Y) of
  30.         false -> dedup(Xs,[X|Y]);
  31.         true -> dedup(Xs,Y)
  32.     end.
  33.  
  34. make_index([],Y,_) -> Y;
  35. make_index([X],Y,_) -> Y ++ [{X,X}];
  36. make_index([X,Nx|Xs],Y,Z) ->
  37.     case Nx == (X + 1) of
  38.         true -> make_index([Nx|Xs], Y, Z);
  39.         _ -> make_index([Nx|Xs], Y ++ [{Z,X}], Nx)
  40.     end.
  41.  
  42. combine_indices({X,I}, []) -> [{X,I}];
  43. combine_indices({X,I}, [{Y,Indices}|Ys]) ->
  44.     case X == Y of
  45.         true -> [{Y, Indices ++ I} | Ys];
  46.         _ -> [{Y,Indices}|combine_indices({X,I},Ys)]
  47.     end.
  48.  
  49. index_words([], Word) -> Word;
  50. index_words(Word, []) -> Word;
  51. index_words([X|Xs], Y) -> index_words(Xs,combine_indices(X,Y)).
  52.  
  53. create_word_index([], Y, _) -> Y;
  54. create_word_index([L|Ls], Y, N) ->
  55.     Words = string:tokens(nopunct(L)," "),
  56.     Entries = dedup(Words, []),
  57.     Indexes = lists:map(fun(X) -> {X,[N]} end, Entries),
  58.     create_word_index(Ls, index_words(Indexes,Y), N+1).
  59.  
  60. index([]) -> [];
  61. index(Lines) ->
  62.         lists:map(fun({Word, AllLines}) ->
  63.             {Word, make_index(AllLines, [], hd(AllLines))}
  64.         end, Lines).
  65.  
  66. % USAGE
  67. % index:index_file("./gettysburg-address.txt").
  68.  
  69. index_file(Name) ->
  70.     Lines = readlines(Name),
  71.     Indices = create_word_index(Lines,[],1),
  72.     index(Indices).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement