chatbot tutorial - codeproject
TRANSCRIPT
-
12/31/13 Chatbot Tutorial - CodeProject
www.codeproject.com/Articles/36106/Chatbot-Tutorial?display=Print 1/15
ArticlesGeneralProgrammingAlgorithms&RecipesGeneral
ChatbotTutorialByGonzalesCenelia,29May2013
OverviewAstepbystepguidetoimplementyourownArtificialIntelligencechatbot.
Tableofcontents1. IntroductionChatbotdescription(firstexample)2. Introducingkeywordsandstimulusresponse3. Preprocessingtheuser'sinputandrepetitioncontrol4. Amoreflexiblewayformatchingtheinputs5. Usingclassesforabetterimplementation6. Controllingrepetitionmadebytheuser7. Using"states"torepresentdifferentevents8. Keywordboundariesconcept9. UsingSignonmessages
10. "KeywordRanking"concept11. Keywordequivalenceconcept12. Transpositionandtemplateresponse13. Keywordlocationconcept14. Handlingcontext15. UsingTextToSpeech16. Usingaflatfiletostorethedatabase17. Abetterrepetitionhandlingalgorithm18. Updatingthedatabasewithnewkeywords19. SavingtheconversationLogs20. Learningcapability
IntroductionBasicallyachatterbotisacomputerprogramthatwhenyouprovideitwithsomeinputsinNaturalLanguage(English,French...)respondswithsomethingmeaningfulinthatsamelanguage.WhichmeansthatthestrengthofachatterbotcouldbedirectlymeasuredbythequalityoftheoutputselectedbytheBotinresponsetotheuser.Bythepreviousdescription,wecoulddeducethataverybasicchatterbotcanbewritteninafewlinesofcodeinagivenspecificprogramminglanguage.Letsmake
4.67(44votes)
-
12/31/13 Chatbot Tutorial - CodeProject
www.codeproject.com/Articles/36106/Chatbot-Tutorial?display=Print 2/15
ourfirstchatterbot(noticethatallthecodesthatwillbeusedinthistutorialwillbewritteninC++.Also,itisassumedthatthereaderisfamiliarwiththeSTLlibrary)Thistutorialisalsoavailableinthefollowinglanguages:Java,VisualBasic,C#,Pascal,PrologandLisp
////ProgramName:chatterbot1//Description:thisisaverybasicexampleofachatterbotprogram////Author:GonzalesCenelia//
#include#include#include
intmain(){std::stringResponse[]={"IHEARDYOU!","SO,YOUARETALKINGTOME.","CONTINUE,IMLISTENING.","VERYINTERESTINGCONVERSATION.","TELLMEMORE..."}
srand((unsigned)time(NULL))
std::stringsInput=""std::stringsResponse=""
while(1){std::cout
-
12/31/13 Chatbot Tutorial - CodeProject
www.codeproject.com/Articles/36106/Chatbot-Tutorial?display=Print 3/15
averageprogrammingskilltobeachatbotprogrammer).So,programmersouttherewhowantedtocreatetrueAIorsomekindofartificialintelligence,writingintelligentchatbotsisagreatplacetostart!
Now,let'sgetbacktoourpreviousprogram,whataretheproblemswithit?Well,thereisalotofthem.Firstofall,wecanclearlyseethattheprogramisn'treallytryingtounderstandwhattheuserissayingbutinsteadheisjustselectingarandomresponsefromhisdatabaseeachtimetheusertypesomesentenceonthekeyboard.Andalso,wecouldnoticethattheprogramrepeathimselfveryoften.Oneofthereasonforthisisbecauseofthesizeofthedatabasewhichisverysmall(5sentences).Thesecondthingthatwouldexplaintherepetitionsisthatwehaven'timplementedanymechanismthatwouldcontrolthisunwantedbehavior.
Howdowemovefromaprogramthatjustselectresponsesrandomlytowhateverinputthattheusermightenteronthekeyboardtoaprogramthatshowssomemoreunderstandingoftheinputs?
Theanswertothatquestionisquietsimplewesimplyneedtousekeywords.
Akeywordisjustasentence(notnecessarilyacompleteone)orevenawordthattheprogrammightrecognizefromtheuser'sinputwhichthenmakesitpossiblefortheprogramtoreacttoit(ex:byprintingasentenceonthescreen).Forthenextprogram,wewillwriteaknowledgebaseordatabase,itwillbecomposedofkeywordsandsomeresponsesassociatedtoeachkeyword.
so,nowweknowwhattodotoimprove"ourfirstchatterbot"andmakeitmoreintelligent.Letsproceedonwriting"oursecondbot",wewillcallitchatterbot2.
////ProgramName:chatterbot2//Description:thisisanimprovedversion//ofthepreviouschatterbotprogram"chatterbot1"//thisonewilltryalittlebitmoretounderstandwhattheuseristryingtosay////Author:GonzalesCenelia//
#pragmawarning(disable:4786)
#include#include#include#include
constintMAX_RESP=3
typedefstd::vectorvstring
vstringfind_match(std::stringinput)voidcopy(char*array[],vstring&v)
typedefstruct{char*inputchar*responses[MAX_RESP]}record
recordKnowledgeBase[]={{"WHATISYOURNAME",
-
12/31/13 Chatbot Tutorial - CodeProject
www.codeproject.com/Articles/36106/Chatbot-Tutorial?display=Print 4/15
{"MYNAMEISCHATTERBOT2.","YOUCANCALLMECHATTERBOT2.","WHYDOYOUWANTTOKNOWMYNAME?"}},
{"HI",{"HITHERE!","HOWAREYOU?","HI!"}},{"HOWAREYOU",{"I'MDOINGFINE!","I'MDOINGWELLANDYOU?","WHYDOYOUWANTTOKNOWHOWAMIDOING?"}},
{"WHOAREYOU",{"I'MANA.IPROGRAM.","ITHINKTHATYOUKNOWWHOI'M.","WHYAREYOUASKING?"}},
{"AREYOUINTELLIGENT",{"YES,OFCORSE.","WHATDOYOUTHINK?","ACTUALY,I'MVERYINTELLIGENT!"}},
{"AREYOUREAL",{"DOESTHATQUESTIONREALLYMATERSTOYOU?","WHATDOYOUMEANBYTHAT?","I'MASREALASICANBE."}}}
size_tnKnowledgeBaseSize=sizeof(KnowledgeBase)/sizeof(KnowledgeBase[0])
intmain(){srand((unsigned)time(NULL))
std::stringsInput=""std::stringsResponse=""
while(1){std::cout
-
12/31/13 Chatbot Tutorial - CodeProject
www.codeproject.com/Articles/36106/Chatbot-Tutorial?display=Print 5/15
//insidethedatabaseoftheprogramvstringfind_match(std::stringinput){vstringresultfor(inti=0i
-
12/31/13 Chatbot Tutorial - CodeProject
www.codeproject.com/Articles/36106/Chatbot-Tutorial?display=Print 6/15
youaretryingtosaytohimandthisisbecauseitwasunabletofindamatchforthisinput.Andthisdefinitelywouldsoundalittlebitsurprisingconsideringthefactthattheprogramcanunderstandthesentence"whatisyourname".
Howdoweovercomethisproblem?Thereareatlisttwowaystosolvethisproblem,themostobviousoneistouseaslightlymoreflexiblewayformatchingkeywordsinthedatabaseagainsttheuser'sinput.Allwehavetodotomakethispossibleistosimplyaloudkeywordstobefoundwithintheinputssothatwewillnolongerhavethepreviouslimitation.
Theotherpossibilityismuchmorecomplex,ituse'stheconceptofFuzzyStringSearch.Toapplythismethod,itcouldbeusefulatfirsttobreaktheinputsandthecurrentkeywordinseparatewords,afterthatwecouldcreatetwodifferentvectors,thefirstonecouldbeusetostorethewordsfortheinputandtheotheronewouldstorethewordsforthecurrentkeyword.OncewehavedonethiswecouldusetheLevenshteindistanceformeasuringthedistancebetweenthetwowordvectors.(Noticethatinorderforthismethodtobeeffectivewewouldalsoneedanextrakeywordthatwouldrepresentthesubjectofthecurrentkeyword).
So,thereyouhaveit,twodifferentmethodsforimprovingthechatterbot.Actuallywecouldcombinebothmethodsandjustselectingwhichonetouseoneachsituation.
Finally,therearestillanotherproblemthatyoumayhavenoticedwiththepreviouschatterbot,youcouldrepeatthesamesentenceoverandoverandtheprogramwouldn'thaveanyreactiontothis.Weneedalsotocorrectthisproblem.
So,wearenowreadytowriteourfourthchatterbot,wewillsimplycallitchatterbot4.ViewthecodeforChatterbot4
Asyouprobablymayhaveseen,thecodefor"chatterbot4"isverysimilartotheonefor"chatterbot3"butalsotherewassomekeychangesinit.Inparticular,thefunctionforsearchingforkeywordsinsidethedatabaseisnowalittlebitmoreflexible.So,whatnext?Dontworrytherearestillalotofthingstobecovered.
Whatcanweimproveinchatterbot4tomakeitbetter?Herearesomeideas
sincethecodeforthechatterbotshavestartedtogrow,itwouldbeagoodthingtoencapsulatetheimplementationofthenextchatterbotbyusingaclass.alsothedatabaseisstillmuchtoosmalltobecapableofhandlingarealconversationwithusers,sowewillneedtoaddsomemoreentriesinit.itmayhappensometimesthattheuserwillpresstheenterkeywithoutenteringanythingonthekeyboard,weneedtohandlethissituationaswell.theusermightalsotrytotrickthechatterbotbyrepeatinghisprevioussentencewithsomeslightmodification,weneedtocountthisasarepetitionfromtheuser.andfinally,prettysoonyouwillalsonoticethatwemightneedawayforrankingkeywordswhenwehavemultiplechoicesofkeywordsforagiveninput,weneedawayforchoosingthebestoneamongthem.
Thatsaid,wewillnowstarttowritetheimplementationforchatterbot5.DownloadChatterbot5
Beforeproceedingtothenextpartofthistutorial,youareencouragedtotrycompilingandrunningthe
-
12/31/13 Chatbot Tutorial - CodeProject
www.codeproject.com/Articles/36106/Chatbot-Tutorial?display=Print 7/15
codefor"chatterbot5"sothatyoucanunderstandhowitworksandalsotoverifiesthechangesthathavebeenmadeinit.Hasyoumayhaveseen,theimplementationofthe"currentchatterbot",isnowencapsulatedintoaclass,also,therehasbeensomenewfunctionsaddedtothenewversionoftheprogram.
Wewillnowtrytodiscusstheimplementationof"chatterbot5"
select_response():thisfunctionselectsaresponsefromalistofresponses,thereisanewhelperfunctionthatwasaddedtotheprogramshuffle,thisnewfunctionshufflesalistofstringsrandomlyafterseed_random_generator()wascalled.save_prev_input():thisfunctionsimplysavesthecurrentuserinputintoavariable(m_sPrevInput)beforegettingsomenewinputsfromtheuser.voidsave_prev_response():thefunctionsave_prev_response()savesthecurrentresponseofthechatterbotbeforethebothavestartedtosearchresponsesforthecurrentinput,thecurrentresponsesissaveinthevaraible(m_sPrevResponse).voidsave_prev_event():thisfunctionsimplysavesthecurrentevent(m_sEvent)intothevariable(m_sPrevEvent).Aneventcanbewhentheprogramhasdetectedanullinputfromtheuseralso,whentheuserrepeatshimselforevenwhenthechatterbotmakesrepetitionshaswelletc.voidset_event(std::stringstr):setsthecurrentevent(m_sEvent)voidsave_input():makesabackupofthecurrentinput(m_sIntput)intothevariablem_sInputBackup.voidset_input(std::stringstr):setsthecurrentinput(m_sInput)voidrestore_input():restoresthevalueofthecurrentinput(m_sInput)thathasbeensavedpreviouslyintothevariablem_sInputBackup.voidprint_response():printstheresponsethathasbeenselectedbythechatrobotonthescreen.voidpreprocess_input():thisfunctiondoessomepreprocessingontheinputlikeremovingpunctuations,redundantspacescharactesandalsoitconvertstheinputtouppercase.boolbot_repeat():verifiesifthechatterbothasstartedtorepeathimself.booluser_repeat():Verifiesiftheuserhasrepeatedhisself.boolbot_understand():Verifiesthatthebotunderstandthecurrentuserinput(m_sInput).boolnull_input():Verifiesifthecurrentuserinput(m_sInput)isnull.boolnull_input_repetition():Verifiesiftheuserhasrepeatedsomenullinputs.booluser_want_to_quit():Checktoseeiftheuserwantstoquitthecurrentsessionwiththechatterbot.
-
12/31/13 Chatbot Tutorial - CodeProject
www.codeproject.com/Articles/36106/Chatbot-Tutorial?display=Print 8/15
boolsame_event():Verifiesifthecurrentevent(m_sEvent)isthesameasthepreviousone(m_sPrevEvent).boolno_response():Checkstoseeiftheprogramhasnoresponseforthecurrentinput.boolsame_input():Verifiesifthecurrentinput(m_sInput)isthesameasthepreviousone(m_sPrevInput).boolsimilar_input():Checkstoseeifthecurrentandpreviousinputaresimilar,twoinputsareconsideredsimilarifoneofthemisthesubstringoftheotherone(e.g.:howareyouandhowareyoudoingwouldbeconsideredsimilarbecausehowareyouisasubstringofhowareyoudoing.voidget_input():Getsinputsfromtheuser.voidrespond():handlesallresponsesofthechatrobotwhetheritisforeventsorsimplythecurrentuserinput.So,basically,thesefunctioncontrolsthebehaviouroftheprogram.find_match():Findsresponsesforthecurrentinput.voidhandle_repetition():Handlesrepetitionsmadebytheprogram.handle_user_repetition():Handlesrepetitionsmadebytheuser.voidhandle_event(std::stringstr):Thisfunctionhandleseventsingeneral.
Youcanclearlyseethat"chatterbot5"havemuchmorefunctionalitiesthan"chatterbot4"andalsoeachfunctionalitiesisencapsulatedintomethods(functions)oftheclassCBotbutstilltherearealotmoreimprovementstobemadeonittoo.
Chattebot5introducetheconceptof"state",inthesenewversionoftheChatterbot,weassociateadifferent"state"tosomeoftheeventsthatcanoccurduringaconversation.Ex:whentheuserentersanullinput,thechatterbotwouldsetitselfintothe"NULLINPUT**"state,whentheuserrepeatthesamesentence,itwouldgointothe"REPETITIONT1**"state,etc.
Alsothesenewchatterbotusesabiggerdatabasethanthepreviouschatbotthatwehaveseensofar:chatterbot1,chatterbot2,chatterbot3...Butstill,thisisquietinsignificantduetothefactthatmostchatterbotsinusetoday(theverypopularones)haveadatabaseofatleast10000linesormore.So,thiswoulddefinitelybeoneofthemajorgoalthatwemighttrytoachieveintothenextversionsofthechatterbot.
Buthoweverfornow,wewillconcentratealittleproblemconcerningthecurrentchatterbot.
Whatexactlywouldbethisproblem?Well,it'sallaboutkeywordboundaries,supposethatuserentersthesentence:"Ithinknot"duringaconversationwiththechatbot,naturallytheprogramwouldlookintohisdatabaseforakeywordthatwouldmatchthesentence,anditmightfoundthekeyword:"Hi",whichisalsoasubstringoftheword"think",clearlythisisanunwantedbehaviour.
Howdoweavoidit?Simplybyputtingaspacecharacterbeforeandafterthekeywordsthatcanbefoundinsidethedatabaseorwecansimplyapplythechangesduringthematchingprocessinsidethe"find_match()function".
-
12/31/13 Chatbot Tutorial - CodeProject
www.codeproject.com/Articles/36106/Chatbot-Tutorial?display=Print 9/15
Arethereotherthingsthatwecanimprovein"Chatterbot5"?Certainlythereis.SofartheChatbotstarta"chattingsession"withtheuserswithoutsayinganythingatthebeginningoftheconversations.Itwouldbegoodifthechatterbotcouldsayanythingatalltostartuptheconversations.Thiscaneasilybeachievedbyintroducing"signonmessages"intotheprogram.WecansimplydothisbycreatinganewstateinsidetheChatbot"knowledgebase"andbyaddingsomeappropriatemessagethatlinkstoit.Thatnewstatecouldbecall"SIGNON**".
DownloadChatterbot6
Introducingtheconceptof"KeywordRanking"Asyoucansee,oneachnewversionofthechatterbot,weareprogressivelyaddingnewfeaturesinordertomaketheChabotmorerealistic.Now,inthesesection,wearegoingtointroducetheconceptof'keywordranking'intotheChatterbot.Keywordrankingisawayfortheprogramtoselectthebestkeywordsinhisdatabasewhentherearemorethanonekeywordthatmatchtheusersinputs.Ex:ifwehavethecurrentuserinput:Whatisyournameagain,bylookingintohisdatabase,theChatbotwouldhavealistoftwokeywordsthatmatchthisinput:'WHAT'and'WHATISYOURNAME'.Whichoneisthebest?Well,theanswerisquietsimple,itisobviously:'Whatisyourname'simplybecauseitisthelongestkeyword.Thesenewfeaturehasbeenimplementedinthenewversionoftheprogram:Chatterbot7.
DownloadChatterbot7
EquivalentkeywordsWithinallthepreviousChatterbotstherecordforthedatabasealoudustouseonlyonekeywordforeachsetofresponsesbutsometimesitcouldbeUsefultohavemorethanonekeywordassociatedtoeachsetofresponses.Speciallywhenthesekeywordshavethesamemeaning.E.g.:WhatisyournameandCanyoupleasetellmeyournamehavebothhadthesamemeaning?Sotherewouldbenoneedtousedifferentrecordsforthesekeywordsinsteadwecanjustmodifytherecordstructuresothatitaloudustohavemorethanonekeywordperrecords.DownloadChatterbot8
KeywordtranspositionandtemplateresponseOneofthewellknownmechanismsofchatterbotsisthecapacitytoreformulatetheuser'sinputbydoingsomebasicverbconjugation.Example,iftheuserenters:YOUAREAMACHINE,thechatterbotmightrespond:So,youthinkthatI'mamachine.
Howdidwearriveatthistransformation?Wemayhavedoneitbyusingtwosteps:
Wemakesurethatthechatterbothavealistofresponsetemplatesthatislinkedtothecorrespondingkeywords.Responsestemplatesareasortofskeletontobuildnewresponsesforthechatterbot.usuallyweusedwildcardsintheresponsestoindicatethatitisatemplate.Onthepreviousexample,wehaveusedthetemplate:(so,youthinkthat*)toconstructourresponse.Duringthereassemblyprocess,wesimplyreplacethewildcardbysomepartoftheoriginalinput.Inthatsameexample,wehaveused:Youareamachine,whichisactuallythecompleteoriginalinputfromtheuser.Afterreplacingthewildcardbytheuser'sinput,wehavethefollowingsentence:So,youthinkthatyouareamachinebutwecannotusethesesentenceasitis,
-
12/31/13 Chatbot Tutorial - CodeProject
www.codeproject.com/Articles/36106/Chatbot-Tutorial?display=Print 10/15
beforethatweneedtomakesomepronounreversalinit.
Theusualtranspositionsthatweusemostlyarethereplacementofpronounofthefirstpersontopronounofthesecondperson,e.g.:you>me,I'm>youareetc.Inthepreviousexamplebyreplacing"YOUARE"by"I'M"intheusersinput,Afterapplyingthesechanges,theoriginalsentencebecomes:I'mamachine.NowwecanreplacethewildcardfromthetemplatebythesenewsentencewhichgiveusourfinalresponsefortheChatbot:So,youthinkthatI'mamachine.
Noticethatit'snotagoodthingtousetranspositiontoomuchduringaconversation,themechanismwouldbecometooobviousanditcouldcreatesomerepetition.
DownloadChatterbot9
KeywordlocationconceptSomekeywordscanbelocatedanywhereinagiveninput,someotherscanonlybefoundinonlysomespecificplacesintheuser'sinputotherwiseitwouldn'tmakeanysense.Akeywordlike:"Whoareyou"canbefoundanywhereontheuser'sinputwithoutcreatinganyproblemswiththemeaningofit.
Someexamplesofsentencesusing"WHOAREYOU"wouldbe:
1. Whoareyou?2. Bytheway,whoareyou?3. Sotellme,whoareyouexactly?
Butakeywordsuchas"whois"canonlybefoundatthebeginningorinthemiddleofagivensentencebutitcannotbefoundatendofthesentenceoralone.
Examplesofsentencesusingthekeyword:"whois":
1. Whoisyourfavoritesinger?2. Doyouknowwhoisthegreatestmathematicianofalltime?3. Tellme,doyouknowwhois?(thisclearlydoesn'tmakeanysense)
Howdowemakesurethatthechatterbotwillbeabletodistinguishsuchkeywordsandthespecificplacesweretheyarealoudtobefoundonasentence?Wewillsimplyintroducesomenewnotationsforkeywords:
1. Keywordsthatcanonlybefoundatthebeginningorinthemiddleofasentencewillberepresentedby:_KEYWORD(Ex:_WHOIS)
2. Keywordsthatcanonlybefoundatendorinthemiddleofasentencewillbedenotedby:KEYWORD_(WHATAREYOU_)
3. Keywordsthatshouldonlybefoundaloneinasentencewillberepresentedby:_KEYWORD_(Ex:_WHAT)
4. Andfinally,keywordsthatcanbefoundanywhereinasentenceorevenalonewouldbesimplyrepresentedby:KEYWORD(Ex:IUNDERSTAND)
Akeywordcanhavedifferentmeaningsdependingonit'spositioninagivensentence.
DownloadChatterbot10
HandlingContextContextaiswayfortheChatterbottokeepintractwhatithassaidpreviouslyandbeingabletotake
-
12/31/13 Chatbot Tutorial - CodeProject
www.codeproject.com/Articles/36106/Chatbot-Tutorial?display=Print 11/15
thisintoaccountwhenselectinghisnextresponse.Sofar,everyresponseselectedbytheChatbotduringaconversationischosenonlybasedonthecurrentuser'sinput.Butsometimes,wemightneedmoredatainordertobeabletorespondproperlytoagiveninput,that'swhenweneedtousecontext.
Toillustratetheseconcept,wearegoingtolookatthefollowingconversationlog:
USER:Whatisyourfavoritemovie?CHATTERBOT:ITISTERMINATORII.
USER:Whydoyoulikethismovie?(NowhowarewesupposedtoanswerthatquestionifweknewnothingaboutthepreviousresponseoftheChatbot?)
Soclearly,someinputsrequirestheusageof"context"inordertobeabletoformulateacorrectanswer.Inthepreviousexample,itwouldsimplybe:ITISTERMINATORII.NowtheBotknowswhatitwastalkingaboutpreviously,itcanmoreeasilyformulateagoodanswertotheuser'sinput.
Wecannowcontinuethepreviousconversationlog:
(Context:ITISTERMINATORII)CHATTERBOT:BECAUSEITISASCIENCEFICTIONMOVIEANDILOVESCIENCEFICTION.
ContextalsoaloudustocontrolimproperreactionfromtheChatbot.Example,iftheuserentersthesentence:"Whydoyoulikethesemovie?"duringaconversationwithouttheChatterboteventalkingaboutthesesubject.Itcouldsimplyrespondbysaying:WHATAREYOUTALKINGABOUT?
ThecontextfeaturehasbeenimplementedinChatterbot11.
DownloadChatterbot11
AnothergreatfeaturethatwouldbeveryinterestingtoimplementintoaChatterbotisthecapacitytoanticipatethenextresponseoftheuser,thesewouldmaketheChatbotlooksevenmoresmarterduringaconversation.
UsingTextToSpeechWouldn'titbegreatifyourcomputercouldspeakbacktoyouwheneveryouorderittodosomething,we'veaccomplishjustthatin"Chatterbot12"thelatestversionoftheprogram.Nowtheprogramcanspeakouteveryanswerthatishasselectedafterexaminingtheuser'sinput.TheSAPIlibraryfromMicrosoftwasusedinordertoaddthe"TextToSpeech"featurewithintheprogram.Fortheimplementationpart,threenewfunctionswereaddedtotheprogramtoimplementthe"TextToSpeech"functionality:Initialize_TTS_Engine(),speak(conststd::stringtext),Release_TTS_Engine().
Initialize_TTS_Engine():Thesefunctionasthenamesuggestinitializedthe"TextToSpeechEngine"thatis,wefirststartbyinitializingthe"COMobjects"sinceSAPIisbuildontopoftheATLlibrary.Iftheinitializationwassuccessful,wethencreateaninstanceoftheISpVoiceobjectthatcontrolledthe"TextToSpeech"mechanismwithintheSAPIlibrarybyusingtheCoCreateInstancefunction.Ifthatalsowassuccessful,itmeansthatour"TextToSpeechEngine"wasinitializedproperlyandwearenowreadyforthenextstage:speakoutthe"responsestring"speak(conststd::stringtext):So,thisisthemainfunctionthatisusedforimplementing"TextToSpeech"withintheprogram,itbasicallytakesthe"responsestring"convertedtowidecharacters(WCHAR)andthenpassittothe"Speakmethod"ofthe"ISpVoice"objectwhichthenspeakoutthe"bot'sresponse".Release_TTS_Engine():Oncewearedoneusingthe"SAPITextToSpeechEngine",wejustreleasealltheresourcesthathasbeenallocatedduringtheprocedure.
-
12/31/13 Chatbot Tutorial - CodeProject
www.codeproject.com/Articles/36106/Chatbot-Tutorial?display=Print 12/15
DownloadChatterbot12
UsingaflatfiletostorethedatabaseSofarthe,databasewasalwaysbuiltintotheprogramwhichmeanswheneveryoumodifiedthedatabase,youwouldalsohavetorecompiletheprogram.Thisisnotreallyconvenientbecauseitmighthappensometimesthatweonlywanttoeditthedatabaseandkeeptherestoftheprogramasitis.Forthesereasonandmanyothers,itcouldbeagoodthingtohaveaseparatefiletostorethedatabasewhichthengivesusthecapabilityofjusteditingthedatabasewithouthavingtorecompileallthefilesintheprogram.Tostorethedatabasewecouldbasicallyuseasimpletextfilewithsomespecificnotationstodistinguishthedifferentelementsofthedatabase(keywords,response,transpositions,context...).Inthecurrentprogram,wewillusethefollowingnotationsthathasbeenusedbeforesomeimplementationoftheElizachatbotinPascal.
1. Linesthatstartsby"K"inthedatabasewillrepresentkeywords.2. Linesthatstartsby"R"willrepresentresponses3. Linesthatstartsby"S"willrepresentsignonmessages4. Linesthatstartsby"T"willrepresenttranspositions5. Linesthatstartsby"E"willrepresentpossiblecorrectionscanbemadeaftertransposingthe
user'sinput6. Linesthatstartsby"N"willrepresentresponsesforemptyinputfromtheuser7. Linesthatstartsby"X"willrepresentresponsesforwhenthatchatbotdidnotfindanymatching
keywordthatmatchthecurrentuserinput.8. Linesthatstartsby"W"willrepresentresponsesforwhentheuserrepeatitself.9. Linesthatstartsby"C"willrepresentthecontextofthechatbot'scurrentresponse.
10. Linesthatstartsby"#"willrepresentcomments
Wenowhaveacompletearchitectureforthedatabase,wejustneedtoimplementthesesfeaturesintothenextversionofthechatbot(Chatterbot13).
DownloadChatterbot13
AbetterrepetitionhandlingalgorithmInanefforttopreventthechatbotfromrepeatingitselftoomuch,previouslywehaveuseaverybasicandsimplealgorithmthatconsistofcomparingthecurrentchatbot'sresponsetothepreviousone.Ifthecurrentresponseselectionisequaltothepreviousone,wesimplydiscardthatresponseandlookoverforthenextresponsecandidateonthelistofavailableresponses.Thisalgorithmisveryefficientwhenitcomestocontrolimmediaterepetitionsfromthechatbot.However,it'snotthatgoodtoavoidmorelongtermrepetition.Duringachattingsession,thesameresponsecanoccursmanytimes.Withthenewalgorithm,wecontrolhowlongittakesforthechatbottoreselectthesameresponse.Actuallywemakesurethatithasuseallavailableresponseforthecorrespondingkeywordbeforeitcanrepeatthesameresponse.Thisisinturncanimprovethequalityoftheconversationexchanges.Hereisadecryptiononhowthealgorithmworks:Duringtheconversationbetweenthechatbotandtheuser,wemakealistofalltheresponsespreviouslyselectedbythechatrobot.Whenselectinganewresponse,wemakeasearchofthencurrentselectedresponseinsidetheliststartingfromtheend.Ifthecurrentresponsecandidatewasfoundduringthatsearchwithinthelist,wethenmakeacomparisonofthatpositionthetotalnumberofavailableresponses.ifthepositionplusoneisinferiortothetotalofavailableresponses,weconsiderthatitisarepetition,sowehavetodiscardthecurrentresponseandselectanotherone.
DownloadChatterbot14
-
12/31/13 Chatbot Tutorial - CodeProject
www.codeproject.com/Articles/36106/Chatbot-Tutorial?display=Print 13/15
UpdatingthedatabasewithnewkeywordsSometimes,whenitcomestoaddnewkeywordstothedatabase,itcouldbedifficulttochoosethosethatarereallyrelevant.However,thereisaverysimplesolutiontothatproblem.Whenchatingwiththechatrobot,wejustmakesurethatwestoretheuser'sinputinafile(ex:unknown.txt)eachtimethechatbotwasnotabletofindanymatchingkeywordforthecurrentinput.Lateron,whenweneedtomakesomekeywordsupdatesinthedatabase,wejusthavetotakealookatthefilethatwe'veusetosavetheunkownsentencesfoundearlierduringthepreviousconversations.Bycontinuouslyaddingnewkeywordsusingtheseprocedure,wecouldcreateaverygooddatabase.DownloadChatterbot15
SavingtheConversationLogsWhysavingtheconversationsbetweentheusersandthechatbot?Becauseitcouldhelpusfindtheweaknessofthechatbotduringagivenconversation.Wemightthendecideonwhichmodificationstomaketothedatabaseinordertomakethefutureconversationsexchangesmorenatural.Wecouldbasicallysavethetimeandalsothedatetohelpusdeterminetheprogressofthechatbotafternewupdateswereappliedtoit.Savingthelogshelpsusdeterminehowhumanlikeistheconversationskillofthechatbot.
DownloadChatterbot16
LearningCapabilitySofar,thechatbotwasnotabletolearnnewdatafromtheuserswhilechatting,itwouldbeveryusefultohavethisfeaturewithinthechatbot.Itbasicallymeansthatwheneverthechatbotencountersaninputthathasnocorrespondingkeyword,itwouldprompttheuseraboutit.Andinreturntheuserwouldbeabletoaddanewkeywordandthecorrespondingresponsetoitinthedatabaseofthechatrobot,doingsocanimprovethedatabaseofthechabotverysignificantly.Hereishowthealgorithmshouldgo:
1. NOKEYWORDWASFOUNDFORTHISINPUT,PLEASEENTERAKEYWORD2. SOTHEKEYWORDIS:(key)3. (ifresponseisno)PLEASEREENTERTHEKEYWORD(gobacktostep#2)4. NORESPONSEWASFOUNDFORTHISKEYWORD:(key),PLEASEENTERARESPONSE5. SO,THERESPONSEIS:(resp)6. (ifresponseisno)PLEASEREENTERTHERESPONSE(gobacktostep#4)7. KEYWORDANDRESPONSELEARNEDSUCCESSFULLY8. ISTHEREANYOTHERKEYWORDTHATISHOULDLEARN9. (ifresponseisyes,otherwisecontinuechating):PLEASEENTERTHEKEYWORD(goback
tostep#2)
Returntobeginningofthedocument
Checktheaiprogramming.blogspot.comwebpageforthelatestupdates
LicenseThisarticle,alongwithanyassociatedsourcecodeandfiles,islicensedunderTheCodeProjectOpenLicense(CPOL)
-
12/31/13 Chatbot Tutorial - CodeProject
www.codeproject.com/Articles/36106/Chatbot-Tutorial?display=Print 14/15
GonzalesCeneliaHelpdesk/SupportGexelTelecomCanada
IhavebeenprogramminginCandC++formorethanfouryears,thefirsttimethatihadlearnprogrammingwasin1999incollege.Howeveritwasonlybytheyear2000whenihavebuymyfirstcomputerthatihadtrulystartedtodosomemoreinterestingthingsinprogramming.Asaprogrammer,mymaininterestisA.Iprogramming.Soi'mreallycaptivatedbyallthatisrelatedtoN.L.U(NaturalLanguageUnderstanding),N.L.P(NaturalLanguageProcessing),ArtificialNeuralNetworksetc.Currentlyi'mlearningtoprograminPrologandLisp.Also,i'mreallyfascinatedwiththeoriginalchatterbotprogramnamed:Eliza,thatprogramwaswrotebyJosephWeizenbaum.Everytimeirunthisprogram,itmakesmereallythinkthatA.Icouldbesolveoneday.AlotofinterestingstuffhasbeenaccomplishinthedomainofArtificialIntelligenceinthepastyears.Averygoodexampleofthoseaccomplishmentsis:LogicProgramming,whichmakesitpossibletomanipulatelogicstatementsandalsotomakesomeinferencesaboutthosestatements.Aclassicalexamplewouldbe:giventhefactthat"Everymanismortal"andthatSocratesisaman,thanlogicallywecandeducethatSocratesismortal.SuchsimplelogicalstatementscanbewroteinPrologbyusingjustafewlinesofcode:prologcodesample:mortal(X):man(X).%ruleman(socrates).%declaringafacttheprecedingprologrulecanberead:foreveryvariableX,ifXisamanthanXismortal.theselastPrologcodesamplecanbeeasilyextentedbyaddingmorefactsorrules,example:mortal(X):man(X).%rulemortal(X):woman(X).%ruleman(socrates).%fact1man(adam).%fact2woman(eve).%fact3formore,check:aiprogramming.blogspot.com
AbouttheAuthor
CommentsandDiscussions
-
12/31/13 Chatbot Tutorial - CodeProject
www.codeproject.com/Articles/36106/Chatbot-Tutorial?display=Print 15/15
Permalink|Advertise|Privacy|MobileWeb01|2.7.131230.1|LastUpdated29May2013
ArticleCopyright2009byGonzalesCeneliaEverythingelseCopyrightCodeProject,19992013
TermsofUse
41messageshavebeenpostedforthisarticleVisithttp://www.codeproject.com/Articles/36106/ChatbotTutorialtopostandviewcommentsonthisarticle,orclickheretogetaprintviewwithmessages.