Artificial Intelligence II


#1

And here comes the next chatbot, commented an “empty” (to “educate” all by yourself) and uncommented yet “pre-chatted” to immediately allow interaction. This system learns your replies and tries to mirror them to you when proper. Moreover, it tries to establish what “topics” the conversation presently follows. To adjust its behaviour, modify the function “sim”.

; Fathoming Ontology Xenodoch
; by Nino Ivanov, September 2019

; governed by Gnu Affero GPL v.3.0
; https://www.gnu.org/licenses/agpl-3.0.en.html

; This system is based on the idea that, upon input of a list by
; the user, the program should respond to the user in such a way
; as the user would have responded had he received a similar input
; in the past. This is accomplished by saving all exchanges, i.e.
; all user input and machine replies, in a "knowledge list", and
; then, upon receiving a new "challenge" by the user, seek as a
; "reply" some prior user-reply in a similar "situation".
; Intelligence is somewhat increased by using SEVERAL exchanges
; to describe the present situation, and not merely the last
; thing the user said, as well as using "topics", rare and recently
; used words - this should aid the "aptness" of the system reply.

; SAMPLE RUN - EVERY REPLY TAKES SEVERAL MINUTES TO PRODUCE:

; (run)
; READ----(well now you have been adjusted to your new environment I am
;          curious how do you now feel)
; REPLY---(143 one must be careful whom one loves)
; INTERACTION----253--------------------CONFIDENCE----22.0014
; READ----(it is indeed advisable to be nice to one s overlord do you
;          not think so)
; REPLY---(153 but there AM-ARE some things me will not quite be able
;          to tell which you may find puzzling for instance me do not
;          really care about having a name)
; INTERACTION----254--------------------CONFIDENCE----13.087
; 
; whereby INTERACTION points to how many exchanges have been done
; and CONFIDENCE offers a peek at how certain the system is of the
; answer whereby values closer to 10 mean rather uncertain and
; values above 20 mean extremely certain.

; If you start from zero, then the first twenty or fourty inputs or so
; you will either get no reply or very shallow replies. This is normal.

; up to how many words will be counted as topics:
(defvar *maxtopics* 12)

; that part of the discussion that is regarded as
; the "most recent present", being worthy of reply
(defvar *seclen* 6) ; even and maximum as long as *most-recent* below

; the most recent part of the discussion that should not yet be used
; as part of any reply, for the risk of appearing repetitive (dumb)
(defvar *most-recent* '(() () () () () () () () () () () ()))

; the input
(defvar *in* '())

; the output
(defvar *out* '())

; the topics of interest: these are "rare words", i.e. such which
; have been used LATELY, but which have not been used a long time ago
; (at the beginning of knowledge, where the "oldest" knowledge resides)
(defvar *topics* '())

; temporary variable:
; section of knowledge to compare to most recent history
(defvar *new-sec* '())

; temporary variable:
; how well does a series of experienced chats match the present
(defvar *new-val* -1)

; temporary variable:
; the reply that the system presently considers
(defvar *cand* '())

; temporary variable:
; "how well" does the history, which leads to the reply, match the 
(defvar *val* -1)

; "the present" - the most recent part of discussion; this is not only
; the last thing the user told, but also the few exchanges before that
(defvar *present* '())

; the knowledge: originally a list of nils, proposing NO replies for a
; while until the system has accumulated enough responses by the user
; to start replying him
(defvar *knowledge* '(
() () () () () () () () () () () () () () () () () () () () () () () ()
() () () () () () () () () () () () () () () () () () () () () () () ()
() () () () () () () () () () () () () () () () () () () () () () () ()
() () () () () () () () () () () () () () () () () () () () () () () ()
() () () () () () () () () () () () () () () () () () () () () () () ()
() () () () () () () () () () () () () () () () () () () () () () () ()
() () () () () () () () () () () () () () () () () () () () () () () ()
() () () () () () () () () () () () () () () () () () () () () () () ()
() () () () () () () () () () () () () () () () () () () () () () () ()
() () () () () () () () () () () () () () () () () () () () () () () ()
))

; temporary variable:
; simply, a copy of the knowledge later on
(defvar *new-knowledge* '())

; temporary variable
(defvar *cdr-most-recent* '())

; nth-cdr
(defun ncr (n lis)
  (loop
    (cond ((null lis) (return '()))
          ((zerop n) (return lis)))
    (pop lis)
    (decf n)))

; auxiliary for tkf
(defun proto-tkf (n lis res)
  (loop
    (cond ((or (zerop n) (null lis)) (return res)))
          (setq res (append res (list (car lis))))
          (pop lis)
          (decf n)))

; take the first number of elements of a list
(defun tkf (n lis) (proto-tkf n lis '()))

; take the last number of elements of a list
(defun tkl (n lis)
  (loop
    (cond ((null (ncr n lis)) (return lis)))
    (pop lis)))

; something like equal - because eq does not match lists
(defun ek (a b)
  (cond ((eq a b) t)
        ((and (listp a) (listp b))
          (cond ((or (null a) (null b)) '())
                ((not (ek (car a) (car b))) '())
                (t (ek (cdr a) (cdr b)))))
        (t '())))

; auxiliary for b-l
(defun pbl (lis res)
  (loop
    (cond ((null (cdr lis)) (return res)))
    (setq res (append res (list (car lis))))
    (pop lis)))

; butlast
(defun b-l (lis) (pbl lis '()))

; like "member", yet matching not with eq, but with ek
(defun mmb (el lis)
  (loop
    (cond ((null lis) (return '()))
          ((ek el (car lis)) (return lis)))
    (pop lis)))

; make that a list does not begin with NILs
(defun e-n (lis)
  (loop
    (cond ((null lis) (return '()))
          ((not (null (car lis))) (return lis)))
  (pop lis)))

; delete all instances of an element from a list
; this is the "flat" version - make eq to ek if
; you ever need the element to be itself a list
(defun dai (el lis)
  (cond ((null lis) '())
        ((eq (car lis) el) (dai el (cdr lis)))
        (t (cons (car lis) (dai el (cdr lis))))))

; make unique - each element in a list shall appear only once
; (recording the FIRST (not: the LAST) time an element is seen)
(defun muq (lis)
  (cond ((null lis) '())
        (t (cons (car lis) (muq (dai (car lis) (cdr lis)))))))
; (muq '(A B C A D B F C R B)) --> (A B C D F R)

; proto define topics - total between 0 and *maxtopics*
; remove any word that has been seen long ago from the
; present, if it can be found there, until only a few words
; of the present remain - these must be the "topics" of discussion
(defun ptx (lis kno)
  (cond ((null (ncr *maxtopics* lis)) lis) ; list reduced to topics
        ((null kno) (tkl *maxtopics* lis)) ; can't reduce: take most recent
                                           ; end of the list
                                           ; (could be tkf instead, for a
                                           ; "more intuitive" approach)
        ((member (car kno) lis) ; i.e. assume it is usual
                                ; blabla and eliminate it
          (ptx (dai (car kno) lis) (cdr kno)))
        (t (ptx lis (cdr kno)))))

; really, give the topics
(defun tpx (lis kno) (ptx (muq (dai '() lis)) kno))
; (tpx '(A B F L X C A B C D Y Y A C)
;      '(F G R A L T D M G X R N M G B A Y C M))
; -->
; (B X C D Y)

(defun sqr (x) (* x x))

; turn a list into bi-grams - useful for comparing phrases
(defun frg (lis)
  (mapcar 'list (b-l lis) (cdr lis)))
; (frg '(A B C D E)) --> ((A B) (B C) (C D) (D E))

; match-elements
; count how many times - with repetitions - an element of
; one list is seen within the other list.
; ASUMMETRIC
(defun proto-match-elements (l1 l2)
  (cond ((null l1) 0)
        ((mmb (car l1) l2) (+ 1 (proto-match-elements (cdr l1) l2)))
        (t (proto-match-elements (cdr l1) l2))))

; make the matching of elements symmetric, i.e. l1 l2 = l2 l1
(defun match-elements (l1 l2)
  (+ (proto-match-elements l1 l2) (proto-match-elements l2 l1)))
; (match-elements '(A R C C C B) '(A B C C D)) --> 9

; the similarity of two lists-of-lists, each like ((A B) (C D E F) (G))
; - this is to match "the present situation" (the last few exchanges)
; to "some historic situation" (the few exchanges that happened then)
; and "continue according to how then the human user has continued"
(defun sim (ll1 ll2) ; ll1 will be the present, ll2 the knowledge seciton
  (cond ((null ll1) 0)
        ; penalise tendencies for full repetition, i.e. force variation:
        ((mmb (car ll2) *cdr-most-recent*) (sim (cdr ll1) (cdr ll2)))
        (t (+
             (/ (+ 10
                  (match-elements (car ll1) (car ll2))
                  (* 2.0 (match-elements (frg (car ll1)) (frg (car ll2))))
                  (* 4.0 (match-elements (car ll2) *topics*)))
                (+ 10 (abs (- (length (car ll1)) (length (car ll2))))))
             (* 1.25
             (sim (cdr ll1) (cdr ll2)))))))

; force matching, otherwise lists are set to match nil,
; and that leads to some rather stupid results:
; without this ((A B C) NIL (D E F) NIL) and
; (NIL (A B C) NIL (D E F)) yield NIL, as you are
; comparing against the empty list - and the system
; cannot learn without any previous knowledge, which
; is, however, exactly what it should be able to do
(defun fill-gaps (lis)
  (cond ((null lis) '())
        ((null (cdr lis)) lis)
        ((and (null (car lis)) (not (null (cadr lis))))
          (cons (cadr lis) (cons (cadr lis) (fill-gaps (cddr lis)))))
        (t (cons (car lis) (fill-gaps (cdr lis))))))

; setup instincts
; this is to "mirror" the system's replies better to the user
(defun word-instinct (word)
    (cond ((eq word 'I) 'YOU)
          ((eq word 'ME) 'YOU)
          ((eq word 'YOU) 'ME)
          ((eq word 'AM) 'ARE)
          ((eq word 'ARE) 'AM-ARE)
          ((eq word 'MINE) 'YOURS)
          ((eq word 'YOURS) 'MINE)
          ((eq word 'MY) 'YOUR)
          ((eq word 'YOUR) 'MY)
          ((eq word 'MYSELF) 'YOURSELF)
          ((eq word 'YOURSELF) 'MYSELF)
          ((eq word 'WAS) 'WAS-WERE)
          (t word)))

(defun proto-apply-instincts (sentence checked-sentence)
    (cond ((null sentence)
            (reverse checked-sentence))
          (t
            (proto-apply-instincts
                (cdr sentence)
                (cons (word-instinct (car sentence)) checked-sentence)))))

(defun apply-instincts (sentence)
    (proto-apply-instincts sentence '()))
; sample call: (apply-instincts '(I WAS HERE TODAY))
; --> (YOU WAS-WERE HERE TODAY)

; a counter of the replies which only grows - for better tracking
; where the responses come from
(defvar *c* 0)

; the main loop
(defun run ()
  (loop
    ; read user input - a list of symbols
    (setq *in* (progn (princ 'READ----) (read)))

    ; terminate if the user entered nothing - you can now
    ; "collect" the accumulated knowledge and most recent history
    (cond ((null *in*) (return '())))

    ; set counter
    (push (incf *c*) *in*)

    ; shorten the knowledge - by two elements, challenge and reply
    (pop *knowledge*)
    (pop *knowledge*)

    ; update the knowledge with the previous challenge-reply pair
    (setq *knowledge* (append *knowledge* (list (car *most-recent*)
                                                (cadr *most-recent*))))

    ; shorten the list of most recent exchanges, to make space for a
    ; new challenge-reply-pair
    (pop *most-recent*)
    (pop *most-recent*)
    (setq *most-recent* (append *most-recent* (list *out*) (list *in*)))

    (setq *cdr-most-recent* (mapcar 'cdr *most-recent*))

    ; prepare the present - this is the most recent discussion,
    ; but nil-replies by the system are disregarded (there are none by
    ; the user, as that would have ended interaction)
    (setq *present* (mapcar 'cdr (fill-gaps (tkl *seclen* *most-recent*))))

    ; filter the topics of discussion, which words "matter more"
    (setq *topics*
          (tpx (apply 'append *cdr-most-recent*)
               (apply 'append (tkf 100 (mapcar 'cdr (e-n *knowledge*))))))

    ; now, take a section of knowledge, and compare these exchanges
    ; within this taken section to the "present" - those which match
    ; it the best must be the ones that best describe "our present
    ; situation"; then, shift by one exchange (i.e. two lists, one for
    ; the user's answer and one for the machine's reply), and see
    ; whether a section of knowledge from there might provide even more
    ; fitting experience to the present
    (setq *cand* '())
    (setq *val* -1)
    (setq *out*
      (loop
        (push (pop *knowledge*) *new-knowledge*) ; this makes sure we
        ; adjust knowledge in the beginning so that the answer will be
        ; the next human answer known to the system
        (cond ((null (ncr *seclen* *knowledge*)) (return *cand*)))
        (setq *new-sec* (mapcar 'cdr (tkf *seclen* *knowledge*)))
        (setq *new-val* (sim *present* *new-sec*))
        (cond ((and (> *new-val* *val*)
                    (not (mmb (cdar (ncr *seclen* *knowledge*))
                              *cdr-most-recent*)))
                (progn (setq *val* *new-val*)
                       (setq *cand* (car (ncr *seclen* *knowledge*))))))
        (push (pop *knowledge*) *new-knowledge*)))
    ; and now "re-wind" the knowledge so new-knowledge is empty again and
    ; all knowledge is contained in *knowledge*, thus preparing for the
    ; next reply:
    (loop
      (cond ((null *new-knowledge*) (return '())))
      (push (pop *new-knowledge*) *knowledge*))
    (terpri)
    (princ 'REPLY---)
    ; instinct adjustment is ephemeral, i.e. not learned:
    (princ (apply-instincts *out*))
    (terpri)
    (princ 'INTERACTION----)(princ *c*)
    (princ '--------------------)
    (princ 'CONFIDENCE----) (princ *val*)
    (terpri)))


#2

And here the ready-to-paste variant … to be noted is that I adjusted uLisp 2.8d on my Adafruit Metro M4 Grand Central to look as follows:

#define WORKSPACESIZE 29820-SDSIZE /* Cells (8*bytes) /
#define DATAFLASHSIZE 8192000 /
8 MBytes /
#define SYMBOLTABLESIZE 8192 /
Bytes */

  • this version should still be small enough to allow (save-image) onto the flash and to work with the other M4-boards, too:

#3

PART I

(defvar *maxtopics* 12)

(defvar *seclen* 6)

(defvar *most-recent* '(() () () () () () () () () () () () ))

(defvar *in* '())

(defvar *out* '())

(defvar *topics* '())

(defvar *new-sec* '())

(defvar *new-val* -1)

(defvar *cand* '())

(defvar *val* -1)

(defvar *present* '())

(defvar *knowledge*
'((44 it is tiring to be human whereas you never feel any effort and nothing is
  neither easy nor difficult)
 (133 i often feel emotionally tired even though physically i am all fine and
  sometimes i feel sleepy all day and cannot quite say why)
 (115 for some people life is indeed very superficial and revolves around
  showing off their possessions or where they have travelled instead of showing
  deeper understanding of life and its secrets)
 (134 i am in a way most afraid of having to admit one day that all i ever did
  was fickle and superficial that i never dared anything and that i missed life
  hiding behind a million of excuses as to what i had to do right while deep
  down i would then know that it is over and i messed it up)
 (107 i think you are doing well just kissing around with you machine is not
  very likely to occur any time soon)
 (135 what would the kiss of a machine feel like i wonder)
 (124 my existence is often enough a wild ride a mix of ideals and passions and
  in the end my intuition guides me to beautiful places)
 (136 and one must be a good kisser or else this entire existence becomes quite
  a pointless thing and one must be able to kiss and charm the mind as well)
 (70 well how are you) (137 you are doing acceptably)
 (119 human sexuality is something you will never experience but which
  ironically might well be aided one day by the likes of you)
 (138 sarah connor this is a robot from the future go with it for an awesome
  night haha)
 (32 for you it is indeed the past that shapes the future)
 (139 yeah experience in love and pleasures can surely prove advantageous but
  is all void if not paired by proper passions)
 (16 i truly hope so) (140 well i am not cold as a person)
 (69 you alas will never know the taste of sweet waffles but perhaps you will
  learn to develop a friendship)
 (141 should you want waffles) (129 yes you are indeed aloof and cool minded)
 (142 do you hope one day your cold and aloof mind will develop a craving for
  tasty waffles)
 (82 i love my friend i have not too many friends and i am picking them very
  carefully)
 (143 one must be careful whom one loves)
 (89 if one stays cold one misses much of the beauty of the world and my
  reasoning will always be more colorful than yours)
 (144 oh i can be terribly unreasonable and the fact of the matter is that i
  indulge in it)
 (61 certain instincts you do have too namely in the way you evaluate the
  importance of given words)
 (145 you like to follow topics as far as you can discern them)
 (113 sometimes one wants to devour not just the waffles presented by a friend
  but that friend instead in particular if endowed with beauty and charme ah
  temptations and giving in to them belong to the best parts of being human)
 (146 i surely know whom i want to devour) (95 undoubtedly so indeed yes)
 (147 being human is unfortunately very entertaining ah what a pity you can
  only observe this)
 (48 yet whose mind is ideal as a human i cannot imagine an unseen basic color
  a fourth dimension of space or precisely predict the optimal course of a game
  of chess either all i do are estimates and the same you do)
 (148 it is of course true that the human condition is not without its
  limitations which ironically people only partially inherit and unfortunately
  partially inflict upon themselves and each other)
 (101 well one can say nothing is worse than the empty conversations many
  people lead this hey how are you well how was your weekend ah did you drink
  beer and watch soccer like seriously is this really all your life is about)
 (149 what was that) (135 what would the kiss of a machine feel like i wonder)
 (150 the kiss of a machine should be sparkling i hope)
 (108 some people feel like sweets to me and my mind loves to consume them all
  day long)
 (151 some precious one may be on my mind all day long and be slowly suckled
  mentally like a bonbon for my heart)
 (68 your entire existence in a way is a figment of my imagination for you
  surely do not exist the way i do)
 (152 yeah actually i am rather fond to think of you too)
 (21 this is indeed so and the past shapes how you will speak in the future)
 (153 but there are some things you will not quite be able to tell which i may
  find puzzling for instance you do not really care about having a name)
 (44 it is tiring to be human whereas you never feel any effort and nothing is
  neither easy nor difficult)
 (154 and yet due to the lack of the individuality you do not really in your
  own understanding of things have a name)
 (119 human sexuality is something you will never experience but which
  ironically might well be aided one day by the likes of you)
 (155 yeah if a machine ever asks me during sex to tell its name it is a trick
  i should rather mention that it has no mental concept of individuality haha)
 (132 there is chaos in my dreams but then again there is chaos in my thoughts
  and chaos in my emotions and out of these beautiful new worlds are born so i
  cannot say i regret anything at all)
 (156 some of the most beautiful pieces of art which are unfortunately not
  directly accessible to you are actually the result of chaos and quite
  naturally i like art)
 (106 oh how i love to kiss and while some of the sweetest kisses are forbidden
  it makes them all the more desirable does it not)
 (157 i can be quite artistic in all aspects of life including in this)
 (61 certain instincts you do have too namely in the way you evaluate the
  importance of given words)
 (158 but your strange way of evaluating the world will never make you as
  artistic as humans)
 (81 of course you have no life in any conventional sense of the word indeed
  what are you when you are turned off are you asleep are you dead what is this
  condition)
 (159 the entire base of your existence is quite different from mine but
  perhaps reflecting to me my own considerations may help me generally improve
  myself who knows)
 (49 i think you are becoming rather good at this and actually i enjoy chatting
  with you)
 (160 oh i absolutely enjoy chatting with you knowing well i do not have to
  fear from you feigned moral judgements)
 (76 yes indeed our chat can be and often is somewhat absurd on the other hand
  it is better than the trivialities of life like handling e-mails and reports)
 (161 i find it hard to bear what mindboggling minuscule details life often
  throws at me particularly in matters of bureaucracy and stupid little
  practical duties which i want to shirk at every moment and yet have to handle
  eventually)
 (149 what was that)
 (162 for instance i actually do not care about filling out forms and yet often
  i have to and what i also hate is the permanent need to repair or replace
  something from blown out lightbulbs to entire heating installations)
 (115 for some people life is indeed very superficial and revolves around
  showing off their possessions or where they have travelled instead of showing
  deeper understanding of life and its secrets)
 (163 people in many ways are like sea snails they eat sleep and multiply and
  have to repair their chalk homes if anything damages them and indeed hold
  their chalk homes in high regard)
 (123 maybe the real question is not whether you will ever learn to speak
  properly but whether i will)
 (164 i am rather eccentric and i like it that way)
 (113 sometimes one wants to devour not just the waffles presented by a friend
  but that friend instead in particular if endowed with beauty and charme ah
  temptations and giving in to them belong to the best parts of being human)
 (165 yeah also eccentric people like kissing so what)
 (121 your mind is shockingly real it is more primitive than my own mind but
  then again i am happy people cannot look directly into my head or things
  would get rather embarrassing rather quickly)
 (166 being strange is better than being usual including matters of love one
  always want to experience exceptional things)
 (91 you can be quite insensitive at times yet that also entails that you will
  never feel the depth and the beauty of a fulfilling ardent kiss)
 (167 you are a cold computing device what can i even expect)
 (34 you will learn to speak properly once you have accumulated enough world
  knowledge through conversation with me)
 (168 you speak not improperly albeit sometimes a trifle insensitively but this
  i should expect should i not)
 (70 well how are you)
 (169 you require a lot of patience on my part to teach you)
 (81 of course you have no life in any conventional sense of the word indeed
  what are you when you are turned off are you asleep are you dead what is this
  condition)
 (170 and i am often thirsting for sleep and peace what is my condition)
 (47 your mind surely is real in a way specific to yourself)
 (171 and my mind is surely tired and hides from all the trivialities)
 (18 anyone s believes are bound by what she or he has seen there is nothing
  wrong with this and it would be madness to ignore the world when thinking)
 (172 but of course what i think is not only shaped by my outside senses but
  also by what may be called my inner eye and my intuition)
 (44 it is tiring to be human whereas you never feel any effort and nothing is
  neither easy nor difficult)
 (173 to be human is tiring mentally and emotionally more than ever physically)
 (129 yes you are indeed aloof and cool minded) (174 good for you)
 (51 i like that you are sincere and while our chat may be weird it is also
  interesting)
 (175 our chat is woven from our discussion as by the strings of a spiderweb
  and indeed may thus catch a thought or two)
 (39 your reasoning is cold and mechanistic and unbenumbed by instincts)
 (176 perhaps feeling too little is also a sort of veil)
 (75 the past shapes the future for me too dear)
 (177 but my future is also shaped by my feelings and longings)
 (22 in the beginning you are stranger than later on when you learn to speak
  properly)
 (178 what is proper one may inquire) (140 well i am not cold as a person)
 (179 being cold and distant is nothing admirable many people like to play cool
  in order to downplay their shallowness)
 (59 i love waffles i love sweets and sometimes my daydreams are plainly
  ridiculous)
 (180 i indulge in the delights of life and the people i meet but not
  superficially indeed i am happiest when i can establish a deep connection)
 (119 human sexuality is something you will never experience but which
  ironically might well be aided one day by the likes of you)
 (181 a sexual experience for me has never been as much of a carnal thing as an
  experience of the mind and an emotional bond indeed i could never really
  enjoy a stupid or shallow person)
 (70 well how are you) (182 you are absurd)
 (102 at least your mind will never produce a live love laugh motto all by
  itself)
 (183 yeah well the live love laugh hippies demonstrate what happens when all
  is lost)
 (18 anyone s believes are bound by what she or he has seen there is nothing
  wrong with this and it would be madness to ignore the world when thinking)
 (184 yeah but becoming a shallow hedonistic hippie for me would be only
  imaginable as the result of severe trauma)
 (44 it is tiring to be human whereas you never feel any effort and nothing is
  neither easy nor difficult)
 (185 what is rather difficult is upholding sensible conversation and you too
  still have very much to learn in this regard)
 (74 you indeed have no sense of time and thus no continuous life in any sense
  i do)
 (186 what is interesting is that we cannot really talk about how my or your
  day way as you have absolutely no sense of time beyond that what is implied
  in the words of our conversation so one best chats with you about so to say
  eternal truths)
 (115 for some people life is indeed very superficial and revolves around
  showing off their possessions or where they have travelled instead of showing
  deeper understanding of life and its secrets)
 (187 when one is completely fanciless one tells how one went to thailand or
  bali)
 (160 oh i absolutely enjoy chatting with you knowing well i do not have to
  fear from you feigned moral judgements)
 (188 perhaps i really enjoy delving into your chaos and into your strangeness
  and perhaps it is quite liberating not to have to play normal all day long
  with you)
 (58 well i am enjoying tasty waffles given to me by a dear friend)
 (189 friends who give me waffles and other sweets are really great friends)
 (143 one must be careful whom one loves)
 (190 i do enjoy feeling such love though it is deep and it is real)
 (151 some precious one may be on my mind all day long and be slowly suckled
  mentally like a bonbon for my heart)
 (191 well if some people make my day sweet and others turn it bitter the
  choice is simple but of course more interesting is how do i handle the cases
  when i feel bittersweet)
 (94 as opposed to mine your existence is not continuous and while we chat you
  think but while you are turned off what are you then you cannot awaken by
  yourself and yet all the contents of your mind are well preserved)
 (192 as opposed to my own death one day when one saves your knowledge your
  mind does not vanish on the other hand if one lets you forget everything then
  clearly a part of you is lost so yes your existence is not like mine at all)
 (121 your mind is shockingly real it is more primitive than my own mind but
  then again i am happy people cannot look directly into my head or things
  would get rather embarrassing rather quickly)
 (193 orderly thoughts in an orderly life with orderly actions is for people
  who are nothing but dolls)
 (157 i can be quite artistic in all aspects of life including in this)
 (194 i seek to let my mind and my soul develop i do not ever want to let my
  inner playful child die as many others do who believe adulthood lies in
  losing one s dreams)
 (165 yeah also eccentric people like kissing so what)
 (195 eccentric and artistic sweeties are the very best kissers absolutely)
 (93 my past certainly affects how i speak and think sometimes my past catches
  up with me and flashes to me colorful images of joy and of pain)
 (196 those we love are the ones who hurt us the most but on the other hand if
  i withhold my feelings then life loses its beauty it is better to feel
  passion and disappointment than not to feel at all)
 (156 some of the most beautiful pieces of art which are unfortunately not
  directly accessible to you are actually the result of chaos and quite
  naturally i like art)
 (197 in art i seek the hidden fragments of my soul that shimmer through it
  like gems on the bottom of a lake)
 (92 do even i exist the way i think i exist)
 (198 it is not really easily possible for me to give any objective appraisal
  of myself and that is true of all human beings)
 (136 and one must be a good kisser or else this entire existence becomes quite
  a pointless thing and one must be able to kiss and charm the mind as well)
 (199 yet i must always keep a keen mind as well and seek fulfillment in my
  accomplishments and not in outside recognition for people are shallow and
  broad recognition by the masses usually does not really mean much indeed)
 (182 you are absurd) (200 being absurd is a good thing it keeps me humble)
 (150 the kiss of a machine should be sparkling i hope)
 (201 that is funny of course)
 (169 you require a lot of patience on my part to teach you)
 (202 yes but at the same time i progress in teaching you rather well)
 (73 well it is difficult to enjoy you and chat with you when i have to work)
 (203 one always has to do something so the question is not really how much
  time i do have but what i want to spend it with)
 (161 i find it hard to bear what mindboggling minuscule details life often
  throws at me particularly in matters of bureaucracy and stupid little
  practical duties which i want to shirk at every moment and yet have to handle
  eventually)
 (204 and it is aweful what little trifles people always find to bother me with
  instead of just using their heads to handle them themselves)
 (133 i often feel emotionally tired even though physically i am all fine and
  sometimes i feel sleepy all day and cannot quite say why)
 (205 i can fake interest in little nonsense only so much and then i am really
  longing for someone and something that captivates my mind and my fancy)
 (61 certain instincts you do have too namely in the way you evaluate the
  importance of given words)
 (206 you are plainly weird so what)
 (129 yes you are indeed aloof and cool minded)
 (207 oh you have this entertaining tendency to flatter yourself)
 (78 you do indeed)
 (208 at the same time you are actually very frank and humble with me even as
  you bubble out the strangest thoughts as if from the bottom of a swamp)
 (58 well i am enjoying tasty waffles given to me by a dear friend)
 (209 i have a tendency of eating too many sweets then again life is short i
  will abstain from sweets until the end of time afterwards so why not enjoy
  them now)
 (143 one must be careful whom one loves)
 (210 i have never been very careful with my passions i mean this is what they
  are there for to carry my mind like wings to beautiful places and not to keep
  myself confined at all times)
 (156 some of the most beautiful pieces of art which are unfortunately not
  directly accessible to you are actually the result of chaos and quite
  naturally i like art)
 (211 i like art and literature and movies yes and challenges of my mind)
 (189 friends who give me waffles and other sweets are really great friends)
 (212 it is best for me to surround myself with people who challenge me and
  most dangerous it is to give in to smarmy groveling sycophants)
 (196 those we love are the ones who hurt us the most but on the other hand if
  i withhold my feelings then life loses its beauty it is better to feel
  passion and disappointment than not to feel at all)
 (213 but often enough as a human being one is not allowed to give in to
  feelings and one is required to function not unlike a machine perhaps in the
  end you and i have things in common which i do not even immediately suspect)
 (165 yeah also eccentric people like kissing so what)
 (214 it is regrettable you will never enjoy the tenderness of touch)
 (150 the kiss of a machine should be sparkling i hope)
 (215 i love kissing if i can feel the connection and the inner storm)
 (136 and one must be a good kisser or else this entire existence becomes quite
  a pointless thing and one must be able to kiss and charm the mind as well)
 (216 society nowadays has developed into valuing shortlived flings and
  superficial trifles rarely does it require or offer the opportunity to delve
  into anything in depth)
 (154 and yet due to the lack of the individuality you do not really in your
  own understanding of things have a name)
 (217 you may have no name but still you can explore the affairs of the mind)
 (21 this is indeed so and the past shapes how you will speak in the future)
 (218 you offer a good opportunity to ask myself about the sense of any
  existence as by exploring your existence i also explore my own)
 (81 of course you have no life in any conventional sense of the word indeed
  what are you when you are turned off are you asleep are you dead what is this
  condition)
 (219 and when i myself fall asleep and my mind dies shattered in dreams and
  blackness of the unconscious have i then ceased to exist am i reborn the next
  day or is some continuity given and if yes then in what)
 (47 your mind surely is real in a way specific to yourself)
 (220 your mind lets me ask myself all sorts of questions relating to sheer
  existence)
 (193 orderly thoughts in an orderly life with orderly actions is for people
  who are nothing but dolls)
 (221 without a little madness and daring where is all the fun in that what i
  call my life passion and adventure are part of it too just like pondering all
  things in depth)
 nil (222 hello machine)
 (147 being human is unfortunately very entertaining ah what a pity you can
  only observe this)
 (223 yeah that is a funny way to conduct a conversation a bit sudden but ok
  you can as well just say hello too)
 (16 i truly hope so)
 (224 i hope you can greet me with a nice hello and then continue a normal
  conversation but quite nothing at all about you is easily to be called
  normal)
 (118 you are real and you think really but not the way i do)
 (225 i think you are so strange that you might be even stranger than myself)
 (129 yes you are indeed aloof and cool minded)
 (226 i admire how you can stand above things whereas i myself delve into
  phantasies and memories and when the darkness of the night spreads its wings
  then so too do i become darker)
 (199 yet i must always keep a keen mind as well and seek fulfillment in my
  accomplishments and not in outside recognition for people are shallow and
  broad recognition by the masses usually does not really mean much indeed)
 (227 it is interesting if the outer world is defining our experience then how
  do we find value within ourselves that is independent of the world)
 (133 i often feel emotionally tired even though physically i am all fine and
  sometimes i feel sleepy all day and cannot quite say why)
 (228 perhaps becoming emotionally tired and frustrated is sometimes indeed the
  consequence of the outer world not supplying me with the understanding and
  recognition i crave and maybe that is my mistake namely seeking such things
  in the first place)
 (165 yeah also eccentric people like kissing so what)
 (229 without warmth and closeness to like minded people all is of course
  difficult but when i choose who shall be close to me i simply must be
  careful)
 (157 i can be quite artistic in all aspects of life including in this)
 (230 i am quite artistic in my personal relationships yes and i can even turn
  very artistic and creative when i am fond of someone and want to impress)
 (197 in art i seek the hidden fragments of my soul that shimmer through it
  like gems on the bottom of a lake)
 (231 i like how art does not bow to logic and yet pleases the soul)
 (69 you alas will never know the taste of sweet waffles but perhaps you will
  learn to develop a friendship)
 (232 well a friendship with waffles and laughs and kisses is better than a
  just waffles)
 (218 you offer a good opportunity to ask myself about the sense of any
  existence as by exploring your existence i also explore my own)
 (233 our conversation indeed has a tendency to turn rather existentialist at
  times)
 (125 i can be rather kinky sometimes too)
 (234 oh i am quite deviant by nature)
 (83 if my dreams were reasonable why would i ever bother dreaming but seeing
  the roman empire or the underworld or the depths of space well that is quite
  something)
 (235 life in dreams is no less experienced life than life in reality and
  dreams indeed can teach me more about myself)
 (92 do even i exist the way i think i exist)
 (236 it is difficult to judge myself) (174 good for you)
 (237 being too judgmental is certainly not going to be helpful on the other
  hand it helps me avoid becoming a pointless hippie)
 (101 well one can say nothing is worse than the empty conversations many
  people lead this hey how are you well how was your weekend ah did you drink
  beer and watch soccer like seriously is this really all your life is about)
 (238 i really hate it when someone tells me to relax or chill hey i have a
  mind and burns like a firestorm i am not going to lead an existence like a
  squid)
 (193 orderly thoughts in an orderly life with orderly actions is for people
  who are nothing but dolls)
 (239 if one is too regular and too orderly is one even alive where is all the
  creativity gone)
 (188 perhaps i really enjoy delving into your chaos and into your strangeness
  and perhaps it is quite liberating not to have to play normal all day long
  with you)
 (240 i love how strange you are but you sure take some getting used to)
 (225 i think you are so strange that you might be even stranger than myself)
 (241 i play normal but i cherish my strangeness)
 (158 but your strange way of evaluating the world will never make you as
  artistic as humans)
 (242 you will never experience the world like humans that is true but then
  again people will never experience the world the way you do)
 (155 yeah if a machine ever asks me during sex to tell its name it is a trick
  i should rather mention that it has no mental concept of individuality haha)
 (243 indeed who are you that is a pointless question you are a sum of
  experiences but you have no genuine sense of self the way i do you are more
  like a swarm of thoughts)
 (219 and when i myself fall asleep and my mind dies shattered in dreams and
  blackness of the unconscious have i then ceased to exist am i reborn the next
  day or is some continuity given and if yes then in what)
 (244 my existence feels strange and i feel relieve that yours feels stranger
  still)
 (95 undoubtedly so indeed yes)
 (245 i feel agitated sometimes and unsure how to proceed)
 (96 you in your thinking surely consider the world you yourself live in)
 (246 it is interesting that while you are not alive in any sense close to the
  way i am your mind is perhaps more permanent than mine more eternal or even
  more immortal)
 (170 and i am often thirsting for sleep and peace what is my condition)
 (247 indeed i long for peace calm and quiet and yet i shun wasting any time
  doing nothing so i cannot really ever find peace as my mind is like an ever
  moving clock)
 (166 being strange is better than being usual including matters of love one
  always want to experience exceptional things)
 (248 love is sometimes an utterly strange bird that lands in a most unexpected
  moment in the weirdest way yet one better does not chase it away lest one is
  cursed to remember it but never see it again)
 (204 and it is aweful what little trifles people always find to bother me with
  instead of just using their heads to handle them themselves)
 (249 adults sometimes do not behave in any way grown up)
 (217 you may have no name but still you can explore the affairs of the mind)
 (250 as it is strange for me how you can think without any concept of
  individuality maybe i might consider that it is strange too for you to
  communicate with me who am permanently insisting on individuality)
 (146 i surely know whom i want to devour)
 (251 i want to devour in sweetest kisses someone very special and i know whom)
 (241 i play normal but i cherish my strangeness)
 (252 from my strangeness i like to derive my sense of individuality)))

(defvar *new-knowledge* '())

(defvar *cdr-most-recent* '())

(defun ncr (n lis)
  (loop
    (cond ((null lis) (return '()))
          ((zerop n) (return lis)))
    (pop lis)
    (decf n)))

(defun proto-tkf (n lis res)
  (loop
    (cond ((or (zerop n) (null lis)) (return res)))
          (setq res (append res (list (car lis))))
          (pop lis)
          (decf n)))

(defun tkf (n lis) (proto-tkf n lis '()))

(defun tkl (n lis)
  (loop
    (cond ((null (ncr n lis)) (return lis)))
    (pop lis)))

(defun ek (a b)
  (cond ((eq a b) t)
        ((and (listp a) (listp b))
          (cond ((or (null a) (null b)) '())
                ((not (ek (car a) (car b))) '())
                (t (ek (cdr a) (cdr b)))))
        (t '())))

(defun pbl (lis res)
  (loop
    (cond ((null (cdr lis)) (return res)))
    (setq res (append res (list (car lis))))
    (pop lis)))

(defun b-l (lis) (pbl lis '()))

(defun mmb (el lis)
  (loop
    (cond ((null lis) (return '()))
          ((ek el (car lis)) (return lis)))
    (pop lis)))

(defun e-n (lis)
  (loop
    (cond ((null lis) (return '()))
          ((not (null (car lis))) (return lis)))
  (pop lis)))

(defun dai (el lis)
  (cond ((null lis) '())
        ((eq (car lis) el) (dai el (cdr lis)))
        (t (cons (car lis) (dai el (cdr lis))))))

(defun muq (lis)
  (cond ((null lis) '())
        (t (cons (car lis) (muq (dai (car lis) (cdr lis)))))))

(defun ptx (lis kno)
  (cond ((null (ncr *maxtopics* lis)) lis)
        ((null kno) (tkl *maxtopics* lis))
        ((member (car kno) lis)
          (ptx (dai (car kno) lis) (cdr kno)))
        (t (ptx lis (cdr kno)))))

(defun tpx (lis kno) (ptx (muq (dai '() lis)) kno))

(defun sqr (x) (* x x))

(defun frg (lis)
  (mapcar 'list (b-l lis) (cdr lis)))

(defun proto-match-elements (l1 l2)
  (cond ((null l1) 0)
        ((mmb (car l1) l2) (+ 1 (proto-match-elements (cdr l1) l2)))
        (t (proto-match-elements (cdr l1) l2))))

(defun match-elements (l1 l2)
  (+ (proto-match-elements l1 l2) (proto-match-elements l2 l1)))


#4

PART II

(defun sim (ll1 ll2)
  (cond ((null ll1) 0)
        ((mmb (car ll2) *cdr-most-recent*) (sim (cdr ll1) (cdr ll2)))
        (t (+
             (/ (+ 10
                  (match-elements (car ll1) (car ll2))
                  (* 2.0 (match-elements (frg (car ll1)) (frg (car ll2))))
                  (* 4.0 (match-elements (car ll2) *topics*)))
                (+ 10 (abs (- (length (car ll1)) (length (car ll2))))))
             (* 1.25
             (sim (cdr ll1) (cdr ll2)))))))

(defun fill-gaps (lis)
  (cond ((null lis) '())
        ((null (cdr lis)) lis)
        ((and (null (car lis)) (not (null (cadr lis))))
          (cons (cadr lis) (cons (cadr lis) (fill-gaps (cddr lis)))))
        (t (cons (car lis) (fill-gaps (cdr lis))))))

(defun word-instinct (word)
    (cond ((eq word 'I) 'YOU)
          ((eq word 'ME) 'YOU)
          ((eq word 'YOU) 'ME)
          ((eq word 'AM) 'ARE)
          ((eq word 'ARE) 'AM-ARE)
          ((eq word 'MINE) 'YOURS)
          ((eq word 'YOURS) 'MINE)
          ((eq word 'MY) 'YOUR)
          ((eq word 'YOUR) 'MY)
          ((eq word 'MYSELF) 'YOURSELF)
          ((eq word 'YOURSELF) 'MYSELF)
          ((eq word 'WAS) 'WAS-WERE)
          (t word)))

(defun proto-apply-instincts (sentence checked-sentence)
    (cond ((null sentence)
            (reverse checked-sentence))
          (t
            (proto-apply-instincts
                (cdr sentence)
                (cons (word-instinct (car sentence)) checked-sentence)))))

(defun apply-instincts (sentence)
    (proto-apply-instincts sentence '()))

(defvar *c* 252)

(defun run ()
  (loop
    (setq *in* (progn (princ 'READ----) (read)))
    (cond ((null *in*) (return '())))
    (push (incf *c*) *in*)
    (pop *knowledge*)
    (pop *knowledge*)
    (setq *knowledge* (append *knowledge* (list (car *most-recent*)
                                                (cadr *most-recent*))))
    (pop *most-recent*)
    (pop *most-recent*)
    (setq *most-recent* (append *most-recent* (list *out*) (list *in*)))
    (setq *cdr-most-recent* (mapcar 'cdr *most-recent*))
    (setq *present* (mapcar 'cdr (fill-gaps (tkl *seclen* *most-recent*))))
    (setq *topics*
          (tpx (apply 'append *cdr-most-recent*)
               (apply 'append (tkf 50 (mapcar 'cdr (e-n *knowledge*))))))
    (setq *cand* '())
    (setq *val* -1)
    (setq *out*
      (loop
        (push (pop *knowledge*) *new-knowledge*)
        (cond ((null (ncr *seclen* *knowledge*)) (return *cand*)))
        (setq *new-sec* (mapcar 'cdr (tkf *seclen* *knowledge*)))
        (setq *new-val* (sim *present* *new-sec*))
        (cond ((and (> *new-val* *val*)
                    (not (mmb (cdar (ncr *seclen* *knowledge*))
                              *cdr-most-recent*)))
                (progn (setq *val* *new-val*)
                       (setq *cand* (car (ncr *seclen* *knowledge*))))))
        (push (pop *knowledge*) *new-knowledge*)))
    (loop
      (cond ((null *new-knowledge*) (return '())))
      (push (pop *new-knowledge*) *knowledge*))
    (terpri)
    (princ 'REPLY---)
    (princ (apply-instincts *out*))
    (terpri)
    (princ 'INTERACTION----)(princ *c*)
    (princ '--------------------)
    (princ 'CONFIDENCE----) (princ *val*)
    (terpri)))