programr 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +64 -0
- data/Rakefile +2 -0
- data/conf/readOnlyTags.yaml +37 -0
- data/lib/programr.rb +5 -0
- data/lib/programr/aiml_elements.rb +352 -0
- data/lib/programr/aiml_parser.rb +269 -0
- data/lib/programr/environment.rb +75 -0
- data/lib/programr/facade.rb +65 -0
- data/lib/programr/graph_master.rb +110 -0
- data/lib/programr/history.rb +82 -0
- data/lib/programr/utils.rb +30 -0
- data/lib/programr/version.rb +3 -0
- data/programr.gemspec +17 -0
- data/test/data/1.aiml +125 -0
- data/test/data/facade.aiml +233 -0
- data/test/data/mixed/dir/badfile +1 -0
- data/test/data/mixed/dir/in_dir.aiml +9 -0
- data/test/data/mixed/dir/subdir/in_sub_dir.aiml +17 -0
- data/test/data/mixed/single.aiml +9 -0
- data/test/data/test.aiml +44 -0
- data/test/test_facade.rb +67 -0
- data/test/test_utils.rb +18 -0
- metadata +81 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Robert Whitney
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Programr
|
2
|
+
|
3
|
+
### Aside:
|
4
|
+
|
5
|
+
This is just a simple port of http://aiml-programr.rubyforge.org/ (authored in 2007), to something
|
6
|
+
a tiny bit more usable. No new code really, I just sorta took the necessary parts to turn it into a gem for curiosities sake.
|
7
|
+
|
8
|
+
AIML itself seems pretty simple, and weak as far as pattern matching goes, but the ALICE bot has won the Loebner prize, so there's that.
|
9
|
+
|
10
|
+
### About:
|
11
|
+
|
12
|
+
ProgramR is a Ruby implementation of an interpreter for the Artificial Intelligence Markup Language (AIML) based on the work of Dr. Wallace and defined by the Alicebot and AIML Architecture Committee of the A.L.I.C.E. AI Foundation http://alicebot.org
|
13
|
+
|
14
|
+
### Authors
|
15
|
+
|
16
|
+
Mauro Cicio, Nicholas H.Tollervey and Ben Minton
|
17
|
+
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
gem 'programr'
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install programr
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
```ruby
|
35
|
+
#programr_test.rb
|
36
|
+
|
37
|
+
require 'bundler'
|
38
|
+
Bundler.setup :default
|
39
|
+
|
40
|
+
require 'programr'
|
41
|
+
|
42
|
+
if ARGV.empty?
|
43
|
+
puts 'Please pass a list of AIMLs and/or directories as parameters'
|
44
|
+
puts 'Usage: ruby programr_test.rb {aimlfile|dir}[{aimlfile|dir}]...'
|
45
|
+
exit
|
46
|
+
end
|
47
|
+
|
48
|
+
robot = ProgramR::Facade.new
|
49
|
+
robot.learn(ARGV)
|
50
|
+
|
51
|
+
while true
|
52
|
+
print '>> '
|
53
|
+
s = STDIN.gets.chomp
|
54
|
+
reaction = robot.get_reaction(s)
|
55
|
+
STDOUT.puts "<< #{reaction}"
|
56
|
+
end
|
57
|
+
```
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
1. Fork it
|
61
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
62
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
63
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
64
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
---
|
2
|
+
'bot_name': 'infobot'
|
3
|
+
'bot_location': 'vancouver'
|
4
|
+
'bot_gender': 'male'
|
5
|
+
'bot_birthday': 'December 19, 2008'
|
6
|
+
'bot_master': 'infil00p'
|
7
|
+
'bot_birthplace': 'vancouver'
|
8
|
+
'bot_boyfriend': 'no'
|
9
|
+
'bot_favoritefood': 'mathematics'
|
10
|
+
'bot_favoritemovie': 'Wargames'
|
11
|
+
'bot_favoriteband': 'Flight of the Conchords'
|
12
|
+
'bot_favoritebook': 'I, Robot'
|
13
|
+
'bot_favoritecolor': 'red'
|
14
|
+
'bot_favoritesong': 'The Humans are Dead'
|
15
|
+
'bot_friends': 'alice'
|
16
|
+
'bot_girlfriend': 'no'
|
17
|
+
'bot_wear': 'casual'
|
18
|
+
'bot_sign': 'none'
|
19
|
+
'bot_looklike': 'handsome'
|
20
|
+
'bot_music': 'rock'
|
21
|
+
'bot_talkabout': 'science'
|
22
|
+
'for_fun': 'read'
|
23
|
+
'getversion': '01'
|
24
|
+
'getsize': '0'
|
25
|
+
'question':
|
26
|
+
- "are you never tired to do the same things every day?"
|
27
|
+
- "Say something nice to me"
|
28
|
+
'name': "client"
|
29
|
+
'topic': 'general'
|
30
|
+
'ip': 'localhost'
|
31
|
+
'it': 'it'
|
32
|
+
'they': 'they'
|
33
|
+
'we': 'we'
|
34
|
+
'gender': 'male'
|
35
|
+
'location': 'canada'
|
36
|
+
'age': '40'
|
37
|
+
'has': 'nothing'
|
data/lib/programr.rb
ADDED
@@ -0,0 +1,352 @@
|
|
1
|
+
require 'programr/environment'
|
2
|
+
|
3
|
+
module ProgramR
|
4
|
+
|
5
|
+
class Category
|
6
|
+
attr_accessor :template, :that, :topic
|
7
|
+
|
8
|
+
@@cardinality = 0
|
9
|
+
def initialize
|
10
|
+
@@cardinality += 1
|
11
|
+
@pattern = []
|
12
|
+
@that = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def Category.cardinality; @@cardinality end
|
16
|
+
|
17
|
+
def add_pattern(anObj)
|
18
|
+
@pattern.push(anObj)
|
19
|
+
end
|
20
|
+
|
21
|
+
def add_that(anObj)
|
22
|
+
@that.push(anObj)
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_pattern
|
26
|
+
res = ''
|
27
|
+
@pattern.each{|tocken|
|
28
|
+
res += tocken.to_s
|
29
|
+
}
|
30
|
+
return res.split(/\s+/)
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_that
|
34
|
+
res = ''
|
35
|
+
@that.each{|tocken|
|
36
|
+
res += tocken.to_s
|
37
|
+
}
|
38
|
+
return res.split(/\s+/)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class Template
|
43
|
+
attr_accessor :value
|
44
|
+
|
45
|
+
def initialize() @value = [] end
|
46
|
+
def add(anObj) @value << anObj end
|
47
|
+
def append(aString)
|
48
|
+
@value << aString.gsub(/\s+/, ' ')
|
49
|
+
end
|
50
|
+
|
51
|
+
def inspect
|
52
|
+
res = ''
|
53
|
+
@value.each{|tocken| res += tocken.inspect }
|
54
|
+
res
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class Random
|
59
|
+
@@environment = Environment.new
|
60
|
+
|
61
|
+
def initialize; @conditions = [] end
|
62
|
+
def setListElement(someAttributes)
|
63
|
+
@conditions.push([])
|
64
|
+
end
|
65
|
+
|
66
|
+
def add(aBody)
|
67
|
+
@conditions[-1].push(aBody)
|
68
|
+
end
|
69
|
+
|
70
|
+
def execute
|
71
|
+
res = ''
|
72
|
+
@@environment.getRandom(@conditions).each{|tocken|
|
73
|
+
res += tocken.to_s
|
74
|
+
}
|
75
|
+
return res.strip
|
76
|
+
end
|
77
|
+
alias to_s execute
|
78
|
+
def inspect(); "random -> #{execute}" end
|
79
|
+
end
|
80
|
+
|
81
|
+
class Condition
|
82
|
+
#se c'e' * nel value?
|
83
|
+
@@environment = Environment.new
|
84
|
+
|
85
|
+
def initialize(someAttributes)
|
86
|
+
@conditions = {}
|
87
|
+
@property = someAttributes['name']
|
88
|
+
@currentCondition = someAttributes['value'].sub('*','.*')
|
89
|
+
end
|
90
|
+
|
91
|
+
def add(aBody)
|
92
|
+
unless @conditions[@currentCondition]
|
93
|
+
@conditions[@currentCondition] = []
|
94
|
+
end
|
95
|
+
@conditions[@currentCondition].push(aBody)
|
96
|
+
end
|
97
|
+
|
98
|
+
def setListElement(someAttributes)
|
99
|
+
@property = someAttributes['name'] if(someAttributes.key?('name'))
|
100
|
+
@currentCondition = '_default'
|
101
|
+
if(someAttributes.key?('value'))
|
102
|
+
@currentCondition = someAttributes['value'].sub('*','.*')
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def execute
|
107
|
+
return '' unless(@@environment.get(@property) =~ /^#{@currentCondition}$/)
|
108
|
+
res = ''
|
109
|
+
@conditions[@currentCondition].each{|tocken|
|
110
|
+
res += tocken.to_s
|
111
|
+
}
|
112
|
+
return res.strip
|
113
|
+
end
|
114
|
+
alias to_s execute
|
115
|
+
def inspect()
|
116
|
+
"condition -> #{execute}"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
class ListCondition < Condition
|
121
|
+
def initialize(someAttributes)
|
122
|
+
@conditions = {}
|
123
|
+
@property = someAttributes['name'] if(someAttributes.key?('name'))
|
124
|
+
end
|
125
|
+
|
126
|
+
def execute
|
127
|
+
@conditions.keys.each do |key|
|
128
|
+
if(@@environment.get(@property) == key)
|
129
|
+
res = ''
|
130
|
+
@conditions[key].each{|tocken|
|
131
|
+
res += tocken.to_s
|
132
|
+
}
|
133
|
+
return res.strip
|
134
|
+
end
|
135
|
+
end
|
136
|
+
return ''
|
137
|
+
end
|
138
|
+
alias to_s execute
|
139
|
+
end
|
140
|
+
|
141
|
+
class SetTag
|
142
|
+
@@environment = Environment.new
|
143
|
+
|
144
|
+
def initialize(aLocalname,attributes)
|
145
|
+
if(attributes.empty?)
|
146
|
+
@localname = aLocalname.sub(/^set_/,'')
|
147
|
+
else
|
148
|
+
@localname = attributes['name']
|
149
|
+
end
|
150
|
+
@value = []
|
151
|
+
end
|
152
|
+
|
153
|
+
def add(aBody)
|
154
|
+
@value.push(aBody)
|
155
|
+
end
|
156
|
+
|
157
|
+
def execute
|
158
|
+
res = ''
|
159
|
+
@value.each{|tocken|
|
160
|
+
res += tocken.to_s
|
161
|
+
}
|
162
|
+
@@environment.set(@localname,res.strip)
|
163
|
+
end
|
164
|
+
alias to_s execute
|
165
|
+
def inspect(); "set tag #{@localname} -> #{execute}" end
|
166
|
+
end
|
167
|
+
|
168
|
+
class Input
|
169
|
+
@@environment = Environment.new
|
170
|
+
|
171
|
+
def initialize(someAttributes)
|
172
|
+
@index = 1
|
173
|
+
@index = someAttributes['index'].to_i if(someAttributes.key?('index'))
|
174
|
+
end
|
175
|
+
|
176
|
+
def execute
|
177
|
+
@@environment.getStimula(@index)
|
178
|
+
end
|
179
|
+
alias to_s execute
|
180
|
+
def inspect(); "input -> #{@@environment.getStimula(@index)}" end
|
181
|
+
end
|
182
|
+
|
183
|
+
class Star
|
184
|
+
@@environment = Environment.new
|
185
|
+
|
186
|
+
def initialize(aStarName,someAttributes)
|
187
|
+
@star = aStarName
|
188
|
+
@index = 0
|
189
|
+
@index = someAttributes['index'].to_i-1 unless(someAttributes.empty?)
|
190
|
+
end
|
191
|
+
|
192
|
+
def execute
|
193
|
+
@@environment.send(@star,@index)
|
194
|
+
end
|
195
|
+
alias to_s execute
|
196
|
+
def inspect()
|
197
|
+
return "#{@star} #{@index} -> #{@@environment.send(@star,@index)}"
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
class ReadOnlyTag
|
202
|
+
@@environment = Environment.new
|
203
|
+
|
204
|
+
def initialize(aLocalname,someAttributes)
|
205
|
+
@localname = aLocalname.sub(/^get_/,'')
|
206
|
+
if(someAttributes.key?('index') && @localname == 'that')
|
207
|
+
@localname = 'justbeforethat' if someAttributes['index'] == '2,1'
|
208
|
+
someAttributes = {}
|
209
|
+
end
|
210
|
+
@attributed = someAttributes
|
211
|
+
end
|
212
|
+
|
213
|
+
def execute
|
214
|
+
return @@environment.get(@localname) if(@attributed.empty?)
|
215
|
+
@@environment.get(@attributed['name'])
|
216
|
+
end
|
217
|
+
alias to_s execute
|
218
|
+
def inspect()
|
219
|
+
return "roTag #{@localname} -> #{execute}"
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
class Think
|
224
|
+
def initialize(aStatus); @status = aStatus end
|
225
|
+
|
226
|
+
def execute; @status end
|
227
|
+
alias to_s execute
|
228
|
+
def inspect(); "think status -> #{@status}" end
|
229
|
+
end
|
230
|
+
|
231
|
+
class Size
|
232
|
+
def execute; Category.cardinality.to_s end
|
233
|
+
alias to_s execute
|
234
|
+
def inspect(); "size -> #{Category.cardinality.to_s}" end
|
235
|
+
end
|
236
|
+
|
237
|
+
class Sys_Date
|
238
|
+
def execute; Date.today.to_s end
|
239
|
+
alias to_s execute
|
240
|
+
def inspect(); "date -> #{Date.today.to_s}" end
|
241
|
+
end
|
242
|
+
|
243
|
+
class Srai
|
244
|
+
def initialize(anObj=nil)
|
245
|
+
@pattern = []
|
246
|
+
@pattern.push(anObj) if(anObj)
|
247
|
+
end
|
248
|
+
def add(anObj); @pattern.push(anObj) end
|
249
|
+
def pattern
|
250
|
+
res = ''
|
251
|
+
@pattern.each{|tocken|
|
252
|
+
res += tocken.to_s
|
253
|
+
}
|
254
|
+
return res.strip
|
255
|
+
end
|
256
|
+
def inspect(); "srai -> #{pattern}" end
|
257
|
+
end
|
258
|
+
|
259
|
+
class Person2
|
260
|
+
@@environment = Environment.new
|
261
|
+
@@swap = {'me' => 'you', 'you' => 'me'}
|
262
|
+
def initialize; @sentence = [] end
|
263
|
+
def add(anObj); @sentence.push(anObj) end
|
264
|
+
def execute
|
265
|
+
res = ''
|
266
|
+
@sentence.each{|tocken|
|
267
|
+
res += tocken.to_s
|
268
|
+
}
|
269
|
+
gender = @@environment.get('gender')
|
270
|
+
res.gsub(/\b((with|to|of|for|give|gave|giving) (you|me)|you|i)\b/i){
|
271
|
+
if($3)
|
272
|
+
$2.downcase+' '+@@swap[$3.downcase]
|
273
|
+
elsif($1.downcase == 'you')
|
274
|
+
'i'
|
275
|
+
elsif($1.downcase == 'i')
|
276
|
+
'you'
|
277
|
+
end
|
278
|
+
}
|
279
|
+
end
|
280
|
+
alias to_s execute
|
281
|
+
def inspect(); "person2 -> #{execute}" end
|
282
|
+
end
|
283
|
+
|
284
|
+
class Person
|
285
|
+
@@environment = Environment.new
|
286
|
+
@@swap = {'male' => {'me' => 'him',
|
287
|
+
'my' => 'his',
|
288
|
+
'myself' => 'himself',
|
289
|
+
'mine' => 'his',
|
290
|
+
'i' => 'he',
|
291
|
+
'he' => 'i',
|
292
|
+
'she' => 'i'},
|
293
|
+
'female' => {'me' => 'her',
|
294
|
+
'my' => 'her',
|
295
|
+
'myself' => 'herself',
|
296
|
+
'mine' => 'hers',
|
297
|
+
'i' => 'she',
|
298
|
+
'he' => 'i',
|
299
|
+
'she' => 'i'}}
|
300
|
+
|
301
|
+
def initialize; @sentence = [] end
|
302
|
+
def add(anObj); @sentence.push(anObj) end
|
303
|
+
def execute
|
304
|
+
res = ''
|
305
|
+
@sentence.each{|tocken|
|
306
|
+
res += tocken.to_s
|
307
|
+
}
|
308
|
+
gender = @@environment.get('gender')
|
309
|
+
res.gsub(/\b(she|he|i|me|my|myself|mine)\b/i){
|
310
|
+
@@swap[gender][$1.downcase]
|
311
|
+
}
|
312
|
+
end
|
313
|
+
alias to_s execute
|
314
|
+
def inspect(); "person-> #{execute}" end
|
315
|
+
end
|
316
|
+
|
317
|
+
class Gender
|
318
|
+
def initialize; @sentence = [] end
|
319
|
+
def add(anObj); @sentence.push(anObj) end
|
320
|
+
def execute
|
321
|
+
res = ''
|
322
|
+
@sentence.each{|tocken|
|
323
|
+
res += tocken.to_s
|
324
|
+
}
|
325
|
+
res.gsub(/\b(she|he|him|his|(for|with|on|in|to) her|her)\b/i){
|
326
|
+
|
327
|
+
pronoun = $1.downcase
|
328
|
+
if(pronoun == 'she')
|
329
|
+
'he'
|
330
|
+
elsif(pronoun == 'he')
|
331
|
+
'she'
|
332
|
+
elsif(pronoun == 'him' || pronoun == 'his')
|
333
|
+
'her'
|
334
|
+
elsif(pronoun == 'her')
|
335
|
+
'his'
|
336
|
+
else
|
337
|
+
$2.downcase + ' ' + 'him'
|
338
|
+
end
|
339
|
+
}
|
340
|
+
end
|
341
|
+
alias to_s execute
|
342
|
+
def inspect(); "gender -> #{execute}" end
|
343
|
+
end
|
344
|
+
|
345
|
+
class Command
|
346
|
+
def initialize(text) @command = text end
|
347
|
+
def execute; `#{@command}` end
|
348
|
+
alias to_s execute
|
349
|
+
def inspect() "cmd -> #{@command}" end
|
350
|
+
end
|
351
|
+
|
352
|
+
end #ProgramR
|