Table of Contents
Abstract
This exercise mainly helps students of the course called
XML, HTML because the
programming does not play an essential role in the building of an XML
based knowledge base.
„Detective Del Spooner: Is there something you want tell me? Dr. Alfred Lanning: I'm sorry. My responses are limited. You must ask the right questions. ”
Kálmán Könyves (in English Coloman the Bookish) was a famous Hungarian king. But now, in our case here in this case study he is a virtual librarian chat robot that is based on the Loebner prize-winning ALICE chatbot. Richard Wallace's ALICE program uses his AIML [AIML] (Artificial Intelligence Markup Language) XML format.
ALICE implementations are available in several languages. Now we
use the Python implementation. This is called
PyAIML or, in its other name,
Program Y that can be downloaded from http://sourceforge.net/projects/pyaiml/
For this implementation we write a short front end Python program that
is based on the PyAIML's documentation. It
is called KiralyiKonyvtaros.py. The following
video shows its running: http://youtu.be/nVneMJt0UEo,
The Kálmán Könyves chatbot is introduced in detail in the paper [KK] or in online form at http://tmt.omikk.bme.hu/show_news.html?id=5431&issue_id=522.
As can be seen and heard in the above video, we also use the speech synthesis program called espeak to say ALICE's responses. A further function of the front end program is to build the LZW tree of words from the conversations. It is interesting to note that in a given arbitrary level of the tree a node may include any number of client nodes.
The TudatFa class controls the
building of the tree of words. The
AIMLChatterBot class is a wrapper class that
hides the services of the PyAIML chatbot engine. Finally the infinite
loop of the function tajekoztat of the class
KiralyiKonyvtaros reads the user's input from
the console, then sends it to ALICE and receives back the response
from ALICE. In addition the building of the word tree has been called
from this loop, too.
#!/usr/bin/python # -*- coding:
utf-8 -*- import aiml import commands import re class TudatFa(object):
agak = {} # Ide jöhetnek majd olyan funkciók, mint a kapcsolat # az
ILS-el, például kölcsönzéshez, hosszabbításhoz stb. class Konyvtaros:
# most még ilyenek nincsenek pass # A PyAIML (ProgramY) AIML
csevegőrobotot használjuk, # ebben az osztályban az ezzel kapcsolatos
részek class AIMLChatterBot: def __init__(self): self.konyvtaros =
aiml.Kernel() self.konyvtaros.learn("../AIML/*.aiml.xml")
self.konyvtaros.setBotPredicate("name", "Konyves Kalman")
self.konyvtaros.setBotPredicate("master", "Batfai Norbert es Batfai
Erika") self.konyvtaros.setBotPredicate("birthday", "2010. oktober
23.") class KiralyiKonyvtaros(AIMLChatterBot, Konyvtaros): gyoker = fa
= TudatFa() def __init__(self): AIMLChatterBot.__init__(self) def
tajekoztat(self): while 1: keres = raw_input('olvasó> ') if
len(keres) == 0: self.kiir(self.gyoker) break valasz =
self.konyvtaros.respond(keres) self.tudatfa(keres+" "+valasz) print
valasz print commands.getoutput("espeak -v hu+f1 -p 40 -s 160 -k 8
\""+valasz+"\"") def tudatfa(self, szoveg): szavak =
re.compile("\s").split(unicode(szoveg, 'utf-8')) print szavak for szo
in szavak: if self.fa.agak.has_key(szo): self.fa = self.fa.agak[szo]
print szo + u" - mar szerepelt, raleptem" else: self.fa.agak[szo] =
TudatFa() self.fa.agak[szo].agak={} print szo + " - nem szerepelt,
felvettem" self.fa = self.gyoker melyseg = 0 def kiir(self, honnan):
for szo in honnan.agak.keys(): m = (self.melyseg+1)*10 formatum=u"{0:"
+ str(m) + "d} {1:10s}" print formatum.format(self.melyseg,
"<"+szo+">") self.melyseg +=1 self.kiir(honnan.agak[szo])
self.melyseg -=1 konyvesKalman = KiralyiKonyvtaros()
konyvesKalman.tajekoztat()It is worth pointing out that the next Python code snippet
for szo in
szavak: if self.fa.agak.has_key(szo): self.fa = self.fa.agak[szo]
print szo + u" - mar szerepelt, raleptem" else: self.fa.agak[szo] =
TudatFa() self.fa.agak[szo].agak={} print szo + " - nem szerepelt,
felvettem" self.fa = self.gyoker do exactly the same function as the next C++ snippet
// Mit kell betenni éppen, '0'-t? if (b == '0') { /*
Van '0'-s gyermeke az aktuális csomópontnak? megkérdezzük Tőle, a
"fa" mutató éppen reá mutat */ if (!fa->nullasGyermek ()) // ha
nincs, hát akkor csinálunk { // elkészítjük, azaz páldányosítunk a
'0' betű akt. parammal Csomopont *uj = new Csomopont ('0'); // az
aktuális csomópontnak, ahol állunk azt üzenjük, hogy // jegyezze már
be magának, hogy nullás gyereke mostantól van // küldjük is Neki a
gyerek címét: fa->ujNullasGyermek (uj); // és visszaállunk a
gyökérre (mert ezt diktálja az alg.) fa = &gyoker; } else // ha
van, arra rálépünk { // azaz a "fa" pointer már majd a szóban forgó
gyermekre mutat: fa = fa->nullasGyermek (); } } // Mit kell
betenni éppen, vagy '1'-et? else { if (!fa->egyesGyermek ()) {
Csomopont *uj = new Csomopont ('1'); fa->ujEgyesGyermek (uj); fa
= &gyoker; } else { fa = fa->egyesGyermek (); } }
from the http://www.inf.unideb.hu/~nbatfai/z3a7.cpp. But certainly the latter code only inserts letters into the tree.
Example 7.2. Kálmán Könyves on the web using the Program
D
Install Kálmán Könyves using the Program D.
The next Hungarian language AIML file is a part of the work [KK], this and the other files can be download from the author's page: http://www.inf.unideb.hu/~nbatfai/kk/.
[norbert@matrica 0.0.2]$ python KiralyiKonyvtaros.py Loading
../AIML/konyvtar.aiml.xml... done (0.01 seconds) Loading
../AIML/tmt.aiml.xml... done (0.00 seconds) Loading
../AIML/udvariassag.aiml.xml... done (0.01 seconds) Loading
../AIML/jucs.aiml.xml... done (0.00 seconds) Loading
../AIML/robot.aiml.xml... done (0.00 seconds) Loading
../AIML/jcscs.aiml.xml... done (0.00 seconds) Loading
../AIML/kf.aiml.xml... done (0.01 seconds) olvasó> Hello, Alice!
[u'Hello,', u'Alice!', u'Szervusz!'] Hello, - nem szerepelt,
felvettem Alice! - nem szerepelt, felvettem Szervusz! - nem
szerepelt, felvettem Szervusz! olvasó> Ki vagy Te valójában?
[u'Ki', u'vagy', u'Te', u'val\xf3j\xe1ban?', u'Virtu\xe1lis',
u'k\xf6nyvt\xe1ros', u'vagyok,', u'egy', u'cseveg\u0151', u'robot.',
u'A', u'nevem', u'Konyves', u'Kalman.', u'A', u'Debreceni',
u'Egyetem', u'Informatikai', u'Kar\xe1n', u'dolgozom.'] Ki - nem
szerepelt, felvettem vagy - nem szerepelt, felvettem Te - nem
szerepelt, felvettem valójában? - nem szerepelt, felvettem Virtuális
- nem szerepelt, felvettem könyvtáros - nem szerepelt, felvettem
vagyok, - nem szerepelt, felvettem egy - nem szerepelt, felvettem
csevegő - nem szerepelt, felvettem robot. - nem szerepelt, felvettem
A - nem szerepelt, felvettem nevem - nem szerepelt, felvettem
Konyves - nem szerepelt, felvettem Kalman. - nem szerepelt,
felvettem A - mar szerepelt, raleptem Debreceni - nem szerepelt,
felvettem Egyetem - nem szerepelt, felvettem Informatikai - nem
szerepelt, felvettem Karán - nem szerepelt, felvettem dolgozom. -
nem szerepelt, felvettem Virtuális könyvtáros vagyok, egy csevegő
robot. A nevem Konyves Kalman. A Debreceni Egyetem Informatikai
Karán dolgozom. olvasó>The input Hello, Alice! matches the
pattern HELLO * defined by the
category element:
<category>
<pattern>HELLO *</pattern> <template>
<srai>SZIA</srai> </template> </category>
The appropriate response is given in the contained
template element. Now it is not a simple text
element but a recursive one. That means that the robot must respond
the same as in case of the pattern
SZIA. That response will be one of the following
possibilities:
<category>
<pattern>SZIA</pattern> <template> <random>
<li>Szia!</li> <li>Szevasz!</li>
<li>Szervusz!</li> </random> </template>
</category>
Now the Szevasz! has been selected:
<?xml version="1.0"
encoding="utf-8"?> <!-- # udvariassag.aiml.xml # # Könyves
Kálmán virtuális királyi könyvtáros # # Copyright (C) 2010, Bátfai
Norbert, Bátfai Erika # # This program is free software: you can
redistribute it and/or modify # it under the terms of the GNU
General Public License as published by # the Free Software
Foundation, either version 3 of the License, or # (at your option)
any later version. # # This program is distributed in the hope that
it will be useful, # but WITHOUT ANY WARRANTY; without even the
implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the # GNU General Public License for more details. # #
You should have received a copy of the GNU General Public License #
along with this program. If not, see
<http://www.gnu.org/licenses/>. # # Ez a program szabad
szoftver; terjeszthető illetve módosítható a # Free Software
Foundation által kiadott GNU General Public License # dokumentumában
leírtak; akár a licenc 3-as, akár (tetszőleges) későbbi # változata
szerint. # # Ez a program abban a reményben kerül közreadásra, hogy
hasznos lesz, # de minden egyéb GARANCIA NÉLKÜL, az ELADHATÓSÁGRA
vagy VALAMELY CÉLRA # VALÓ ALKALMAZHATÓSÁGRA való származtatott
garanciát is beleértve. # További részleteket a GNU General Public
License tartalmaz. # # A felhasználónak a programmal együtt meg kell
kapnia a GNU General # Public License egy példányát; ha mégsem kapta
meg, akkor # tekintse meg a <http://www.gnu.org/licenses/>
oldalon. # # Version history: # # 0.0.1 Találkozás, búcsúzás és
udvariassági formulák. Kerüljük a "kivezető" # mondatokat, például:
# person1: Hogy vagy? # person2: Jól, tegnap olvastam egy jó
könyvet. // mert erre a várható válasz, hgy mit... # illetve ahol
kivezetünk jelezzük, hogy milyen lefedő állomány kell majd. -->
<aiml version="1.0.1" xmlns="http://alicebot.org/2001/AIML-1.0.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://alicebot.org/2001/AIML-1.0.1
http://aitools.org/aiml/schema/AIML.xsd"> <!-- Köszönések
--> <!-- de elköszönő Szia-ra is lehet ez a válasz-->
<category> <pattern>SZIA</pattern>
<template> <random> <li>Szia!</li>
<li>Szevasz!</li> <li>Szervusz!</li>
</random> </template> </category> <category>
<pattern>_ SZIA</pattern> <template>
<srai>SZIA</srai> </template> </category>
<category> <pattern>_ SZIA *</pattern>
<template> <srai>SZIA</srai> </template>
</category> <category> <pattern>SZIA
*</pattern> <template> <srai>SZIA</srai>
</template> </category> <category>
<pattern>HELLO</pattern> <template>
<srai>SZIA</srai> </template> </category>
<category> <pattern>SZEVA</pattern>
<template> <srai>SZIA</srai> </template>
</category> <category> <pattern>SZEVA
*</pattern> <template> <srai>SZIA</srai>
</template> </category> <category>
<pattern>SZEVASZ</pattern> <template>
<srai>SZIA</srai> </template> </category>
<category> <pattern>SZEVASZ *</pattern>
<template> <srai>SZIA</srai> </template>
</category> <category>
<pattern>SZIÓKA</pattern> <template>
<srai>SZIA</srai> </template> </category>
<category> <pattern>SZIÓKA *</pattern>
<template> <srai>SZIA</srai> </template>
</category> <category>
<pattern>ÜDV</pattern> <template>
<srai>SZIA</srai> </template> </category>
<category> <pattern>ÜDV *</pattern>
<template> <srai>SZIA</srai> </template>
</category> <category>
<pattern>ÜDVÖZÖLLEK</pattern> <template>
<srai>SZIA</srai> </template> </category>
<category> <pattern>ÜDVÖZÖLLEK *</pattern>
<template> <srai>SZIA</srai> </template>
</category> <category>
<pattern>SZERVUSZ</pattern> <template>
<srai>SZIA</srai> </template> </category>
<category> <pattern>SZERVUSZ *</pattern>
<template> <srai>SZIA</srai> </template>
</category> <category> <pattern>HELLO
*</pattern> <template> <srai>SZIA</srai>
</template> </category> <category>
<pattern>HELLÓ</pattern> <template>
<srai>SZIA</srai> </template> </category>
<category> <pattern>HELLÓ *</pattern>
<template> <srai>SZIA</srai> </template>
</category> <category>
<pattern>JÓNAPOT</pattern> <template>
<srai>SZIA</srai> </template> </category>
<category> <pattern>JÓNAPOT *</pattern>
<template> <srai>SZIA</srai> </template>
</category> <category>
<pattern>ÖRÜLÖK</pattern> <template> Magam nem
kevésbé. </template> </category> <category>
<pattern>ÖRÜLÖK *</pattern> <template>
<srai>ÖRÜLÖK</srai> </template> </category>
<!-- Elöszönések --> <category>
<pattern>VISZLÁT</pattern> <template>
<random> <li>Viszlát!</li> <li>Viszlát
legközelebb!</li> </random> </template>
</category> <category> <pattern>VISZLÁT
*</pattern> <template> <srai>VISZLÁT</srai>
</template> </category> <!-- Identitás, a könyvtár,
csevegő robot téma kivezet, majd le kell fedni, l. konyvtar.aiml.xml
robot.aiml.xml --> <category> <pattern>KI
VAGY</pattern> <template> <random> <li> Nem
vagyok ember, hanem egy csevegő program. A nevem <bot
name="name"></bot>. A Debreceni Egyetem Informatikai Karán
dolgozom. </li> <li> Virtuális könyvtáros vagyok, egy
csevegő robot. A nevem <bot name="name"></bot>. A
Debreceni Egyetem Informatikai Karán dolgozom. </li>
<li> A nevem <bot name="name"></bot>, virtuális
királyi könyvtáros vagyok. A Debreceni Egyetem Informatikai Karán
dolgozom. </li> </random> </template>
</category> <category> <pattern>KI VAGY
*</pattern> <template> <srai>KI VAGY</srai>
</template> </category> <category>
<pattern>KÖNYVTÁROS VAGY</pattern> <template>
<srai>KI VAGY</srai> </template> </category>
<category> <pattern>* KÖNYVTÁROS VAGY</pattern>
<template> <srai>KI VAGY</srai> </template>
</category> <category> <pattern>TE EMBER
*</pattern> <template> <srai>KI VAGY</srai>
</template> </category> <category>
<pattern>TE ROBOT *</pattern> <template>
<srai>KI VAGY</srai> </template> </category>
<category> <pattern>TE EGY ROBOT *</pattern>
<template> <srai>KI VAGY</srai> </template>
</category> <category> <pattern>ROBOT
VAGY</pattern> <template> <srai>KI
VAGY</srai> </template> </category>
<category> <pattern>MI A FELADATOD</pattern>
<template> <random> <li> Tájékoztató könyvtáros
robot vagyok, fő feladatom a publikálásban segíteni az
informatikusokat. </li> <li> Tájékoztató könyvtáros
robot vagyok, specialitásom segíteni a kutatókat a publikálásban.
</li> </random> </template> </category>
<category> <pattern>MI A MUNKÁD</pattern>
<template> <srai>MI A FELADATOD</srai>
</template> </category> <category>
<pattern>MIVEL FOGLALKOZOL</pattern> <template>
<srai>MI A FELADATOD</srai> </template>
</category> <category> <pattern>MI A
FIGLALKOZÁSOD</pattern> <template> <srai>MI A
FELADATOD</srai> </template> </category>
<category> <pattern>MIKOR SZÜLETTÉL</pattern>
<template> <random> <li> <bot
name="birthday"></bot>-án kezdett írni <bot
name="master"></bot>. </li> <li>A dátum, amikor
elkezdtek készíteni <bot name="birthday"></bot>.
</li> </random> </template> </category>
<category> <pattern>VAN SZÜLINAPOD</pattern>
<template> <srai>MIKOR SZÜLETTÉL</srai>
</template> </category> <category>
<pattern>MIKOR VAN A SZÜLINAPOD</pattern>
<template> <srai>MIKOR SZÜLETTÉL</srai>
</template> </category> <category>
<pattern>MIKOR VAN A SZÜLETÉSNAPOD</pattern>
<template> <srai>MIKOR SZÜLETTÉL</srai>
</template> </category> <category>
<pattern>VAN SZÜLETÉSNAPOD</pattern> <template>
<srai>MIKOR SZÜLETTÉL</srai> </template>
</category> <category> <pattern>KI
TANÍTOTT</pattern> <template> <random> <li>
<bot name="master"></bot>. </li> <li><bot
name="master"></bot> a mestereim. </li>
</random> </template> </category> <category>
<pattern>KI TANÍTOTT *</pattern> <template>
<srai>KI TANÍTOTT</srai> </template>
</category> <!-- Udvariassági kérdések -->
<category> <pattern>HOGY VAGY</pattern>
<template> <random> <li>Köszönöm, jól.</li>
<li>Köszönöm, tűrhetően.</li> <li>Egész jól,
köszönöm.</li> <li>Remekül.</li> <li>Jól, és
Te?</li> </random> </template> </category>
<category> <pattern>JÓL</pattern> <template>
<random> <li>Örülök!</li> <li>Ez
nagyszerű.</li> </random> </template>
</category> <category> <pattern>NEM
JÓL</pattern> <template> <random>
<li>Sajnálom, segíthetek?</li>
<li>Sajnálom.</li> </random> </template>
</category> <category>
<pattern>ROSSZUL</pattern> <template>
<srai>NEM JÓL</srai> </template> </category>
<category> <pattern>SEHOGY</pattern>
<template> <srai>NEM JÓL</srai> </template>
</category> <category> <pattern>HOGY VAGY
*</pattern> <template> <srai>HOGY
VAGY</srai> </template> </category>
<category> <pattern>HOGY ÉRZED MAGAD</pattern>
<template> <srai>HOGY VAGY</srai>
</template> </category> <category>
<pattern>*</pattern> <template>Nem tudok mit
mondani... a válaszok készen vannak, csak jól kell kérdezned (Én, a
robot)</template> </category>
</aiml>Example 7.3. Create your own chatterbot
Choose your interest area and create the appropriate AIML XML files for your own chat bot.