-
Notifications
You must be signed in to change notification settings - Fork 1
/
attdsc.metta
280 lines (246 loc) · 9.64 KB
/
attdsc.metta
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
; (convert-to-metta-file attdsc $_363976 attdsc/attdsc.pl attdsc/attdsc.metta)
!(op 300 xfx <==)
; /******************************************************************/
; /* ATTDSC.PRO Last Modification: Fri Jan 14 19:22:05 1994 */
; /* Bratko's simple algorithm for attributional descriptions. */
; /******************************************************************/
; ; ; Copyright (c) 1990 Ivan Bratko ;
; /******************************************************************/
; /* reimpl. by : Thomas Hoppe */
; /* Mommsenstr. 50 */
; /* D-10629 Berlin */
; /* F.R.G. */
; /* E-Mail: [email protected] */
; /* 1991 */
; /* */
; /* reference : Chapter 18, */
; /* Ivan Bratko */
; /* MeTTa */
; /* 2nd extend edition */
; /* Addison-Wesley, 1990 */
; /* */
; /* call : learn(+ClassName) */
; /* */
; /* argument : ClassName = name of the class, whose descrip- */
; /* tion should be learned */
; /* */
; /******************************************************************/
; /* The representation used: */
; /* Attribute = attributes(AttributeName,ListOfPossibleValues) */
; /* Example = example(ClassName,ListOfAttributeValues) */
; /* AttributeValues = Attribute = Value */
; /* */
; /* learn induces class descriptions of the form: */
; /* */
; /* Class <== ListOfAttributeValues */
; /* */
; /* with the meaning: An object is an Class, if it fulfills the */
; /* class description given by ListOfAttributeValues. */
; /* */
; /******************************************************************/
; ; TH Sat May 29 23:45:01 1993 - Made some minor modifications
!(dynamic (/ <== 2))
; /******************************************************************/
; /* Some M-, C- and YAP-MeTTa dependent declarations. */
; /******************************************************************/
(= (learn $Class)
( (bagof
(example $ClassX $Obj)
(example $ClassX $Obj) $Examples)
(learn $Examples $Class $Description) nl
(write $Class)
(write <==) nl
(writelist $Description)
(add-atom &self
(<== $Class $Description))))
; /******************************************************************/
; /* */
; /* call : learn(+Class) */
; /* */
; /* arguments : Class = class name to be learned */
; /* */
; /* side effects: asserts classification rules in the database */
; /* */
; /******************************************************************/
; /* 'learn' collects all examples into a list, constructs and out- */
; /* puts a description for Class, and asserts the corresponding */
; /* rule about Class. */
; /******************************************************************/
(= (learn $Examples $Class Nil)
(not (member (example $Class $_) $Examples)))
; /******************************************************************/
; /* */
; /* call : learn(+Examples,+Class,-Description) */
; /* */
; /* arguments : Examples = list of all available examples */
; /* Class = class name to be learned */
; /* Description = induced concept description */
; /* */
; /******************************************************************/
; /* Description covers exactly the positive examples of Class in */
; /* list Examples. If no examples exist for Class, an empty De- */
; /* scription is returned. After a conjunction was learned the */
; /* examples matching the conjunction are removed from Examples */
; /* and for remaining RestExamples a further conjunct is learned. */
; /******************************************************************/
(= (learn $Examples $Class (Cons $Conj $Conjs))
(learn-conj $Examples $Class $Conj)
(remove $Examples $Conj $RestExamples)
(learn $RestExamples $Class $Conjs))
(= (learn-conj $Examples $Class Nil)
(not (, (member (example $ClassX $_) $Examples) (not (== $ClassX $Class))))
(set-det))
; /******************************************************************/
; /* */
; /* call : learn_conj(+Examples,+Class,-Conj) */
; /* */
; /* arguments : Examples = list of all available examples */
; /* Class = class name to be learned */
; /* Conj = list of attribute/value pairs */
; /* */
; /******************************************************************/
; /* Conj is a list of attribute/value pairs are satisfied by some */
; /* examples of class Class and no other classes. If there is no */
; /* other Example of a different class covered, the empty Conj is */
; /* returned. Otherwise, we choose the best attribute/value pair */
; /* according to the evaluation criterion used in 'score' and */
; /* filter out all examples, that cover this attribute/value pair. */
; /******************************************************************/
(= (learn-conj $Examples $Class (Cons $Cond $Conds))
(choose-cond $Examples $Class $Cond)
(filter $Examples
(:: $Cond) $Examples1)
(learn-conj $Examples1 $Class $Conds))
(= (choose-cond $Examples $Class $AttVal)
(findall
(/ $AV $Score)
(score $Examples $Class $AV $Score) $AVs)
(best $AVs $AttVal))
(= (best ((/ $AttVal $_)) $AttVal) True)
(= (best (Cons (/ $AV0 $S0) (Cons (/ $AV1 $S1) $AVSlist)) $AttVal)
(or
(,
(> $S1 $S0)
(set-det)
(best
(Cons
(/ $AV1 $S1) $AVSlist) $AttVal))
(best
(Cons
(/ $AV0 $S0) $AVSlist) $AttVal)))
(= (score $Examples $Class $AttVal $Score)
(candidate $Examples $Class $AttVal)
(filter $Examples
(:: $AttVal) $Examples1)
(length $Examples1 $N1)
(count-pos $Examples1 $Class $NPos1)
(> $NPos1 0)
(is $Score
(-
(* 2 $NPos1) $N1)))
; /******************************************************************/
; /* */
; /* call : score(+Examples,+Class,-AttVal,-Score) */
; /* */
; /* arguments : Examples = list of all available examples */
; /* Class = class name to be learned */
; /* AttVal = chosen attribute/value pair */
; /* Score = value of AttVal */
; /* */
; /******************************************************************/
; /* 'score' determines an suitable attribute/value pair, determines*/
; /* how many examples are covered and computes a value for the */
; /* chosen attribute/value pair. */
; /* Remark: In the current implementation at least one example has */
; /* to be covered. Thus, in some cases it can happen, that rules */
; /* are generated, which cover exactly one example. Hence, we do */
; /* not benefite from learning. This can be changed by requiring */
; /* that at least two examples should be covered. */
; /******************************************************************/
(= (candidate $Examples $Class (= $Att $Val))
( (==
(=
(attribute $Att $Values) true)
(get-atoms &self))
(member $Val $Values)
(suitable
(= $Att $Val) $Examples $Class)))
(= (suitable $AttVal $Examples $Class)
(member
(example $ClassX $ObjX) $Examples)
(not (== $ClassX $Class))
(not (satisfy $ObjX (:: $AttVal)))
(set-det))
; ; atleast one neg. example must not match AttVal
(= (count-pos () $_ 0) True)
; /******************************************************************/
; /* */
; /* call : count_pos(+Examples,+Class,-N) */
; /* */
; /* arguments : Examples = list of all available examples */
; /* Class = class name to be learned */
; /* N = number of covered examples */
; /* */
; /******************************************************************/
; /* N is the number of positive examples of Class */
; /******************************************************************/
(= (count-pos (Cons (example $ClassX $_) $Examples) $Class $N)
(count-pos $Examples $Class $N1)
(or
(,
(= $ClassX $Class)
(set-det)
(is $N
(+ $N1 1)))
(= $N $N1)))
(= (filter $Examples $Cond $Examples1)
(findall
(example $Class $Obj)
(,
(member
(example $Class $Obj) $Examples)
(satisfy $Obj $Cond)) $Examples1))
; /******************************************************************/
; /* */
; /* call : filter(+Examples,+Cond,-Examples1) */
; /* */
; /* arguments : Examples = list of all available examples */
; /* Cond = attribute/value pair */
; /* Examples1 = list of examples with attr/val */
; /* */
; /******************************************************************/
; /* Examples1 contains elements of Examples that satisfy Condition */
; /******************************************************************/
(= (remove () $_ ()) True)
; /******************************************************************/
; /* */
; /* call : remove(+Examples,+Conj,-RestExamples) */
; /* */
; /* arguments : Examples = list of all available examples */
; /* Conj = list of attribute/value pair */
; /* RestExamples = list of examples not matching */
; /* Conj */
; /* */
; /******************************************************************/
; /* removing from Examples those examples that match Conj gives */
; /* RestExamples . */
; /******************************************************************/
(= (remove (Cons (example $Class $Obj) $Es) $Conj $Es1)
(satisfy $Obj $Conj)
(set-det)
(remove $Es $Conj $Es1))
(= (remove (Cons $E $Es) $Conj (Cons $E $Es1))
(remove $Es $Conj $Es1))
(= (satisfy $Object $Conj)
(not (, (member (= $Att $Val) $Conj) (member (= $Att $ValX) $Object) (not (== $ValX $Val)))))
(= (match $Object $Description)
(member $Conj $Description)
(satisfy $Object $Conj))
(= (writelist ()) True)
(= (writelist (Cons $X $L))
( (tab 2)
(write $X) nl
(writelist $L)))
(= help
( (write 'Load data set and start learning with command: [Filename].') nl))
!(help *)