This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
BigSHinyToys's profile picture

[FUN] AI learning program that can talk.

Started by BigSHinyToys, 20 August 2012 - 03:52 PM
BigSHinyToys #1
Posted 20 August 2012 - 05:52 PM
[edit]
This is not an AI sorry

I apologizes for falsely calming this was an AI worry.


While refactoring this code i learned that this was not using the look up table correctly and just randomly selecting words from it. This is not an AI my apologizes.

I will leave the original post as it was bellow this edit.
[/edit]

Ok so AI is a slight over statement this program is designed to learn from you and talk to yo.

the program comes with no no words in its dictionary and cant save so it will lose all words when it resets.

Instructions
talk to he say hello discuss the weather what ever you say to it will make it smarter.

Note: thanks to immibis it have save system now

Version 4 with save system by immibis
*NEW* now saves conversation in convo file.

[EXPERIMENTAL]
this latest it can read information from files and add it to its memory
start it with a parth to a folder that contains the stuff you want it to read
example


AI books
would read all test documents in the folder named "books" on the computers HDD
http://pastebin.com/BdtQCH4a
[/EXPERIMENTAL]

[STABLE]
http://pastebin.com/Bpqpcv9j

pastebin get Bpqpcv9j AI
Spoiler

--[[
	Brain Damage Algorithm BDA for short
	Emulates drunk or brain damaged persons speech patterns
	Devloped By BigSHinyToys
	http://www.computercraft.info/forums2/index.php?/topic/3546-fun-ai-learning-program-that-can-talk/
  
]]--
local winX,winY = term.getSize()
local length = 8
local tDictionary = {}
local shado = {}
local savefile = "words"
local convofile = "convo"
local function saveTalk(sentence)
	local f = fs.open(convofile, "a")
	f.write(sentence.."n")
	f.close()
end
local function save()
	local f = fs.open(savefile, "w")
	f.write(textutils.serialize({tDictionary, shado}))
	f.close()
end
if fs.exists(savefile) then
	local f = fs.open(savefile, "r")
	local save = textutils.unserialize(f.readAll())
	f.close()
	tDictionary = save[1]
	shado = save[2]
end
local function dataAI()
	local lastTimer = os.startTimer(math.random(1,120))
	while true do
		local event,arg1,arg2,arg3 = os.pullEvent()
		if event == "new_line" or event == "timer" then
			if event == "timer" then
				if arg1 == lastTimer then
					lastTimer = os.startTimer(math.random(1,120))
				end
				arg1 = ""
			end
			local sLine = arg1
			local tWords = {}
			for match in string.gmatch(sLine, "[^ t]+") do
				table.insert( tWords, match )
			end
			for i = 1,#tWords do
				if tDictionary[tWords[i]] then
					table.insert(tDictionary[tWords[i]]["to"],tWords[i])
				else
					table.insert(shado,tWords[i])
					tDictionary[tWords[i]] = {}
					tDictionary[tWords[i]]["name"] = tWords[i]
					tDictionary[tWords[i]]["to"] = {}
				end
			end
			local test = ""
			local last = nil
			for i = 1,math.random(1,length) do
				if last then
					if #tDictionary[last]["to"] and #tDictionary[last]["to"] > 0 then
						sNext = tDictionary[last]["to"][math.random(1,#tDictionary[last]["to"])]
						if sNext ~= last then
							test = test..sNext.." "
							last = sNext
						else
							last = tDictionary[shado[math.random(1,#shado)]]["name"]
							test = test..last.." "
						end
					else
						last = tDictionary[shado[math.random(1,#shado)]]["name"]
						test = test..last.." "
					end
				else
					last = tDictionary[shado[math.random(1,#shado)]]["name"]
					test = test..last.." "
				end
			end
			term.scroll(1)
			term.setCursorPos(1,winY - 1)
			term.clearLine()
			save()
			saveTalk("AI> "..test)
			print("AI> "..test)
		end
	end
end
local function userInput()
	while true do
		term.setCursorPos(1,winY)
		term.clearLine()
		local input = read()
		saveTalk(input)
		os.queueEvent("new_line",input)
	end
end
term.clear()
parallel.waitForAny(userInput,dataAI)


NitrogenFingers ver with grammar and immibis save system
*New* with conversation save ver 2

http://pastebin.com/pTKpFVJN

pastebin get pTKpFVJN GrammarAI
Spoiler

--[[
	BDA: Brain Damaged Algorithm
	A natural language parsing and attempting to creating program
  
	Written by: BigSHinyToys
	Modified by: NitrogenFingers 28/8/2012 (12:20am)
--]]
local winX, winY = term.getSize()
--Add your stop word lists here- they can be common subjects, participles, or any other grammatical
--feature you'd like to capture. Denote it with a single, lowercase character- this indicates it is a
--terminating symbol
local stopWords = {
	["s"] = {"I", "you", "he", "she", "they", "we"};
	["v"] = {"is", "are", "am", "can be", "like", "hate", "often", "will", "must", "should", "might"};
	["a"] = {"and", "or", ", also", ", but", ", however", "like"};
}
--This list is of words that aren't stop words- they'll be added as you type them
local vocab = {}
local vcLength = 0
local savefile = "Grammerwords"
local convofile = "Grammerconvo"
local function saveTalk(sentence)
	local f = fs.open(convofile, "a")
	f.write(sentence.."n")
	f.close()
end
local function save()
	local f = fs.open(savefile, "w")
	f.write(textutils.serialize(vocab))
	f.close()
end
if fs.exists(savefile) then
	local f = fs.open(savefile, "r")
	local save = textutils.unserialize(f.readAll())
	f.close()
	vocab = save
end
--Here you should have tables with capital letters, your non-terminating characters, that include
--all possible rules for your grammar. These tokens will in turn be selected and randomly replaced
--with one of the contents of the table.
--Note: AVOID INFINITE LOOPS: make sure every possible grammar has a terminating-only character string.
local grammar = {
	["S"] = {"svW"},
	["W"] = {"waW", "wasW", "w"}
}
function readWords()
	while true do
		local arg1, arg2 = os.pullEvent("new_line")
	  
		if arg1 == "new_line" then
			local lastMatch = ""
			for match in string.gmatch(arg2, "[^ t]+") do
				local stop = false
				for _,list in pairs(stopWords) do
					for _,stopword in pairs(list) do
						--If you want to be supersmart you can try to detect stop words and add them
						--to your list here- try looking for "ly" for adverbs, "ing" for present tense verbs
						--etc.
						if match == stopword then
							stop = true
							break
						end
					end
				end
			  
				if not stop then
					--We handle linkages here
					if not vocab[match] then
						--I must admit I'm unsure how to get just the key in a table (there must be a way)
						--Anyway I just include the word itself as the first value in the value table. Cheap and dusty.
						vocab[match] = {match, ""}
						--And of course because it's a string indexed value now it's not included in the array, or the length!
						vcLength = vcLength+1
					end
				  
					if lastMatch ~= "" then
						table.insert(vocab[lastMatch], match)
					end
				  
					lastMatch = match
				else
					lastMatch = ""
				end
			  
			end
		end
	  
		local newGrammar = "S"
		local found = false
		--This randomly creates your grammar
		repeat
			found = false
			for k,v in pairs(grammar) do
				local f = string.find(newGrammar, k)
				if f then
					newGrammar = string.gsub(newGrammar, k, v[math.random(1, #v)], 1)
					found = true
				end
			end
		until not found
	  
		--This then randomly replaces your grammar with actual words
		local newSentence = ""
		for i=1,#newGrammar do
			local token = string.sub(newGrammar, i, i)
			if token == "w" then
				--This is horribly messy- will fix when less tired!
				local chosenWord = math.random(1,vcLength)
				local i=1
				for k,_ in pairs(vocab) do
					if i==chosenWord then
						chosenWord = k
						break
					end
					i=i+1
				end
			  
				local chosenLink = vocab[chosenWord][math.random(2,#vocab[chosenWord])]
				if chosenLink ~= "" then chosenLink = chosenLink.." " end
			  
				--Again cheap and dusty here, the extra space will make sentences look a bit ugly. Requires a little tweaking, but I'm to tired to fix right now.
				newSentence=newSentence..vocab[chosenWord][1].." "..chosenLink
			else
				local newstop = stopWords[token][math.random(1,#stopWords[token])]
				--Cheap and dusty (lots of it in this program!) to get rid of extra space
				if string.sub(newstop, 1, 1)=="," then newSentence = string.sub(newSentence, 1, #newSentence-1) end
				newSentence=newSentence..newstop.." "
			end
		end
		term.scroll(1)
		term.setCursorPos(1,winY - 1)
		term.clearLine()
		save()
		local talk = "AI> "..string.sub(newSentence, 1, #newSentence-1).."."
		saveTalk(talk)
		print(talk)
	end
end
function getInput()
	while true do
		term.setCursorPos(1,winY)
		term.clearLine()
		local input = read()
		saveTalk(input)
		os.queueEvent("new_line",input)
	end
end
term.clear()
parallel.waitForAny(getInput, readWords)

and remember it will say what you say swear at it it might swear back lol XD
Cranium #2
Posted 20 August 2012 - 06:00 PM
Neat! Going to have to try this when I get home! I might be able to help save the dictionary to a file too. I'll send you a message whether I can or not.
Graypup #3
Posted 24 August 2012 - 01:32 AM
ipb syntax highlighting ftw (ps to invision.power, Y U MAKE GENERIC SYNTAX HIGHLIGHTING) ;D
Cranium #4
Posted 24 August 2012 - 03:42 AM
At one point it asked me if I was Chuck. I am not Chuck. This made my day.
Lettuce #5
Posted 25 August 2012 - 08:18 PM
Oh yeah. It learns all right. Not quite a "Robots are Taking Over the World" AI, but it recognizes the basics. I said "I" it said "you" Then it said "F@#$ you" so I'm mad now.
BigSHinyToys #6
Posted 26 August 2012 - 01:10 AM
Oh yeah. It learns all right. Not quite a "Robots are Taking Over the World" AI, but it recognizes the basics. I said "I" it said "you" Then it said "F@#$ you" so I'm mad now.
I warned you about swearing at it lol XD

It is just a little experiment. I got this idea of using tables in a tables to store words that link with other words and have the computer choose from the words creating a kinda of neuron group of word each linked to others, stuck in my head. then made it. I was surprised about how life like it could be at times but then it stared talking gibberish.


ALSO ver 2 is basically the same but it will randomly talk back now.

[EDIT]
fixed spelling auto correct error above.
[/EDIT]
Imgoodisher #7
Posted 26 August 2012 - 05:11 PM
lol. my ai said "world. want over" so i said "what over what?" and it said "nonsense! no world? stuff! stuff! you!" this ai is hilarious

EDIT: now it said "want", i said "want what?" and it said "world. yes want sense understanding". i said "so you want a world?" and it said "give give" lol again. now it says it "undersand nonsense!"
and it just said "hello random nonsense!"
BigSHinyToys #8
Posted 27 August 2012 - 06:44 AM
Good to see that it is doing as I told it WORLD DOMINATION …. I means making people laugh.
Cloudy #9
Posted 27 August 2012 - 12:13 PM
It is just a little excrement.

LOL! I think you meant experiment - but that made me laugh.
BigSHinyToys #10
Posted 27 August 2012 - 12:31 PM
LOL! I think you meant experiment - but that made me laugh.
fixed
nitrogenfingers #11
Posted 27 August 2012 - 12:47 PM
It would be really cool if you could recognize a bit of the grammar of sentences and notice common stop words so as to fill up noun and very libraries- that's a big task but it could work on an elementary level.
This reminds me a lot of the policeman's beard is half constructed. Nice program!

EDIT: Oh, my favourite quote so far: dog brain be strengthened is Hello
nitrogenfingers #12
Posted 27 August 2012 - 03:18 PM
I've been messing around with this a little and added a context free grammar, so you can define (and if you really want to detect) stop words in written language, and use them to structure the words the program learns into slightly more meaningful phrases. You can of course go back to the original version of this program by simply having a grammar like
S -> wW
W -> wW
W -> w
W -> _

which is not far off the current grammar! You can also make it quite sophisticated, attempting to structure sentences more carefully, with subjects and objects more carefully obeyed, and even punctuation. It's entirely up to you. I might try and write a good grammar, and when I come up with one post it.

The only problem with this program is I've kind of butchered it (sorry BigSHinyToys), and lost the word linkages, which was one of it's more interesting powers. But you can see what I've done with it here: http://www.mediafire...2yq2w44kye5ucza


EDIT: The linkage is back, so words now retain associations based on the order they are said. I also cleaned up the output and messied up the code and it now speaks broken but somewhat coherent english. The link to that is here: http://www.mediafire.com/download.php?awkgluwe6gtj4av
BigSHinyToys #13
Posted 27 August 2012 - 05:19 PM
added nitrogenfingers Grammar OP and save function by immibis to the ver 2 making it ver 3

enjoy

Big SHiny Toys
Hodoo #14
Posted 27 August 2012 - 07:50 PM
I LOVE this program xD Version 2 was funny, but V3 just blew my mind and made me lmfao

Images:


Lettuce #15
Posted 27 August 2012 - 08:09 PM
Hodoo,
It's a prophecy! I don't know what it means but it's a prophecy!

Been watching this topic, I scooped up ver. 3 right away. I love the funny programs people are coming up with lately.
filipkwro #16
Posted 27 August 2012 - 08:36 PM
My attempts to chat with the AI.
[attachment=404:2012-08-27_21.26.03.png]
[attachment=405:2012-08-27_21.30.24.png]
[attachment=403:2012-08-27_21.26.03.png]
Hodoo #17
Posted 29 August 2012 - 03:19 AM


My computer no longer wants to kill me. :3 (even though he earlier said "I might die" and "you should die")
bbqroast #18
Posted 29 August 2012 - 08:56 AM
This is amazing, I'm amazed by this.
Honestly, this is insanely cool. Could you make it possible to save conversations in plain text files?
BigSHinyToys #19
Posted 29 August 2012 - 11:44 AM
This is amazing, I'm amazed by this.
Honestly, this is insanely cool. Could you make it possible to save conversations in plain text files?
Added conversation saving to a file called convo or Grammerconvo dependent on witch version you are using.
Leo Verto #20
Posted 29 August 2012 - 08:55 PM
My AI hates Valve but is a huge fan of GlaDOS:

hello
AI> we can be hello, also hello, however he hello, however I hello.
how are you?
AI> he is hello.
GlaDOS is an AI
AI> we am you? and I how, also an AI and hello and hello, but she you? or we GlaDOS and we you?.
you are an AI
AI> she should AI.
GlaDOS is from Portal
AI> I can be Portal like I how, also you?.
Valve made Portal
AI> we must how.
Portal is awesome
AI> I should GlaDOS.
Portal is a game
AI> they is made Portal like he Portal.
Minecraft is a game
AI> you can be game, also Portal.
Minecraft is awesome
AI> we are Portal.
the cake is a lie
AI> we hate Valve made, but the cake.
Wheatley is in space
AI> he should awesome or Portal, however they game and AI, however AI, however they Wheatley.
Pwrocks5648 #21
Posted 29 September 2012 - 01:54 AM
the Ai got creepy exventually somehow learning the word kill *shiver runs down spine* im lucky i didnt put it in a turtle….. or plug it into the mainframe of my WIP Ai House! i think it might of tried to kill me then!
Adjgamer #22
Posted 10 December 2012 - 09:45 AM
Frick, you say "F*ck you." once after "yes yes hello how are am do?" and everything else is "yes f*ck hello f*ck am f*ck do?" lololol
#ThisIsEpic
BigSHinyToys #23
Posted 10 December 2012 - 09:50 AM
ah funny think while refactoring this i found out that it is purely selecting words based on random numbers not from a look up table. This was due to a error in the code so that was all randomly generated not procedural.
Meaning this is not an AI sorry

I apologizes for falsely calming this was an AI worry.