DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Noé has posted 2 posts at DZone. View Full User Profile

Irma The Chatterbot

  • submit to reddit
        This is a very simple chatterbot written in Io
To learn Irma how to answer something, do :
Irma register("something", "how to answer")
To get an answer :
Irma answer("Blaha blaha")
To save Irma to a file : 
Irma saveToFile("filename")
To get Irma's vocabulary from a file
Irma withFile("filename")

Sequence words := method( Regex setIsUTF8(true) setString(self) setPattern("\\w+") allMatches map(asString) )
Object or := method(self)
Irma := Object clone do(
	vocabulary := Map clone
	vocabulary atPut("bonjour",list("Salut !"))
	vocabulary atPut("salut",list("Comment vas-tu ?","Bonjour"))

	register := method(rep1, rep2, 
		rep1 := rep1 asLowercase words join(" ")
		if(vocabulary at(rep1),
			vocabulary at(rep1) append(rep2),
			vocabulary atPut(rep1, list(rep2))
	answer := method(rep,
		exactAnswer(rep) or nearAnswer(rep)
	exactAnswer := method(rep, 
		rep := rep asLowercase words
		vocabulary detect(k, k words == rep ) ?at(1) ?anyOne

	randAnswer := method( vocabulary values flatten anyOne )

	nearAnswer := method( rep,vocabulary at(vocabulary keys max(words intersect(rep asLowercase words) join size)) anyOne)

	save := method(	vocabulary serialized	)
	with := method( dict, vocabulary = doString(dict) )

	saveToFile := method( file, f := File clone with(file or "") openForUpdating ; f write(self save println) ; f close)

	withFile := method( file, self with(File clone with(file or "") open readLines join("\n")))

	willFree := method( "Saving Irma" println ; saveToFile( "" ) )