Chat bots

Blitz3D Forums/Blitz3D Programming/Chat bots

David819(Posted 2005) [#1]
Hi there, How would i go about creating a chat bot in blitz?
Thanks in advance.


John J.(Posted 2005) [#2]
If you are refferring to english "comprehending" artificial intelligence, there are an almost infinite number of ways to do this. One simple way is to compare the words and phrases of the human's input against a database of "form" inputs and replys. Other ways include "learning" artificial intelligence where the bot picks up words ans syntax from the human.


John J.(Posted 2005) [#3]
Oh, and here's a little "chat bot" I made in blitz. Just type to it and it will talk back. Remember, this bot learns 100% of it's syntax and knowledge from you.

;Set up screen
Graphics 640,480,0,2
Cls

;Main loop
While 1
	x$ = Input()
	Print Reply(x)
	
	;debug
	;For t.Word = Each Word
	;	Print t\txt+": "+t\Links
	;Next
Wend
End



Type Word
	Field Txt$
	Field Link.Word[256]
	Field LinkStrength[256]
	Field Links
End Type

;Generate a rely for the given sentance
Function Reply$(s$)
	Local word$[256]
	Local words = 1

	;Split to words
	For i = 1 To Len(s)
		m$ = Mid(s, i, 1)
		If m$ = " " Then
			If word[words] <> "" Then words = words + 1
		Else
			If m <> "." And m <> "," And m <> "!" And m <> "?" Then word[words] = word[words] + m
		End If
	Next
	If words = 1 And word[1] = "" Then
		words = 0
	Else
		words = words + 1
		word[words] = "."
	End If
	
	;Remember the words and their relationships
	For i = 1 To words
		AddWord(Lower(word[i]))
		If i > 1 Then AddLink(Lower(word[i-1]), Lower(word[i]))
	Next
	
	;If nothing is in memory, return blank
	t.Word = First Word
	If t = Null Then Return ""
	
	;Pick a keyword for the reply (t.Word)
	If words = 0 Then
		;Pick a word at random
		count = 0
		For t.Word = Each Word: count = count + 1: Next
		pick = Rand(1, count)
		t.Word = First Word
		For i = 2 To pick
			t = After t
		Next
	Else
		;Pick a word the user used that has the least links
		maxlinks = 100000
		maxword.Word = Null
		For i = 1 To words
			lw$ = Lower(word[i])
			For t.Word = Each Word
				If t\Txt = lw Then Exit
			Next
			If t\Links > 0 And t\Links < maxlinks Then maxlinks = t\Links: maxword = t
		Next
		t = maxword
	End If
	
	;Generate a sentance
	out$ = "": done = False
	While done = False
		out = out + t\Txt + " "
		If t\Txt = "." Then
			done = True
		Else
			pick = Rand(1, t\Links)
			pick2 = Rand(1, t\Links)
			If t\LinkStrength[pick] > t\LinkStrength[pick2] Then t = t\Link[pick] Else t = t\Link[pick2]
		End If
	Wend
	
	;Decay
	If s = "" Then
		;For t.Word = Each Word
		;	Decay(t)
		;Next
	End If
	
	Return out
	
End Function

;Add a word to the AI bot's memory
Function AddWord(word$)
	For t.Word = Each Word
		If t\Txt = word Then Return ;word is already in memory
	Next
	this.Word = New Word
	this\Txt = word
	this\Links = 0
End Function

;Link two words together so the AI will know how to form sentances later
Function AddLink(word$, linkword$)
	;Get the objects for the words specified
	For t.Word = Each Word
		If t\Txt = word Then this.Word = t: Exit
	Next
	If this.Word = Null Then Return
	For t.Word = Each Word
		If t\Txt = linkword Then link.Word = t: Exit
	Next
	If link.Word = Null Then Return
	
	;Does the link exist?
	exists = False
	For i = 1 To this\Links
		If this\Link[i] = link Then exists = True: Exit
	Next
	
	;If link exists, strengthen it
	If exists = True Then
		this\LinkStrength[i] = this\LinkStrength[i] + 1
	End If
	
	;If link does not exist, add it
	If exists = False Then
		this\Links = this\Links + 1
		this\Link[this\Links] = link
		this\LinkStrength[this\Links] = 1
	End If
	
End Function

;Weaken all word links for the specified word. Using "decay" for word links allows old unused
;words (such as mis-spelled words) to be forgotten.
Function Decay(word.Word)
	For i = 1 To word\Links
		word\LinkStrength[i] = word\LinkStrength[i] - 1
		
		If word\LinkStrength[i] = 0 Then
			word\LinkStrength[i] = word\LinkStrength[word\Links]
			word\Link[i] = word\Link[word\Links]
			word\Link[word\Links] = Null
			word\LinkStrength[word\Links] = 0
			word\Links = word\Links - 1
			i = i - 1
		End If
	Next
End Function



David819(Posted 2005) [#4]
Do you mean like if i input, 'Hi, How are you?' the datavase would check:
you - I
etc.
Where can i learn A.I Basics to advanced levels?


John J.(Posted 2005) [#5]
That method would work like this:

You type: "Hi! How are you?"

The computer searches through it's database of form Q&A's, rating each item with it's "closeness" to your sentance:

"How are you?" Score: 3 (words matching)
"What's new"?" Score: 0
"How did you do that?" Score: 1

Then, the computer picks the closest matching input ("How are you?", in this case), and then looks up the appropriate answer, which could be "I'm fine."

So you would type "How are you?", and the computer replys "I'm fine".

I'm not sure exacty where to find AI tutorials, but I'm sure google can find you plenty.

P.S. If you try the demo I posted (which really isn't that much good for practal purposes), remember that it will copy you until it learns enough of your language to generate it's own sentances (any language can be typed in to it, since it learns completely from you)


sswift(Posted 2005) [#6]
I made a chat bot years ago for BBS's. It was called Siggy, and the way it worked was it split up everythign you typed into sentences and then saved each of them. Then it went through the words and found other sentences with the same words to parrot them back to you.

Worked well enough to fool people, but didn't usually hold very coherent conversations. :-)


John J.(Posted 2005) [#7]
Yeah.. That's really sounds like a good way to make a adaptive chat bot.

I once made a "virtual" chat room with bots similar to that where a couple of chat bots would talk to each other (you also could participate in the conversation), each with a different personality. It was quite interesting to watch them talk, argue, and even have fights with each other :)


Snarkbait(Posted 2005) [#8]
You could fairly easily adapt a mIRC script bot to blitz. A good source for mirc scripts is http://www.hawkee.com/ (*cough* and not just because I have an entry on there http://www.hawkee.com/snippet.php?snippet_id=302 ;) )


Techlord(Posted 2005) [#9]
Perhaps a Binary Decision Tree could be useful for a chatter bot.


Techlord(Posted 2005) [#10]
Kain,

I'm curious if you intend to use Chat Bot Technology in a Game? Seems like it be very interesting for a RPG or game where conversational dialog is needed. The topic prompted me to research the subject.

I'm learning about topics like: CBR Cased-Based Reasoning and AIML - Artificial Intelligence Markup Language. I feel motivation building up. I'm fomulating a design strategy for an Adult-Oriented Chat Bot.


WolRon(Posted 2005) [#11]
You can get some ideas from my conversion of the classic game Eliza.
http://home.cmit.net/rwolbeck/programmingtutorial/code/eliza.htm


Techlord(Posted 2005) [#12]
WolRon,

Excellent Chat Bot.


gpete(Posted 2005) [#13]
John J.
What a neat program...I regretted closing the program as my computer began putting together better sentences...really fun. Is there a way to save the linked lists so the "personality" survives?


Leiden(Posted 2005) [#14]
Try getting the source to EMacs, it has a rather interesting 'therapist' chat bot thing.


Techlord(Posted 2005) [#15]
Anyone using Chat Bots in their games?


Leiden(Posted 2005) [#16]
Counter-Strike bots use some kind of primitive chat-bot stuff, Check out PodBOT


John J.(Posted 2005) [#17]
gpete: Yeah, you'd just have to save all the Word type objects, something like this:
file = WriteFile("output.bot")
For this.Word = Each Word
	WriteString this\Txt
	WriteInt this\Links
	For i = 1 To this\Links
		WriteInt this\LinkStrength[i]
		WriteString this\Link[i]\Txt
	Next
Next
CloseFile(file)