-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmycin.nim
415 lines (339 loc) · 9.05 KB
/
mycin.nim
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import std/[
tables,
options,
sugar,
strutils
]
# confidence factor related type and functions
type
ConfidenceFactor = object
value: float
true_value: float = 1.0
false_value: float = -1.0
unknown: float = 0.0
cutoff: float = 0.2
Cf = ConfidenceFactor
proc `and`(cf1: Cf, cf2: Cf): Cf =
let value = min(cf1.value, cf2.value)
Cf(value: value)
proc `or`(cf1: Cf, cf2: Cf): Cf =
let a = cf1.value
let b = cf2.value
var value: float
if (a > 0 and b > 0):
value = a + b - a * b
elif (a < 0 and b < 0):
value = a + b + a * b
else:
value = (a + b) / (1 - min(abs(a), abs(b)))
Cf(value: value)
proc is_valid(cf: Cf): bool =
(cf.value <= cf.true_value) and (cf.value >= cf.false_value)
proc is_true(cf: Cf): bool =
cf.is_valid and (cf.value > cf.cutoff)
proc is_false(cf: Cf): bool =
cf.is_valid and (cf.value < (1.0 - cf.cutoff))
# parameters
type
ParameterType = enum
String, Float, Integer, Boolean
ParameterValue = object
case kind: ParameterType
of String:
string_value: Option[string]
of Float:
float_value: Option[float]
of Integer:
integer_value: Option[int]
of Boolean:
boolean_value: Option[bool]
proc eq(a, b: ParameterValue): bool =
if a.kind != b.kind:
return false
case a.kind:
of String:
result = a.string_value == b.string_value
of Integer:
result = a.integer_value == b.integer_value
of Float:
result = a.float_value == b.float_value
of Boolean:
result = a.boolean_value == b.boolean_value
# use object variants in nim
type
Parameter = object
name: string
context_name: string
ask_first: bool
case kind: ParameterType
of String:
string_valid: Option[seq[string]]
of Float:
float_valid: Option[seq[float]]
of Integer:
integer_valid: Option[seq[int]]
of Boolean:
discard
proc parse_bool(input: string): Option[bool] =
if input == "true":
result = some(true)
elif input == "false":
result = some(false)
else:
result = none(bool)
proc parse_int_to_option(input: string): Option[int] =
try:
result = some(parse_int(input))
except ValueError:
result = none(int)
proc parse_float_to_option(input: string): Option[float] =
try:
result = some(parse_float(input))
except ValueError:
result = none(float)
proc from_string(parameter: Parameter, input: string): ParameterValue =
case parameter.kind:
of String:
let valid = parameter.string_valid
var string_value: Option[string]
if valid.is_some and valid.get.contains(input):
string_value = some(input)
else:
string_value = none(string)
result = ParameterValue(kind: String, string_value: string_value)
of Integer:
let valid = parameter.integer_valid
var integer_value = parse_int_to_option(input)
if valid.is_some and integer_value.is_some and
not valid.get.contains(integer_value.get):
integer_value = none(int)
result = ParameterValue(kind: Integer, integer_value: integer_value)
of Float:
let valid = parameter.float_valid
var float_value = parse_float_to_option(input)
if valid.is_some and float_value.is_some and
not valid.get.contains(float_value.get):
float_value = none(float)
result = ParameterValue(kind: Float, float_value: float_value)
of Boolean:
result = ParameterValue(kind: Boolean, boolean_value: parse_bool(input))
proc ask(parameter: Parameter): ParameterValue =
parameter.from_string(read_line(stdin))
# context
type
Context = ref object
name: string
count: int = 0
initial_data: seq[string] = @[]
goals: seq[string] = @[]
proc instantiate(c: Context): (string, int) =
inc(c.count)
(c.name, c.count)
# condition
type
CondMatchOp = (a: ParameterValue, b: ParameterValue) -> bool
Condition = object
param_name: string
context_name: string
operation: CondMatchOp
value: ParameterValue
Cond = Condition
# rules
type
Rule = object
num: int
premises: seq[Cond]
conclusions: seq[Cond]
cf: float = 1.0
# expert system
type
ExpertSystem = ref object
contexts: seq[Context] = @[]
parameters: seq[Parameter] = @[]
rules: seq[Rule] = @[]
proc add_context(expert: ExpertSystem, c: Context) =
expert.contexts.add(c)
proc add_param(expert: ExpertSystem, p: Parameter) =
expert.parameters.add(p)
proc add_rule(expert: ExpertSystem, r: Rule) =
expert.rules.add(r)
# main
proc main() =
var expert = ExpertSystem()
expert.add_context(Context(name: "patient", initial_data: @["name", "sex", "age"]))
expert.add_context(Context(name: "culture", initial_data: @["site", "days-old"]))
expert.add_context(Context(name: "organism", goals: @["identity"]))
# patient paramas
expert.add_param(Parameter(
name: "name",
context_name: "patient",
ask_first: true,
kind: String
))
expert.add_param(Parameter(
name: "sex",
context_name: "patient",
ask_first: true,
kind: String,
string_valid: some(@["M", "F"])
))
expert.add_param(Parameter(
name: "age",
context_name: "patient",
ask_first: true,
kind: Integer
))
expert.add_param(Parameter(
name: "burn",
context_name: "patient",
ask_first: true,
kind: String,
string_valid: @["no", "mild", "serious"].some
))
expert.add_param(Parameter(
name: "compromised-host",
context_name: "patient",
kind: Boolean
))
# culture params
expert.add_param(Parameter(
name: "site",
context_name: "culture",
ask_first: true,
kind: String,
string_valid: @["blood"].some
))
expert.add_param(Parameter(
name: "days-old",
context_name: "culture",
ask_first: true,
kind: Integer
))
# organism
expert.add_param(Parameter(
name: "identity",
context_name: "organism",
ask_first: true,
kind: String,
string_valid: @[
"pseudomonas",
"klebsiella",
"enterobacteriaceae",
"staphylococcus",
"bacteroides",
"streptococcus"
].some
))
expert.add_param(Parameter(
name: "gram",
context_name: "organism",
ask_first: true,
kind: String,
string_valid: @["rod", "coccus"].some
))
expert.add_param(Parameter(
name: "morphology",
context_name: "organism",
kind: String,
string_valid: @["aerobic", "anaerobic"].some
))
expert.add_param(Parameter(
name: "growth-conformation",
context_name: "organism",
kind: String,
string_valid: @["chains", "pairs", "clumps"].some
))
# add rules
proc str_cond(param: string, context: string, operation: CondMatchOp,
value: string): Cond =
Cond(
param_name: param,
context_name: context,
operation: operation,
value: ParameterValue(kind: String, string_value: value.some)
)
proc bool_cond(param: string, context: string, operation: CondMatchOp,
value: bool): Cond =
Cond(
param_name: param,
context_name: context,
operation: operation,
value: ParameterValue(kind: Boolean, boolean_value: value.some)
)
expert.add_rule(Rule(
num: 52,
premises: @[
str_cond("site", "culture", eq, "blood"),
str_cond("gram", "organism", eq, "neg"),
str_cond("morphology", "organism", eq, "rod"),
str_cond("aerobicity", "organism", eq, "anaerobic"),
],
conclusions: @[
str_cond("identity", "organism", eq, "bacteroides")
],
cf: 0.4
))
expert.add_rule(Rule(
num: 71,
premises: @[
str_cond("gram", "organism", eq, "pos"),
str_cond("morphology", "organism", eq, "coccus"),
str_cond("growth-conformation", "organism", eq, "clumps"),
],
conclusions: @[
str_cond("identity", "organism", eq, "staphylococcus")
],
cf: 0.7
))
expert.add_rule(Rule(
num: 73,
premises: @[
str_cond("site", "culture", eq, "blood"),
str_cond("gram", "organism", eq, "neg"),
str_cond("morphology", "organism", eq, "rod"),
str_cond("aerobicity", "organism", eq, "anaerobic")
],
conclusions: @[
str_cond("identity", "organism", eq, "bacteroides")
],
cf: 0.9
))
expert.add_rule(Rule(
num: 73,
premises: @[
str_cond("gram", "organism", eq, "neg"),
str_cond("morphology", "organism", eq, "rod"),
bool_cond("compromised-host", "patient", eq, true)
],
conclusions: @[
str_cond("identity", "organism", eq, "pseudomonas")
],
cf: 0.6
))
expert.add_rule(Rule(
num: 107,
premises: @[
str_cond("gram", "organism", eq, "neg"),
str_cond("morphology", "organism", eq, "rod"),
str_cond("aerobicity", "organism", eq, "aerobic")
],
conclusions: @[
str_cond("identity", "organism", eq, "enterobacteriaceae")
],
cf: 0.8
))
expert.add_rule(Rule(
num: 165,
premises: @[
str_cond("gram", "organism", eq, "pos"),
str_cond("morphology", "organism", eq, "coccus"),
str_cond("growth-conformation", "organism", eq, "chain")
],
conclusions: @[
str_cond("identity", "organism", eq, "streptococcus")
],
cf: 0.7
))
# execute main
when is_main_module:
main()