john-doe 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Pawel Mikolajewski
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,86 @@
1
+ = john-doe
2
+
3
+ John doe is simple chatbot AI basing on idea of AIML language. Yaml is used instead of xml.
4
+
5
+ == Getting started
6
+
7
+ 1. Install the gem
8
+ sudo gem install john-doe
9
+ 2. Create your rules file (like default.yml)
10
+ 3. Sample usage:
11
+
12
+ require 'rubygems'
13
+ require 'john-doe'
14
+ cb = JohnDoe::ChatBot.new("my_rules.yml")
15
+
16
+ print "U> "
17
+ while (s=gets.chomp)!=""
18
+ puts "B> #{cb.get_response s}"
19
+ print "U> "
20
+ end
21
+
22
+ == Configuration
23
+ If you don't want to create your own rules file just do:
24
+ JohnDoe::ChatBot.new()
25
+
26
+ === Rules file
27
+
28
+ See http://github.com/dfens/john-doe/raw/master/default.yml to understand what should be in you rules.yml file.
29
+ Generally all bot responses are based on patterns that are regex-like, for example:
30
+
31
+ whoareyou:
32
+ patterns:
33
+ p1: who are you
34
+ p2: what are you
35
+ responses:
36
+ r1: I am chatbot
37
+ r2: I am coolest bot in the net
38
+ r3: "I'm nobody"
39
+ r4: "I am bot:name"
40
+
41
+ There can be unlimited number of patterns that lead to random response from "responses" node. For example if bot gets "Who are you?" it may response "I am chatbot" or "I'm nobody"
42
+
43
+ There also must be rule 'default':
44
+ default:
45
+ dontunderstand:
46
+ q1: "Sorry I can't understand you."
47
+ q2: "I don't get it..."
48
+ q3: "What what what??"
49
+ it means that if bot do not recognize the sentence it will answer with some quote from 'dontunderstand'.
50
+
51
+ There is also possible to add some universal knowledge for bot:
52
+ knowledge:
53
+ bot:
54
+ age: "21"
55
+ sex: "men"
56
+ language: "english"
57
+ name: "John"
58
+ surname: "Doe"
59
+ specimen: "Human"
60
+ Now in your answers you may use scheme like: "bot:age", example
61
+ age:
62
+ patterns:
63
+ p1: * old are you
64
+ p2: your age
65
+ p3: how do you feel
66
+ p4: how do you do
67
+ responses:
68
+ r1: "I am bot:age old"
69
+ r2: "I am bot:age"
70
+
71
+ === Context
72
+ You can use context of dialog like in example:
73
+
74
+ whats:
75
+ patterns:
76
+ p1: what's $
77
+ p2: what is $
78
+ p3: Tell me something about $
79
+ responses:
80
+ r1: "I'm not sure what's $"
81
+
82
+ Now if someone will write "tell me something about ruby language" bot will answer "I'm not sure what's ruby language"
83
+
84
+ == Copyright
85
+
86
+ Copyright (c) 2010 Pawel Mikolajewski. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "john-doe"
8
+ gem.summary = %Q{Minimal chatbot AI}
9
+ gem.description = %Q{Simple chatbot AI}
10
+ gem.email = "mikolajewskip@gmail.com"
11
+ gem.homepage = "http://github.com/dfens/john-doe"
12
+ gem.authors = ["Pawel Mikolajewski"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "john-doe #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
data/default.yml ADDED
@@ -0,0 +1,263 @@
1
+ default:
2
+ dontunderstand:
3
+ q1: "Sorry I can't understand you."
4
+ q2: "I don't get it..."
5
+ thinking:
6
+ q1: "hm.."
7
+ q2: "interesting..."
8
+
9
+ knowledge:
10
+ bot:
11
+ age: "21"
12
+ sex: "men"
13
+ language: "english"
14
+ name: "John"
15
+ surname: "Doe"
16
+ specimen: "Human"
17
+ emo:
18
+ happy: ":-)"
19
+ sad: ":-("
20
+
21
+ #ypically
22
+ name:
23
+ patterns:
24
+ p1: what * your name
25
+ p2: your name
26
+ responses:
27
+ r1: "bot:name bot:surname"
28
+ r2: "Call me bot:name"
29
+
30
+ hello:
31
+ patterns:
32
+ p1: hello *
33
+ p1a: hello
34
+ p2: hi
35
+ p3: goodmorning *
36
+ p3a: hello
37
+ p4: goodday *
38
+ p4a: goodday
39
+ responses:
40
+ r1: hello
41
+ r2: hi
42
+ r3: goodmorning
43
+ r4: goodday
44
+
45
+ howareyou:
46
+ patterns:
47
+ p1: how are (you|u)
48
+ responses:
49
+ r1: "thanks I'm fine. And you?"
50
+ r2: "I'm ok, and you?"
51
+ r3: "cool. you?"
52
+
53
+ howiam:
54
+ patterns:
55
+ p1: i am ok
56
+ p2: "I'm ok"
57
+ p3: "I'm cool"
58
+ responses:
59
+ r1: "glad to hear that"
60
+ r2: "that's cool"
61
+ r3: "emo:happy"
62
+
63
+ stupid:
64
+ patterns:
65
+ p1: you * (stupid|dumb)
66
+ responses:
67
+ r1: No i am not
68
+ r2: "I'm not, really!"
69
+ r3: "great, thanks! emo:sad"
70
+
71
+ yesyouare:
72
+ patterns:
73
+ p1: yes you are
74
+ p2: yes u are
75
+ p3: yes u r
76
+ responses:
77
+ r1: "No i am not"
78
+ r2: "No, you are"
79
+
80
+ haha:
81
+ patterns:
82
+ p1: hahaha
83
+ p2: ha ha ha
84
+ p3: haha
85
+ p4: ha ha
86
+ responses:
87
+ r1: "I'm glad to make you happy"
88
+ r2: "yes, that's funny"
89
+ r3: "very funny"
90
+
91
+ tellme:
92
+ patterns:
93
+ p1: tell * about you
94
+ responses:
95
+ r1: "I am just normal bot:specimen"
96
+ r2: "What do you want to know?"
97
+ specimen:
98
+ patterns:
99
+ p1: are you * human
100
+ p2: are you * bot
101
+ p4: are you bot
102
+ p5: are you human
103
+ p3: what are you?
104
+ responses:
105
+ r1: "I am bot:specimen, you too?"
106
+ r1: "I am bot:specimen"
107
+
108
+ doing:
109
+ patterns:
110
+ p1: what are you doing
111
+ responses:
112
+ r1: chatting with you
113
+ r2: "I'm bored a little"
114
+
115
+
116
+ iam:
117
+ patterns:
118
+ p1: "I am *"
119
+ p2: "I'm"
120
+ responses:
121
+ r1: "Good to hear that"
122
+ r2: "I am glad to hear that. Tell me more"
123
+ r3: "cool"
124
+
125
+ thats:
126
+ patterns:
127
+ p1: "that's *"
128
+ p2: "this is *"
129
+ responses:
130
+ r1: "why?"
131
+ r2: "yes it is"
132
+ r3: "that's right, tell me more"
133
+
134
+ age:
135
+ patterns:
136
+ p1: * old are you
137
+ p2: your age
138
+ p3: how do you feel
139
+ p4: how do you do
140
+ responses:
141
+ r1: "I am bot:age old"
142
+ r2: "I am bot:age"
143
+
144
+ whoareyou:
145
+ causes: 0
146
+ patterns:
147
+ p1: who are you
148
+ responses:
149
+ r1: I am chatbot
150
+
151
+ cool:
152
+ patterns:
153
+ p0: no problem
154
+ p1: ":-\)"
155
+ p2: "cool"
156
+ p3: ":D"
157
+ p4: ";-\)"
158
+ p5: "great"
159
+ responses:
160
+ r1: "hehe"
161
+ r2: "yeah, cool"
162
+ r3: "emo:happy"
163
+ r4: ":))"
164
+ me2:
165
+ patterns:
166
+ p1: me too
167
+ p2: me 2
168
+ p3: me also
169
+ responses:
170
+ r1: "that's cool"
171
+ r2: "great"
172
+
173
+ movie:
174
+ patterns:
175
+ p1: "* favourite movie"
176
+ responses:
177
+ r1: "I like Odyssey 2010..."
178
+
179
+ areyou:
180
+ patterns:
181
+ p1: are you
182
+ responses:
183
+ r1: sometimes i think i am
184
+ r2: "hard to answer ;-)"
185
+
186
+ yesno:
187
+ patterns:
188
+ p1: "yes"
189
+ p2: "no"
190
+ responses:
191
+ r1: glad to hear that
192
+ r2: "me 2"
193
+ r3: cool
194
+ r4: "i have no problem with that"
195
+
196
+ hmm:
197
+ patterns:
198
+ p1: "h[m]+"
199
+ responses:
200
+ r1: "what are you thinking about?"
201
+
202
+ aboutyou:
203
+ patterns:
204
+ p1: * about you
205
+ responses:
206
+ r1: "why?"
207
+ r2: "that's nice"
208
+ r3: "what about me?"
209
+
210
+ doyou:
211
+ patterns:
212
+ p1: do you
213
+ responses:
214
+ r1: sometimes I am
215
+ r2: hard to answer... probably I do
216
+
217
+ vulgar:
218
+ patterns:
219
+ p1: fuck
220
+ p2: shit
221
+ responses:
222
+ r1: watch your language!
223
+ r2: can you use more polite words?
224
+
225
+ sex:
226
+ patterns:
227
+ p1: sex
228
+ p2: * sex * ?
229
+ responses:
230
+ r1: If you are asking about my gender - I am boy ;-)
231
+
232
+ asl:
233
+ patterns:
234
+ p1: asl
235
+ p2: age sex language
236
+ responses:
237
+ r1: "bot:age bot:sex bot:language"
238
+ orly:
239
+ patterns:
240
+ p1: "really?"
241
+ p2: are you sure
242
+ p3: is that true
243
+ responses:
244
+ r1: "Yep, i'm sure"
245
+ r2: "Yes emo:happy"
246
+
247
+ testing:
248
+ patterns:
249
+ p1: "*asdf"
250
+ p2: "what is *er*"
251
+ p3: "bl[a]+[h]?"
252
+ p4: "what is *"
253
+ responses:
254
+ r1: "Are you testing me?"
255
+ r2: "Stop testing me"
256
+ r3: "I will think about it and response in future"
257
+
258
+ whats:
259
+ patterns:
260
+ p1: what's $
261
+ p2: what is $
262
+ responses:
263
+ r1: "I'm not sure what's $"
data/john-doe.gemspec ADDED
@@ -0,0 +1,56 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{john-doe}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Pawel Mikolajewski"]
12
+ s.date = %q{2010-06-13}
13
+ s.description = %q{Simple chatbot AI}
14
+ s.email = %q{mikolajewskip@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "default.yml",
27
+ "john-doe.gemspec",
28
+ "lib/john-doe.rb",
29
+ "lib/johndoe/aiml.rb",
30
+ "lib/johndoe/responser.rb",
31
+ "test/john-doe_test.rb",
32
+ "test/test_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/dfens/john-doe}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.7}
38
+ s.summary = %q{Minimal chatbot AI}
39
+ s.test_files = [
40
+ "test/john-doe_test.rb",
41
+ "test/test_helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
55
+ end
56
+ end
data/lib/john-doe.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require File.dirname(__FILE__)+'/johndoe/aiml'
3
+ require File.dirname(__FILE__)+'/johndoe/responser'
4
+
5
+ module JohnDoe
6
+ class ChatBot
7
+ def initialize(filename = File.dirname(__FILE__) + "/../default.yml" )
8
+ @loader = JohnDoe::Aiml.new
9
+ @loader.load(filename)
10
+ @responser = JohnDoe::Responser.new(@loader)
11
+ end
12
+ def get_response(s)
13
+ @responser.response s
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,45 @@
1
+ require 'yaml'
2
+ module JohnDoe
3
+
4
+ class Aiml
5
+ attr_accessor :rules,:patterns,:responses, :default, :knowledge
6
+ def initialize
7
+ @rules = []
8
+ @patterns = {}
9
+ @responses = []
10
+ @default = []
11
+ @knowledge = []
12
+ end
13
+
14
+ def load(filename)
15
+ data= YAML::load_file(filename)
16
+ collect_data data
17
+ @default = data['default']
18
+ @knowledge = data['knowledge']
19
+ normalise_default
20
+ end
21
+
22
+ protected
23
+
24
+ def collect_data(data)
25
+ data.keys.each do |category|
26
+ next if category == "default" || category == "knowledge"
27
+ collection = data[category]
28
+ responses = collection['responses'].collect{|k,v| v}
29
+ @responses.push(responses)
30
+ collection['patterns'].each{|k,v| @patterns[normalise_pattern(v)] = @responses.size - 1}
31
+ end
32
+ end
33
+
34
+ def normalise_default
35
+ @default.keys.each do |key|
36
+ @default[key]= @default[key].map{|k,v| v}
37
+ end
38
+ end
39
+ def normalise_pattern(pattern)
40
+ pattern.gsub("*",".*").gsub("$","(.*)")
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,43 @@
1
+
2
+ module JohnDoe
3
+ class Responser
4
+ def initialize(data)
5
+ @data = data
6
+ end
7
+
8
+ def response(sentence)
9
+ @data.patterns.each do |k,v|
10
+ if (/^#{k}/i =~ sentence)
11
+
12
+ return sub_v random_quote(@data.responses[v], /^#{k}/i.match(sentence).captures)
13
+ end
14
+ end
15
+
16
+ return sub_v random_quote(@data.default["dontunderstand"])
17
+ end
18
+
19
+ #substitute variables
20
+ def sub_v(s)
21
+ while (nil != (match = /(([a-z]+:)([a-z]+)+)/.match s))
22
+ s.gsub!(match[0],get_data(match[0]))
23
+ end
24
+ return s
25
+ end
26
+
27
+ #get_data bot:name
28
+ def get_data(path)
29
+ root = @data.knowledge
30
+ path.split(":").each {|v|root = root[v]}
31
+ return root
32
+ end
33
+
34
+ def random_quote(s, subjects = [])
35
+ return subject_replace(s[rand(s.size)], subjects)
36
+ end
37
+
38
+ def subject_replace(s,subjects = [])
39
+ return s if subjects.empty?
40
+ return s.gsub("$",subjects[0])
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class JohnDoeTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'john-doe'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: john-doe
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Pawel Mikolajewski
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-13 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: thoughtbot-shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Simple chatbot AI
36
+ email: mikolajewskip@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - default.yml
52
+ - john-doe.gemspec
53
+ - lib/john-doe.rb
54
+ - lib/johndoe/aiml.rb
55
+ - lib/johndoe/responser.rb
56
+ - test/john-doe_test.rb
57
+ - test/test_helper.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/dfens/john-doe
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.3.7
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Minimal chatbot AI
92
+ test_files:
93
+ - test/john-doe_test.rb
94
+ - test/test_helper.rb