├── .gitignore ├── expert-system-children_diabetes ├── lunch.sh ├── example-url ├── package.json ├── main.js └── expert-system.py ├── symantic-network-nodejs-home_accidents ├── package.json ├── yarn.lock └── main.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /expert-system-children_diabetes/lunch.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | pip install pyknow && npm install && node main.js -------------------------------------------------------------------------------- /symantic-network-nodejs-home_accidents/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "expert": "^0.3.2", 4 | "underscore": "^1.8.3" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /symantic-network-nodejs-home_accidents/yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | expert@^0.3.2: 6 | version "0.3.2" 7 | resolved "https://registry.yarnpkg.com/expert/-/expert-0.3.2.tgz#e762930a1a986f8c61d702184c6581ee02a17988" 8 | dependencies: 9 | underscore "*" 10 | 11 | underscore@*, underscore@^1.8.3: 12 | version "1.8.3" 13 | resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.8.3.tgz#4f3fb53b106e6097fcf9cb4109f2a5e9bdfa5022" 14 | -------------------------------------------------------------------------------- /expert-system-children_diabetes/example-url: -------------------------------------------------------------------------------- 1 | http://localhost:3000/res/api?age=3 2 | &glycemie=2 3 | &shakiness=True 4 | &hunger=True 5 | &sweating=True 6 | &headach=True 7 | &diabetic_parents=False 8 | &pale=False 9 | &urination=False 10 | &thirst=False 11 | &blurred_vision=False 12 | &dry_mouth=False 13 | &smelling_breath=False 14 | &shortness_of_breath=False 15 | -------------------------------------------------------------------------------- /expert-system-children_diabetes/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "expert-system-node-web-api", 3 | "version": "1.0.0", 4 | "description": "Web API for the expert system", 5 | "main": "main.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/Fcmam5/kr-and-expert-system.git" 12 | }, 13 | "author": "Fortas Abdeldjalil (Fcmam5)", 14 | "license": "MIT", 15 | "bugs": { 16 | "url": "https://github.com/Fcmam5/kr-and-expert-system/issues" 17 | }, 18 | "homepage": "https://github.com/Fcmam5/kr-and-expert-system#readme", 19 | "dependencies": { 20 | "express": "^4.16.2", 21 | "python-shell": "^0.4.0" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /symantic-network-nodejs-home_accidents/main.js: -------------------------------------------------------------------------------- 1 | var expert = require('expert'); 2 | var _ = require('underscore'); 3 | 4 | var domain = expert.Domain(), 5 | Concept = domain.Concept, 6 | Relation = domain.Relation; 7 | 8 | var accident = Concept.create({id:"accident"}), 9 | chute = Concept.create({id:"chute"}), 10 | brulure = Concept.create({id:"brulure"}); 11 | nosee = Concept.create({id:"nosee"}); 12 | fracture = Concept.create({id:"fracture"}); 13 | douleur = Concept.create({id:"douleur"}); 14 | 15 | 16 | var isa = domain.isa, 17 | example = domain.example; 18 | 19 | var has = Relation.create({id:"has"}), 20 | whatHas = Relation.create({id:"what has",inverseFor:has}); 21 | 22 | // Examples 23 | fracture 24 | .isa(accident) 25 | .has(nosee) 26 | .has(douleur) 27 | 28 | brulure 29 | .isa(accident) 30 | .has(douleur) 31 | 32 | console.log("What has fracture?"); 33 | console.log(whatHas(douleur)); 34 | -------------------------------------------------------------------------------- /expert-system-children_diabetes/main.js: -------------------------------------------------------------------------------- 1 | const express = require('express') 2 | const app = express() 3 | const PythonShell = require('python-shell'); 4 | 5 | /* GET home page. */ 6 | app.get('/res/api',(req, res, next) => { 7 | let options = { 8 | mode: 'text', 9 | pythonPath: '/usr/bin/python3', 10 | scriptPath: './', 11 | args: [req.query.age, req.query.glycemie, req.query.shakiness, req.query.hunger, req.query.sweating, req.query.headach,req.query.diabetic_parents, req.query.pale, req.query.urination, req.query.thirst, req.query.blurred_vision, req.query.dry_mouth, req.query.smelling_breath, req.query.shortness_of_breath] 12 | }; 13 | 14 | PythonShell.run('expert-system.py', options, (err, results) => { 15 | if (err) throw err; 16 | // results is an array consisting of messages collected during execution 17 | console.log(results); 18 | res.json({"res": results}) 19 | }); 20 | 21 | }); 22 | 23 | app.get('/', (req, res) => res.send('Try: http://localhost:3000/res/api?age=3\ 24 | &glycemie=2\ 25 | &shakiness=True\ 26 | &hunger=True\ 27 | &sweating=True\ 28 | &headach=True\ 29 | &diabetic_parents=False\ 30 | &pale=False\ 31 | &urination=False\ 32 | &thirst=False\ 33 | &blurred_vision=False\ 34 | &dry_mouth=False\ 35 | &smelling_breath=False\ 36 | &shortness_of_breath=False\ 37 | ')) 38 | 39 | app.listen(3000, () => console.log('Example app listening on port 3000!')) 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Knowledge representation examples 2 | 3 | This repository contains some programming exercises for **Ontologies and Knowledge representation** class in University. It contains until the moment two examples: 4 | 5 | * [Expert System that diagnosis children diabetes](expert-system-for-children-diabetes-diagnosis). 6 | * Semantic network (home accidents as example) *unfinished yet* 7 | 8 | ## Expert System for children Diabetes Diagnosis 9 | 10 | Built with Python, using [PyKnow]() library. And there's a tiny Node/Express server for offering a Web api 11 | ### Usage 12 | * First install `pyknow` Python package, and `Express` and `python-shell` for Node.js, then lunch the server (`main.js`) in order to use the Web API. 13 | All this could be done with this command: 14 | `pip install pyknow && npm install && node main.js` 15 | * Then open your browser, and past this URL for example. 16 | ``` 17 | localhost:3000/res/api?age=3 18 | &glycemie=2 19 | &shakiness=True 20 | &hunger=True 21 | &sweating=True 22 | &headach=True 23 | &diabetic_parents=False 24 | &pale=False 25 | &urination=False 26 | &thirst=False 27 | &blurred_vision=False 28 | &dry_mouth=False 29 | &smelling_breath=False 30 | &shortness_of_breath=False 31 | ``` 32 | Now you can play with each parameter 33 | * age = [int from 0 to 5] 34 | * glycemie= [ int `mmol/l` ] 35 | * Signs, like shakiness, hunger, sweating..= [Boolean: True/False] 36 | 37 | Or You can use the python file directly just introduce your facts manually 38 | 39 | Change 40 | ```python 41 | # ligne 102 42 | engine.declare(Personne(age= int(sys.argv[1]), 43 | glycemie=int(sys.argv[2]), 44 | shakiness= bool(sys.argv[3]), 45 | hunger= bool(sys.argv[4]), 46 | sweating= bool(sys.argv[5]), 47 | headach= bool(sys.argv[6]), 48 | diabetic_parents = bool(sys.argv[7]), 49 | pale= bool(sys.argv[8]), 50 | urination = bool(sys.argv[9]), 51 | thirst = bool(sys.argv[10]), 52 | blurred_vision = bool(sys.argv[11]), 53 | dry_mouth = bool(sys.argv[12]), 54 | smelling_breath = bool(sys.argv[13]), 55 | shortness_of_breath = bool(sys.argv[14]), 56 | )) 57 | ``` 58 | To (for example) 59 | ```python 60 | # ligne 102 61 | engine.declare(Personne(age= 2, 62 | glycemie=True, 63 | shakiness=True, 64 | hunger= True, 65 | sweating= True, 66 | headach= True, 67 | diabetic_parents = False, 68 | pale= False, 69 | urination = False, 70 | thirst = False, 71 | blurred_vision = False, 72 | dry_mouth = False, 73 | smelling_breath = False, 74 | shortness_of_breath = False, 75 | )) 76 | ``` 77 | -------------------------------------------------------------------------------- /expert-system-children_diabetes/expert-system.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # # -*- coding: utf-8 -*- 3 | 4 | from pyknow import * 5 | import sys 6 | 7 | """ 8 | Expert system for detecting if an enfant is diabetic 9 | @author: Fortas Abdeldjalil. 10 | Thanks to Chawki Benchehida for the introduction to PyKe and Nesrine Rekkab for help. 11 | Helpful Ressources: 12 | * http://www.nospetitsmangeurs.org/quoi-faire-si-mon-enfant-a-le-diabete/ 13 | * http://www.nospetitsmangeurs.org/les-hauts-et-les-bas-du-taux-de-sucre/ 14 | * http://www.childrenshospital.org/conditions-and-treatments/conditions/hypoglycemia-and-low-blood-sugar/symptoms-and-causes 15 | * https://www.mayoclinic.org/diseases-conditions/hyperglycemia/symptoms-causes/syc-20373631 16 | """ 17 | 18 | 19 | class Personne(Fact): 20 | """Info about the patient""" 21 | pass 22 | 23 | 24 | def SUMFIELDS(p, *fields): 25 | return sum([p.get(x, 0) for x in fields]) 26 | 27 | 28 | class InreferenceEngine(KnowledgeEngine): 29 | @Rule(Personne(age=P(lambda x: x <= 5))) 30 | def concerned_person(self): 31 | self.declare(Fact(concerned=True)) 32 | 33 | @Rule(Fact(concerned=True), 34 | Personne(glycemie=MATCH.glycemie)) 35 | def hyper_glycemy(self, glycemie): 36 | if glycemie > 10: 37 | self.declare(Fact(hyperglycemic_risk=True)) 38 | print("Warning! High blood sugar") 39 | else: 40 | self.declare(Fact(hyperglycemic_risk=False)) 41 | 42 | @Rule(Fact(concerned=True), 43 | Personne(glycemie=MATCH.glycemie)) 44 | def hypo_glycemy(self, glycemie): 45 | if glycemie < 4: 46 | print("Warning! Low blood sugar") 47 | self.declare(Fact(hypoglycemic_risk=True)) 48 | else: 49 | self.declare(Fact(hypoglycemic_risk=False)) 50 | 51 | @Rule(Fact(concerned=True), 52 | AS.p << Personne(), 53 | TEST(lambda p: SUMFIELDS(p, 54 | 'shakiness', 55 | 'hunger', 56 | 'sweating', 57 | 'headach', 58 | 'pale') > 2)) 59 | def has_signs_low_sugar(self, p): 60 | self.declare(Fact(has_signs_low_sugar=True)) 61 | 62 | 63 | # If the patient is a child and has one or many signes or his blood sugar level is low 64 | @Rule(Fact(concerned=True), 65 | Fact(has_diabetic_parents=True), 66 | Fact(has_signs_low_sugar=True)) 67 | def protocole_risk_low(self): 68 | print("Warning! Child could be diabetic") 69 | 70 | # If the patient is a child and has one or many signes, and his blood sugar level is low and passed the test 71 | @Rule(Fact(concerned=True), 72 | Fact(hypoglycemic_risk=True), 73 | Fact(has_signs_low_sugar=True)) 74 | def protocole_alert_low(self): 75 | print("Alert! High risk of diabetes, you must see a doctor") 76 | 77 | 78 | #If the patient is child and has at least one of his parents diabetic 79 | @Rule(Fact(concerned=True), 80 | Personne(diabetic_parents=True)) 81 | def has_diabetic_parents(self): 82 | self.declare(Fact(has_diabetic_parents=True)) 83 | 84 | 85 | @Rule(Fact(concerned=True), 86 | AS.p << Personne(), 87 | TEST(lambda p: SUMFIELDS(p, 88 | 'urination', 89 | 'thirst', 90 | 'blurred_vision', 91 | 'headach', 92 | 'dry_mouth', 93 | 'smelling_breath', 94 | 'shortness_of_breath') > 2) 95 | ) 96 | def has_signs_high_sugar(self, **_): 97 | self.declare(Fact(has_signs_high_sugar=True)) 98 | 99 | @Rule(Fact(concerned=True), 100 | Fact(has_diabetic_parents=True), 101 | Fact(has_signs_high_sugar=True)) 102 | def protocole_risk_high(self): 103 | print("Warning! Child could be diabetic") 104 | 105 | @Rule(Fact(concerned=True), 106 | Fact(hyperglycemic_risk=True), 107 | Fact(has_signs_high_sugar=True)) 108 | def protocole_alert_high(self): 109 | print("Alert! High risk of diabetes, you must see a doctor") 110 | 111 | 112 | engine = InreferenceEngine() 113 | engine.reset() 114 | 115 | # Initial facts 116 | 117 | # Sick high 118 | engine.declare(Personne(age= int(sys.argv[1]), 119 | glycemie=int(sys.argv[2]), 120 | shakiness= bool(sys.argv[3]), 121 | hunger= bool(sys.argv[4]), 122 | sweating= bool(sys.argv[5]), 123 | headach= bool(sys.argv[6]), 124 | diabetic_parents = bool(sys.argv[7]), 125 | pale= bool(sys.argv[8]), 126 | urination = bool(sys.argv[9]), 127 | thirst = bool(sys.argv[10]), 128 | blurred_vision = bool(sys.argv[11]), 129 | dry_mouth = bool(sys.argv[12]), 130 | smelling_breath = bool(sys.argv[13]), 131 | shortness_of_breath = bool(sys.argv[14]), 132 | )) 133 | 134 | # engine.declare(Personne(age=2, 135 | # glycemie=11, 136 | # shakiness= False, 137 | # hunger= False, 138 | # sweating= False, 139 | # headach= False, 140 | # diabetic_parents = True, 141 | # pale= False, 142 | # urination = True, 143 | # thirst = True, 144 | # blurred_vision = False, 145 | # dry_mouth = True, 146 | # smelling_breath = False, 147 | # shortness_of_breath = False, 148 | # )) 149 | engine.run() 150 | --------------------------------------------------------------------------------