0

I have added the code as it stands. It can used on any piece of text I am doing some work in Erlang and I am getting an error message which I have included below.

exception error: no function clause matching string:to_lower({error,[80,75,3,4,20,0,6,0,8,0,0,0,33,0,2020],                                                                     <<210,108,90,1,0,0,32,5,0,0,19,0,8,2,91,67,111,110,116,
                                                                        101,110,116,95,84,121,...>>}) (string.erl, line 2084)
     in function  word_sort:readlines/1 (word_sort.erl, line 17).

I have also included an extract of my code below and I would appreciate if I could get pointers on where I am going wrong.

enter code here -module(word_sort). enter code here-export([main/1]).

-export([unique/2]).
-export([sort/1]).
-export([readlines/1]).
-export([wordCount/3]).


% ========================================================== %
%  Load the file and create a list %
% ========================================================== %

readlines(FileName) ->
    io:format("~nLoading File : ~p~n", [FileName]),
    {ok, File} = file:read_file(FileName),
    Content = unicode:characters_to_list(File),
    TokenList = string:tokens(string:to_lower(Content), " .,;:!?~/>'<{}£$%^&()@-=+_[]*#\\\n\r\"0123456789"),
    main(TokenList).

% ========================================================== %
% Scan through the text file and find a list of unique words %
% ========================================================== %
main(TokenList) ->                                  
    UniqueList = unique(TokenList,[]),           
    io:format("~nSorted List : ~n"),
    SortedList = sort(UniqueList),          % Sorts UniqueList into SortedList%
    io:format("~nSorted List : "),

    io:format("~nWriting to file~n"),
    {ok, F} = file:open("unique_words.txt", [write]),
    register(my_output_file, F),
    U = wordCounter(SortedList,TokenList,0),
    io:format("~nUnique : ~p~n", [U]),
    io:fwrite("~nComplete~n").

wordCounter([H|T],TokenList,N) ->
    %io:fwrite("~p \t:  ~p~n", [H,T]),
    wordCount(H, TokenList, 0),
    wordCounter(T,TokenList,N+1);

wordCounter([], _, N) -> N.

% =============================================================%
%Word count takes the unique word, and searches the original list for occurrences of that word%
%==============================================================%
wordCount(Word,[H|T],N) ->
    case Word == H of           % checks to see if H is in Seen List
        true -> wordCount(Word, T, N+1);              % if true, N_Seen = Seen List
        false -> wordCount(Word, T, N)       % if false, head appends Seen List.
    end;

wordCount(Word,[],N) -> 
    io:fwrite("~p   \t:  ~p ~n", [N,Word]),
    io:format(whereis(my_output_file), "~p   \t: ~p ~n", [N,Word]).

%=================================================================================

unique([H|T],Seen) ->                       % Accepts List of numbers and Seen List
    case lists:member(H, Seen) of           % checks to see if H is in Seen List
        true -> N_Seen = Seen;              % if true, N_Seen = Seen List
        false -> N_Seen = Seen ++ [H]       % if false, head appends Seen List.
    end,
    unique(T,N_Seen);                       % calls uniques with Tail and Seen List.

%=================================================================================

unique([],Seen) -> Seen.                    

sort([Pivot|T]) ->                          
    sort([ X || X <- T, X < Pivot]) ++     
    [Pivot] ++                              
    sort([ X || X <- T, X >= Pivot]);

sort([]) -> [].     
9
  • tokenList is an atom, not a variable so replace it by TokenList. Commented Mar 25, 2018 at 9:12
  • and which version of Erlang do you using? Commented Mar 25, 2018 at 9:14
  • I am using Eshell V9.2, changing from tokenList to TokenList gives the same error. Commented Mar 25, 2018 at 12:24
  • what is content of the file? Commented Mar 25, 2018 at 12:49
  • it is a text file Commented Mar 25, 2018 at 12:51

1 Answer 1

1

unicode:characters_to_list returned some error.

Variable 'Content' contains error message instead of data.

And string:to_lower() got error message as parameter instead of string.

You need just check what characters_to_list returns to you.

readlines(FileName) ->
   io:format("~nLoading File : ~p~n", [FileName]),
   {ok, File} = file:read_file(FileName),
   case  unicode:characters_to_list(File) of
       Content when is_list(Content) ->
              LCcontent = string:to_lower(Content),
              TokenList = string:tokens(LCcontent, 
                              " .,;:!?~/>'<{}£$%^&()@-=+_[]*#\\\n\r\"0123456789"),
              main(TokenList);
       Err ->
           io:format("Cannot read file, got some unicode error ~p~n", [Err])
   end.
Sign up to request clarification or add additional context in comments.

3 Comments

I am using Eshell V9.2, changing from tokenList to TokenList gives the same error.
any suggestions on how I can improve on these 3
@RustHinges, Did you remember to recompile your module? Making changes to the source code is not enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.