-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule_common.rb
391 lines (358 loc) · 9.19 KB
/
rule_common.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
$missing_value = :'?'
class AbstractAttribute
attr_reader :attr_name, :values
def initialize(attr_name)
@attr_name, @values = attr_name, ['abstract']
end
def to_n(v)
return nil if v.to_sym == $missing_value
v
end
def to_s(n)
return $missing_value.to_s if n == nil
n
end
def hash
@attr_name.hash
end
def eql?(o)
return nil unless o.kind_of?(AbstractAttribute)
@attr_name == o.attr_name
end
def ==(o)
return nil unless o.kind_of?(AbstractAttribute)
@attr_name == o.attr_name
end
def <=>(o)
return nil unless o.kind_of?(AbstractAttribute)
@attr_name <=> o.attr_name
end
end
class RealAttribute < AbstractAttribute
def initialize(attr_name)
@attr_name, @values = attr_name, ['real']
end
def to_n(v)
return nil if v.to_sym == $missing_value
v.to_f
end
def to_s(n)
return $missing_value.to_s if n == nil
n.to_s
end
end
class IntegerAttribute < AbstractAttribute
def initialize(attr_name)
@attr_name, @values = attr_name, ['integer']
end
def to_n(v)
return nil if v.to_sym == $missing_value
v.to_i
end
def to_s(n)
return $missing_value.to_s if n == nil
n.to_s
end
end
class StringAttribute < AbstractAttribute
def initialize(attr_name)
@attr_name, @values = attr_name, ['string']
end
def to_n(v)
return nil if v.to_sym == $missing_value
v.to_sym
end
def to_s(n)
return $missing_value.to_s if n == nil
n.to_s
end
end
class DiscreteAttribute < AbstractAttribute
def initialize(attr_name,values)
@attr_name, @values = attr_name, values
end
def to_n(v)
return nil if v.to_sym == $missing_value
(0...@values.size).find{|i| values[i] == v.to_sym}
end
def to_s(n)
return $missing_value.to_s if n == nil
values[n].to_s
end
end
class NominalFeature
include Comparable
attr_reader :attri, :value
def initialize(attri,value)
@attri, @value = attri, value
end
def match?(o)
o[attri] == @value
end
def hash
@attri.hash + @value.hash
end
def eql?(o)
return false unless o.instance_of?(NominalFeature)
@attri == o.attri && @value == o.value
end
def ==(o)
return false unless o.instance_of?(NominalFeature)
@attri == o.attri && @value == o.value
end
def <=>(o)
return nil unless o.instance_of?(NominalFeature)
if @attri == o.attri
return @value <=> o.value
else
return @attri <=> o.attri
end
end
def to_s
'[' + attri.attr_name.to_s + ' = ' + attri.to_s(value) + ']'
end
def is_compatible?(o)
return true unless @attri == o.attri
@value == o.value
end
end
class AtMostFeature
include Comparable
attr_reader :attri, :value
def initialize(attri,value)
@attri, @value = attri, value
end
def match?(o)
if o[attri] == nil then return false end
o[attri] <= @value
end
def hash
@attri.hash + @value.hash
end
def eql?(o)
return false unless o.instance_of?(AtMostFeature)
@attri == o.attri && @value == o.value
end
def ==(o)
return false unless o.instance_of?(AtMostFeature)
@attri == o.attri && @value == o.value
end
def <=>(o)
return nil unless o.instance_of?(AtMostFeature)
if @attri == o.attri
return @value <=> o.value
else
return @attri <=> o.attri
end
end
def to_s
'[' + attri.attr_name.to_s + ' <= ' + attri.to_s(value) + ']'
end
def is_compatible?(o)
return true unless @attri == o.attri && o.instance_of?(AtLeastFeature)
@value >= o.value
end
end
class AtLeastFeature
include Comparable
attr_reader :attri, :value
def initialize(attri,value)
@attri, @value = attri, value
end
def match?(o)
if o[attri] == nil then return false end
o[attri] >= @value
end
def hash
@attri.hash + @value.hash
end
def eql?(o)
return false unless o.instance_of?(AtLeastFeature)
@attri == o.attri && @value == o.value
end
def ==(o)
return false unless o.instance_of?(AtLeastFeature)
@attri == o.attri && @value == o.value
end
def <=>(o)
return nil unless o.instance_of?(AtLeastFeature)
if @attri == o.attri
return @value <=> o.value
else
return @attri <=> o.attri
end
end
def to_s
'[' + attri.attr_name.to_s + ' >= ' + attri.to_s(value) + ']'
end
def is_compatible?(o)
return true unless @attri == o.attri && o.instance_of?(AtMostFeature)
@value <= o.value
end
end
class Rule
attr_reader :body, :head, :coverage
def initialize(head,body,coverage)
@head, @body, @coverage = head, body, coverage
end
def match?(o)
body.all?{|f| f.match?(o)}
end
def hash
@head.hash + @body.hash
end
def eql?(o)
@head == o.head && @body == o.body
end
def ==(o)
@head == o.head && @body == o.body
end
def to_s
dec = head.attri
head.to_s + ' <-' + body.inject(''){|str,f| str + ' ' + f.to_s} + "\n\t" + coverage.to_a.inject("[ "){|str,c| str + dec.to_s(c[0].value) + '(' + c[1].to_s + ') '} + ']'
end
end
def read_attr(file_name)
file = File.open(file_name)
attributes = []
file.each{|l|
l.strip!
if l == '' then next end
items = l.split(':').map{|s| s.strip}
if items[1] == nil then p 'error in attr file: ' + l; exit(1) end
if items[1] == 'real'
a = RealAttribute.new(items[0].to_sym)
elsif items[1] == 'integer'
a = IntegerAttribute.new(items[0].to_sym)
elsif items[1] == 'string'
a = StringAttribute.new(items[0].to_sym)
else
values = items[1].split(/\s+/).map{|s| s.to_sym}
a = DiscreteAttribute.new(items[0].to_sym,values)
end
attributes << a
}
return attributes
end
def read_data(file_name)
file = File.open(file_name)
header = []
file.each{|l|
l.strip!
if l != ''
header = l.split(/\s+/).map{|s| s.to_sym}
break
end
}
if file.eof? then p 'error in the data file: EOF.'; exit(1) end
objects = []
file.each{|l|
l.strip!
if l == '' then next end
items = l.split(/\s+/)
if items.size == header.size
objects << items
else
p 'error in the data file: row.size != header.size: ' + l; exit(1)
end
}
file.close
if objects.size == 0 then p 'error in the data file: no objects.'; exit(1) end
return header,objects
end
def read_cond(file_name)
ordinals = []
conditions = []
decision = ''
file = File.open(file_name)
file.each{|l|
l.strip!
if l == '' then next end
item = l.split(/\s+/)
if item[0].downcase == 'ordinal'
ordinals += item[1..-1].map{|s| s.to_sym}
elsif item[0].downcase == 'condition'
conditions += item[1..-1].map{|s| s.to_sym}
elsif item[0].downcase == 'decision'
if decision != ''
p 'errro in the info file: multiple decision.'; exit(1)
else
decision = item[1].to_sym
end
end
}
file.close
if decision == '' then p 'error in the info file: decision is not specified.'; exti(1) end
return conditions,decision,ordinals
end
def to_h_attributes(attributes,header)
h_attributes = header.map{|h| attributes.find{|a| a.attr_name == h}}
if h_attributes.include?(nil) then p 'there are unknown attributes in data.' + "\n"; exit(1) end
return h_attributes
end
def to_numeric(h_attributes,objects)
return objects.map{|o| (0...o.size).map{|i| [h_attributes[i],h_attributes[i].to_n(o[i])]}.to_h}
end
def read_rule(file_name,attributes)
heads = []
bodys = []
covs = []
file = File.open(file_name)
while !file.eof?
l = file.readline
l.strip!
if l == '' then next end
item = l.split('<-')
head_str = item[0].strip
f = head_str.slice!(/\[[^\[^\]]+\]/)
f = f.gsub(/[\[\]]/,'').strip.split('=').map{|s| s.strip}
h_attr = attributes.find{|a| a.attr_name == f[0].to_sym}
head = NominalFeature.new(h_attr,h_attr.to_n(f[1]))
body = []
if item[1] != nil
body_str = item[1].strip
while body_str != ''
f = body_str.slice!(/\[[^\[^\]]+\]/)
f = f.gsub(/[\[\]]/,'')
if f.include?('<=')
f = f.strip.split('<=').map{|s| s.strip}
h_attr = attributes.find{|a| a.attr_name == f[0].to_sym}
body << AtMostFeature.new(h_attr,h_attr.to_n(f[1]))
elsif f.include?('>=')
f = f.strip.split('>=').map{|s| s.strip}
h_attr = attributes.find{|a| a.attr_name == f[0].to_sym}
body << AtLeastFeature.new(h_attr,h_attr.to_n(f[1]))
else
f = f.strip.split('=').map{|s| s.strip}
h_attr = attributes.find{|a| a.attr_name == f[0].to_sym}
body << NominalFeature.new(h_attr,h_attr.to_n(f[1]))
end
body_str.strip!
end
end
l = file.readline
str = l.strip!
cov = str.slice!(/\[[^\[^\]]+\]/)
cov = cov.gsub(/[\[\]]/,'').strip.split(/\s+/).map{|s| s.strip}
heads << head
bodys << body
covs << cov
end
file.close
labels = heads.uniq.sort
coverages = []
(0...bodys.size).each{|i|
cov = covs[i]
coverage = Hash.new(0)
cov.each{|c|
c =~ /([\w\-]+)\((\d+)\)/
lm = Regexp.last_match
l = labels.find{|ll| ll.value == ll.attri.to_n(lm[1])}
if l != nil then coverage[l] = lm[2].to_i end
}
coverages << coverage
}
rules = []
(0...bodys.size).each{|i| rules << Rule.new(heads[i],bodys[i],coverages[i])}
return rules,labels
end