Class: ProgramR::AimlParser

Inherits:
Object
  • Object
show all
Defined in:
lib/programr/aiml_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(learner, environment, history) ⇒ AimlParser

Returns a new instance of AimlParser.


8
9
10
11
12
# File 'lib/programr/aiml_parser.rb', line 8

def initialize learner, environment, history
  @learner = learner
  @history = history
  @environment = environment
end

Instance Method Details

#parse(aiml) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/programr/aiml_parser.rb', line 14

def parse aiml
  category             = nil
  openLabels           = []
  patternIsOpen        = false
  thatIsOpen           = false
  currentLanguage      = nil
  currentCondition     = nil
  currentConditionItem = nil
  currentSrai          = nil
  currentGender        = nil
  currentPerson        = nil
  currentPerson2       = nil
  currentTopic         = nil

  @parser = REXML::Parsers::SAX2Parser.new(aiml)

  @parser.listen(%w{ category }) do |uri, localname, qname, attributes|
    category = Category.new language: currentLanguage
    category.topic = currentTopic if currentTopic
  end

  @parser.listen(:end_element, %w{ category }) { @learner.learn(category) }

  @parser.listen(%w{ topicstar thatstar star }) do |uri, localname, qname, attributes|
    openLabels[-1].add(Star.new(localname, attributes, @history))
  end

### meta
  @parser.listen(:start_element, %w{ aiml }) do |uri, localname, qname, attributes|
    if localname == 'meta' and attributes["name"] == 'language'
      currentLanguage = attributes["content"].to_sym
    end
  end

  @parser.listen(:end_element, %w{ aiml }) do
    currentLanguage = nil
  end

### condition
  @parser.listen(%w{ condition }) do |uri, localname, qname, attributes|
    if attributes.has_key?('value')
      currentCondition = Condition.new attributes, @environment
    else
      currentCondition = ListCondition.new attributes
    end
    openLabels[-1].add(currentCondition)
    openLabels.push(currentCondition)
  end

  @parser.listen(%w{ random }) do |uri, localname, qname, attributes|
    currentCondition = Random.new
    openLabels[-1].add(currentCondition)
    openLabels.push(currentCondition)
  end

  @parser.listen(:characters, %w{ condition }) do |text|
    next if text =~ /^\s+$/
    currentCondition.add(text)
  end

  @parser.listen(:end_element, %w{ condition random }) do
    currentCondition = nil
    openLabels.pop
  end

  @parser.listen(%w{ li }) do |uri, localname, qname, attributes|
    next unless currentCondition
    currentConditionItem = ConditionItem.new attributes, currentCondition, @environment
    currentCondition.setListElement(currentConditionItem)
    openLabels.push currentConditionItem
  end

  @parser.listen(:characters, %w{ li }) do |text|
    next unless currentConditionItem
    next if text =~ /^\s+$/
    currentConditionItem.add text
  end

  @parser.listen(:end_element, %w{ li }) do
    next unless currentConditionItem
    currentConditionItem = nil
    openLabels.pop
  end### end condition


### get
  @parser.listen([/^get.*/, /^bot_*/, 'for_fun', /that$/, 'question']) do
                 |uri, localname, qname, attributes|
    unless openLabels.empty?
      openLabels[-1].add(GetTag.new localname, attributes, @environment)
    end
  end

  @parser.listen(%w{ bot name }) do |uri, localname, qname, attributes|
    if localname == 'bot'
      localname = 'bot_' + attributes['name']
    else
      localname = 'bot_name'
    end

    if patternIsOpen
      category.add_pattern(GetTag.new(localname, {}, @environment))
    elsif thatIsOpen
      category.add_that(GetTag.new(localname, {}, @environment))
    else
      openLabels[-1].add(GetTag.new(localname, {}, @environment))
    end
  end### end get


### set
  @parser.listen([/^set_*/, 'set']) do |uri, localname, qname, attributes|
    setObj = SetTag.new localname, attributes, @environment
    openLabels[-1].add(setObj)
    openLabels.push(setObj)
  end

  @parser.listen(:characters, [/^set_*/]) do |text|
    openLabels[-1].add(text)
  end

  @parser.listen(:end_element, [/^set_*/, 'set']) do
    openLabels.pop
  end### end set


### pattern
  @parser.listen(%w{ pattern }){ patternIsOpen = true }
  @parser.listen(:characters, %w{ pattern }) do |text|
    #TODO verify if case insensitive. Cross check with facade
    category.add_pattern(text.upcase)
  end
  @parser.listen(:end_element, %w{ pattern }){ patternIsOpen = false }#end pattern


### that
  @parser.listen(%w{ that }){ thatIsOpen = true }
  @parser.listen(:characters, %w{ that }){ |text| category.add_that(text) }
  @parser.listen(:end_element, %w{ that }){ thatIsOpen = false }### end that


### template
  @parser.listen(%w{ template }) do
    category.template = Template.new
    openLabels.push(category.template)
  end

  @parser.listen(:characters, %w{ template }) do |text|
    category.template.append(text)
  end

  @parser.listen(:end_element, %w{ template }) do
    openLabels.pop
  end### end template


  @parser.listen(%w{ input }) do |uri, localname, qname, attributes|
    category.template.add(Input.new(attributes, @history))
  end

### think
  @parser.listen(:start_element, %w{ think }) do
    openLabels[-1].add(Think.new('start'))
  end

  @parser.listen(:end_element, %w{ think }) do
    openLabels[-1].add(Think.new('end'))
  end###end think


  @parser.listen(:characters, %w{ uppercase }) do |text|
    openLabels[-1].add(text.upcase.gsub(/\s+/, ' '))
  end

  @parser.listen(:characters, %w{ lowercase }) do |text|
    openLabels[-1].add(text.downcase.gsub(/\s+/, ' '))
  end

  @parser.listen(:characters, %w{ formal }) do |text|
    text.gsub!(/(\w+)/){ $1.capitalize }
    openLabels[-1].add(text.gsub(/\s+/, ' '))
  end

  @parser.listen(:characters, %w{ sentence }) do |text|
    openLabels[-1].add(text.capitalize.gsub(/\s+/, ' '))
  end

  @parser.listen(%w{ date }) do
    openLabels[-1].add(Sys_Date.new)
  end

  @parser.listen(:characters, %w{ system }) do |text|
    openLabels[-1].add(Command.new(text))
  end

  @parser.listen(%w{ size }) do
    openLabels[-1].add(Size.new)
  end

### srai
  @parser.listen(%w{ sr }) do |uri, localname, qname, attributes|
    openLabels[-1].add(Srai.new(Star.new('star', {}, @history)))
  end

  @parser.listen(%w{ srai }) do |uri, localname, qname, attributes|
    currentSrai = Srai.new
    openLabels[-1].add(currentSrai)
    openLabels.push(currentSrai)
  end

  @parser.listen(:characters, %w{ srai }) do |text|
    currentSrai.add(text)
  end

  @parser.listen(:end_element, %w{ srai }) do
    currentSrai = nil
    openLabels.pop
  end### end srai


### gender
  @parser.listen(%w{ gender }) do |uri, localname, qname, attributes|
    currentGender = Gender.new
    openLabels[-1].add(currentGender)
    openLabels.push(currentGender)
  end

  @parser.listen(:characters, %w{ gender }) do |text|
    currentGender.add(text)
  end

  @parser.listen(:end_element, %w{ gender }) do
    currentGender = nil
    openLabels.pop
  end### end gender


### person
  @parser.listen(%w{ person }) do |uri, localname, qname, attributes|
    currentPerson = Person.new @environment
    openLabels[-1].add(currentPerson)
    openLabels.push(currentPerson)
  end

  @parser.listen(:characters, %w{ person }) do |text|
    currentPerson.add(text)
  end

  @parser.listen(:end_element, %w{ person }) do
    currentPerson = nil
    openLabels.pop
  end### end person


### person2
  @parser.listen(%w{ person2 }) do |uri, localname, qname, attributes|
    currentPerson2 = Person2.new
    openLabels[-1].add(currentPerson2)
    openLabels.push(currentPerson2)
  end

  @parser.listen(:characters, %w{ person2 }) do |text|
    currentPerson2.add(text)
  end

  @parser.listen(:end_element, %w{ person2 }) do
    currentPerson2 = nil
    openLabels.pop
  end### end perso2


### topic
  @parser.listen(%w{ topic }) do |uri, localname, qname, attributes|
    currentTopic = attributes['name']
  end

  @parser.listen(:end_element, %w{ topic }) do
    currentTopic = nil
  end### end topic


  @parser.parse
end