title: Understanding Eliza Chatbot in prolog
body: <p>I'm trying to understand how it really works the next eliza chatbot provided as example at:
  <a href="https://swish.swi-prolog.org/p/yet%20another%20eliza.pl" rel="nofollow noreferrer">https://swish.swi-prolog.org/p/yet%20another%20eliza.pl</a></p>
  <p>What I so far understand is, <strong>that at the beginning we have a list (Dictionary) empty that will be fulfill with the matches of each word and each integer.</strong>** The matching of words and integers will be performed through</p>
  <pre><code>match([N|Pattern], Dictionary, Target) :-
      integer(N), lookup(N, Dictionary, LeftTarget),
      append(LeftTarget, RightTarget, Target),
      match(Pattern, Dictionary, RightTarget).
  match([Word | Pattern], Dictionary, [Word | Target]) :-
      atom(Word), match(Pattern, Dictionary, Target).
  match([], _Dictionary, []).
  </code></pre>
  <p><strong>The matches will be saved in the dictionary through:</strong></p>
  <pre><code>lookup(Key, [(Key, Value) | _Dict], Value).
  lookup(Key, [(Key1, _Val1) | Dictionary], Value) :-
      Key \= Key1, lookup(Key, Dictionary, Value).
  </code></pre>
  <p>I have been also reading the explanation found in the book &quot;The art of prolog&quot; of Leon Sterling and Ehud Shapiro but I can't really get it</p>
  <p>I would appreciate your help</p>
  <pre><code>eliza :-
      write('? '), read_word_list(Input), eliza(Input), !.
  
  eliza([bye]) :-
      write('Goodbye. I hope I have helped you'), nl.
  eliza(Input) :-
      pattern(Stimulus, Response),
      match(Stimulus, Dictionary, Input),
      match(Response, Dictionary, Output),
      reply(Output),
      !, eliza.
  
  match([N|Pattern], Dictionary, Target) :-
      integer(N), lookup(N, Dictionary, LeftTarget),
      append(LeftTarget, RightTarget, Target),
      match(Pattern, Dictionary, RightTarget).
  match([Word | Pattern], Dictionary, [Word | Target]) :-
      atom(Word), match(Pattern, Dictionary, Target).
  match([], _Dictionary, []).
  
  pattern([i,am,1],[how,long,have,you,been,1,'?']).
  pattern([1,you,2,me],[what,makes,you,think,i,2,you,'?']).
  pattern([i,like,1],[does,anyone,else,in,your,family,like,1,'?']).
  pattern([i,feel,1],[do,you,often,feel,that,way,'?']).
  pattern([1,X,2],[can,you,tell,me,more,about,your,X,'?']) :- important(X).
  pattern([1],[please,go,on]).
  
  important(father).
  important(mother).
  important(son).
  important(sister).
  important(brother).
  important(daughter).
  
  reply([Head | Tail]) :-
      write(Head), write(' '), reply(Tail).
  reply([]) :- nl.
  
  lookup(Key, [(Key, Value) | _Dict], Value).
  lookup(Key, [(Key1, _Val1) | Dictionary], Value) :-
      Key \= Key1, lookup(Key, Dictionary, Value).
  
  
  read_word_list(Ws) :-
      read(A),
      tokenize_atom(A, Ws).
  </code></pre>