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 )