Jump to content

Updated AIML bot


JRowe
 Share

Recommended Posts

I'm working through the AIML chatterbot code that was posted here about 2 years ago by theguy0000. I'm slowly picking it apart and trying to make it a little more practical.

Basically, I've cut everything down to the minimum necessary for it to work. You need the brain.aiml and XML.au3

^^ rename this as brain.aiml and put it in your script folder.^^

It seems very simple. I like the idea of templated input/response interaction with a bot. This version accepts only <category>, <pattern> and <template> tags. There's no capability for randomized responses or interaction with your system. This loads purely static data into a set of arrays and basically performs an if/then comparison to see whether the input matches any patterns. If a pattern is matched, it returns the reply specified in the <template> tags for that category.

What I'd like help with is ideas and help with adding dynamic elements. For example, the symbol '%audience' could signify the name of the person being addressed.

Change the " I am *" template in your brain.aiml from

That's a nice name.

to

That's a nice name, %audience

Set the variable $botmaster to your name.

We've just abstracted a conversational element to a dynamic (albeit statically defined) variable.

Are there any obvious problems with a method like this?

What symbols would be useful? Abstracting the functionality and logical processing from the templates seems a good idea, to me. This way, you can define ever more generic categories as the scope of your symbols expands.

I was thinking things like %system, %memory, %math, %googleLookup, and so on. Symbols could be used as a simple way of initiating functions.

On the flip side, next thing I'm going to do is work out multiple wildcards and some sort of parse recursion, so that patterns like

<pattern> is a * a *</pattern> can be matched, and the wildcard data passed to whatever functions need it.

Any contributions welcome!

More AIML sets can be found at www.alicebot.org . Lots of them won't completely work, because they use the full set of AIML tags, and this version can handle only 6.

CODE

#include <XML.au3>

#include <GUIConstants.au3>

#include <Misc.au3>

#include <GUIEdit.au3>

Global $contents = _XMLLoad("brain.aiml")

Global $categories_num = __StringNumOccur($contents, "<category>")

Global $botinfo

Global $bot_name

Global $unknown_reply

Global $botmaster = "Your Name"

;MsgBox (0, "info", "found "&$categories_num&" categories.")

Global $patterns[$categories_num + 1]

Global $templates[$categories_num + 1]

If $categories_num = 0 Then Exit MsgBox (48, "AIML Bot", "AIML Syntax Error:"&@CRLF&@CRLF&"There are no <category> tags. Please check your code and try again.")

If $categories_num > __StringNumOccur ($contents, "</category>") Then Exit MsgBox (48, "AIML Bot", "AIML Syntax Error:"&@CRLF&@CRLF&"There is one or more <category> tag without an ending </category> tag. Please check your code and try again.")

If $categories_num < __StringNumOccur ($contents, "</category>") Then Exit MsgBox (48, "AIML Bot", "AIML Syntax Error:"&@CRLF&@CRLF&"There is one or more extra </category> tags without beginning <category> tags. Please check your code and try again.")

_InitBot()

For $i=1 To $categories_num

$cat_contents = _XMLGet ($contents, "category", $i)

$cat_pattern = StringLower (StringReplace (StringLower(_XMLGet ($cat_contents, "pattern")), "&botname;", $bot_name))

__StringStripPunct ($cat_pattern)

If @error Then Exit MsgBox (48, "AIML Bot", "AIML Syntax Error:"&@CRLF&@CRLF&"Category number "&$i&" does not have a <pattern> tag. Please check your code and try again.")

If $cat_pattern = "" Then Exit MsgBox (48, "AIML Bot", "AIML Syntax Error:"&@CRLF&@CRLF&"The <pattern> tag in category number "&$i+1&" is empty. Please check your code and try again.")

$patterns[$i] = $cat_pattern

$cat_template = _XMLGet ($cat_contents, "template")

If @error Then Exit MsgBox (48, "AIML Bot", "AIML Syntax Error:"&@CRLF&@CRLF&"Category number "&$i&" does not have a <template> tag. Please check your code and try again.")

If $cat_template = "" Then Exit MsgBox (48, "AIML Bot", "AIML Syntax Error:"&@CRLF&@CRLF&"The <template> tag in category number "&$i+1&" is empty. Please check your code and try again.")

$templates[$i] = $cat_template

Next

GUICreate ( "AIML Bot", 261, 321, (@DesktopWidth-261)/2, (@DesktopHeight-321)/2 )

$input = GUICtrlCreateInput("", 10, 260, 180, 40)

$history = GUICtrlCreateEdit("Please type in the input field at the bottom."&@CRLF&"---------------", 10, 10, 240, 230, BitOR($WS_VSCROLL, $ES_READONLY, $ES_AUTOVSCROLL))

$Sbutton = GUICtrlCreateButton("Say", 200, 260, 50, 30, $BS_DEFPUSHBUTTON)

$exit = GUICtrlCreateButton("Exit", 200, 300, 50, 20)

GUISetState()

While 1

$msg = GUIGetMsg()

If $msg = $GUI_EVENT_CLOSE Then

Exit

ElseIf $msg = $Sbutton Then

$Text = GUICtrlRead($input)

If $Text <> "" Then

$response = AIML_Respond ($Text)

GUICtrlSetData ( $history, GUICtrlRead ($history) & @CRLF & "You: " & $Text & @CRLF & $bot_name & ": " & $response )

EndIf

GUICtrlSetData ($input, "")

ElseIf $msg = $exit Then

Exit

EndIf

For $Line_Count = 1 To _GUICtrlEdit_GetLineCount ($history)

_GUICtrlEdit_Scroll ($history, $SB_LINEDOWN)

Next

WEnd

Func AIML_Respond ($message)

$message = StringLower ($message)

__StringStripPunct ($message)

;~ $pos = _ArraySearch ($patterns, $message)

$pos = __ArraySearchWithWildcards ($patterns, $message)

;_ArrayDisplay ($patterns, "patterns")

_ArrayDisplay ($templates, "templates")

;MsgBox (0, "pos", $pos)

If $pos = -1 Or $pos=0 Then

$response = $unknown_reply

Else

$response = $templates[$pos]

EndIf

$response = StringReplace ($response, "&botname;", $bot_name)

If StringInStr($response, "%audience") Then

$response = StringReplace($response, "%audience", $botmaster)

EndIf

Return $response

EndFunc

Func _InitBot()

$botinfo = _XMLGet ($contents, "botinfo")

If @error Then Exit MsgBox (48, "AIML Bot", "AIML Syntax Error:"&@CRLF&@CRLF&"There is no <botinfo> tag. This tag should contain a <name> tag with the name of your bot, and an <unknown> tag containing your bot's default response for when a user inputs a message that is unknown to the bot. Please check your code and try again.")

$bot_name = _XMLGet ($botinfo, "name")

If @error Then Exit MsgBox (48, "AIML Bot", "AIML Syntax Error:"&@CRLF&@CRLF&"There is no <name> tag inside your <botinfo> tag. The <name> tag should contain the bot's name. Please check your code and try again.")

$unknown_reply = _XMLGet ($botinfo, "unknown")

If @error Then Exit MsgBox (48, "AIML Bot", "AIML Syntax Error:"&@CRLF&@CRLF&"There is no <unknown> tag inside your <botinfo> tag. The <name> tag should contain your bot's default response for when a user inputs a message that is unknown to the bot. Please check your code and try again.")

EndFunc

Edited by Jrowe
Link to comment
Share on other sites

  • 10 months later...

Something I found on another site . . .

http://www.aimlworld.com/forums/showthread.php?t=48

Looks promising - working on it now. -will be adding in support for aaa a.l.i.c.e. aiml files for personlity

Also follow . . ..

http://www.vrconsulting.it/vhf/topic.asp?w...=906&#17678

#NoTrayIcon

$RecoContext=ObjCreate("SAPI.SpSharedRecoContext")

$SinkObject=ObjEvent($RecoContext,"MYEvent_")

$Grammar = $RecoContext.CreateGrammar(1)

$Grammar.Dictationload

$Grammar.DictationSetState(1)

sleep(5000)

$Grammar.DictationSetState(0)

Func MYEvent_Recognition($StreamNumber,$StreamPosition,$RecognitionType,$Result)

$file = FileOpen ( "heard.txt", 2)

FileWrite ( $file, $Result.PhraseInfo.GetText )

FileClose ( $file )

exit

EndFunc

AutoItSetOption ( "TrayAutoPause", 0)

Local $o_speech = ObjCreate ("SAPI.SpVoice")

$voice = $o_speech.GetVoices().Item(1)

$o_speech.Voice = $voice

Global $text

While 1

gettext()

TrayTip ( "Heard:", $text, 5, 1 )

Select

Case $text = "exit"

$o_speech.Speak ('<rate speed="4">' & 'goodbye' & '</rate>', 8)

Exit

Case $text = "hello"

TrayTip ( "Respond", "Hi There", 5, 1 )

$o_speech.Speak ('<rate speed="4">' & $text & '</rate>', 8)

Sleep(300)

Case $text = "change voice"

$o_speech.Speak ('<rate speed="4">' & 'Mary, Mike, or Sam' & '</rate>', 8)

gettext()

Select

case $text = "mary"

$voice = $o_speech.GetVoices().Item(1)

$o_speech.Voice = $voice

$o_speech.Speak ('<rate speed="4">' & 'Mary' & '</rate>', 8)

case $text = "mike"

$voice = $o_speech.GetVoices().Item(2)

$o_speech.Voice = $voice

$o_speech.Speak ('<rate speed="4">' & 'mike' & '</rate>', 8)

case $text = "sam"

$voice = $o_speech.GetVoices().Item(3)

$o_speech.Voice = $voice

$o_speech.Speak ('<rate speed="4">' & 'sam' & '</rate>', 8)

case Else

$o_speech.Speak ('<rate speed="4">' & 'The Voice Was Not changed' & '</rate>', 8)

EndSelect

case else

TrayTip ( $voice.GetAttribute("Name"), $text, 5, 1 )

$o_speech.Speak ('<rate speed="4">' & $text & '</rate>', 8)

EndSelect

WEnd

Func OnAutoItExit ( )

FileDelete ( @ScriptDir & "\heard.txt" )

EndFunc

Func gettext()

RunWait(@ScriptDir & "\write.exe")

$file = FileOpen ( "heard.txt", 0 )

$text = FileRead ($file )

FileClose ( $file )

$file = FileOpen ( "heard.txt", 2 )

FileWrite ($file, "")

FileClose ( $file )

EndFunc

Link to comment
Share on other sites

Oh, what the hell. Some random dickwad is claiming credit for my scripts on that forum... Among others as well.

That's not gonna last long.

Edited by JRowe
Link to comment
Share on other sites

I posted on the forums and directed credit appropriately, at least for the AutoIt stuff. If it gets deleted or if the owner tries to claim credit, that forum is in for a world of hurt. It *really* bothers me. I don't mind people using it, I don't mind people reposting it, I don't mind people hacking it to shreds or turning into a nuclear weapons guidance system. I just want acknowledgement for something that took me a ton of tedious scripting.

Link to comment
Share on other sites

I posted on the forums and directed credit appropriately, at least for the AutoIt stuff. If it gets deleted or if the owner tries to claim credit, that forum is in for a world of hurt. It *really* bothers me. I don't mind people using it, I don't mind people reposting it, I don't mind people hacking it to shreds or turning into a nuclear weapons guidance system. I just want acknowledgement for something that took me a ton of tedious scripting.

it looks like they just copied it from one of Cyber's threads from 06' - you can tell from the update comment - they copied that too. ;-)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...