Never been to DZone Snippets before?

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

About this user

NoƩ Rubinstein http://dotdotno.canalblog.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Left to right affectation operator in Io

This simple line makes a simple left to right affectation operator in Io

   1  
   2  -> := method( call sender setSlot(call argAt(0) name, self)) 
   3  # Use :
   4  2 -> a
   5  3 -> b
   6  a * b -> c
   7  c println # => 6
   8  # (thaks to jer) Note : This is quite broken
   9  foo := Object clone
  10  2 -> foo c 
  11  foo c # raises an exception: "Number does not respond to c"
  12  foo # => 2
  13  # However, one can do :
  14  foo do(2 -> c)

Irma the Chatterbot

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")

   1  
   2  Sequence words := method( Regex setIsUTF8(true) setString(self) setPattern("\\w+") allMatches map(asString) )
   3  Object or := method(self)
   4  Irma := Object clone do(
   5  	vocabulary := Map clone
   6  	vocabulary atPut("bonjour",list("Salut !"))
   7  	vocabulary atPut("salut",list("Comment vas-tu ?","Bonjour"))
   8  
   9  	register := method(rep1, rep2, 
  10  		rep1 := rep1 asLowercase words join(" ")
  11  		if(vocabulary at(rep1),
  12  			vocabulary at(rep1) append(rep2),
  13  			vocabulary atPut(rep1, list(rep2))
  14  		)
  15  	)
  16  	
  17  	answer := method(rep,
  18  		exactAnswer(rep) or nearAnswer(rep)
  19  	)
  20  	exactAnswer := method(rep, 
  21  		rep := rep asLowercase words
  22  		vocabulary detect(k, k words == rep ) ?at(1) ?anyOne
  23  	)
  24  
  25  	randAnswer := method( vocabulary values flatten anyOne )
  26  
  27  	nearAnswer := method( rep,vocabulary at(vocabulary keys max(words intersect(rep asLowercase words) join size)) anyOne)
  28  
  29  	save := method(	vocabulary serialized	)
  30  	
  31  	with := method( dict, vocabulary = doString(dict) )
  32  
  33  	saveToFile := method( file, f := File clone with(file or "irma_sav.io") openForUpdating ; f write(self save println) ; f close)
  34  
  35  	withFile := method( file, self with(File clone with(file or "irma_sav.io") open readLines join("\n")))
  36  
  37  	willFree := method( "Saving Irma" println ; saveToFile( "irma.emergency.io" ) )
  38  )
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS