Discussions related to Visual Prolog
billanova
Posts: 5
Joined: 26 Jul 2022 5:50

Code Improvement

Unread post by billanova »

Hi everyone,
I am about to finish my Thesis.
Can anyone tell me if this code need improvements ??
Any suggestions are welcome !!
Thanks

Code here:

Code: Select all

implement diagnoseRun     open core   class facts - factdb     xpositive : (symbol, symbol).     xnegative : (symbol, symbol) nondeterm.   clauses     positive(X, Y) :-         xpositive(X, Y),         !.     positive(X, Y) :-         not(xnegative(X, Y)),         question(X, Y).   clauses     question(X, Y) :-         !,         Reply = answerDialog::ask(string::concat(X, " it ", Y, "\n")),         % !,         C = string::charLower(string::frontChar(Reply)),         remember(X, Y, C).   clauses     remember(X, Y, Ans) :-         if Ans = 'y' then             assertz(xpositive(X, Y))         elseif Ans = 'n' then             assertz(xnegative(X, Y)),             fail         end if.       clear_facts() :-         retractFactDb(factDb). %_Reply = stdio::readLine().   clauses     run() :-         disease(X),         !,         messageBox::displayNote(X, "The disease having all these symptoms is"),         clear_facts.       run() :-         messageBox::displayError("The disease cannot be determined."),         clear_facts.   clauses     disease('Covid-19') :-         is_disease('Covid-19').       disease('Pharyngitis') :-         is_disease('Pharyngitis').       disease('Flu') :-         is_disease('Flu').       disease('Common Cold') :-         is_disease('Common Cold').       disease('Pneumonia') :-         is_disease('Pneumonia').       disease('Menigitis') :-         is_disease('Menigitis').   clauses     is_disease('Covid-19') :-         positive('have', 'fever?'),         positive('feel', 'chest pain?'),         positive('have', 'lose of taste?'),         positive('have', 'lose of smell?'),         positive('have', 'cough?'),         positive('feel', 'difficulty breathing?'),         positive('have', 'diarrhea?'),         positive('have', 'vomiting?').       is_disease('Pharyngitis') :-         positive('have', 'sudden onset of sore throat?'),         positive('have', 'pain with swallowing?'),         positive('have', 'fever?'),         positive('have', 'cough?'),         positive('have', 'rhinorrea?'),         positive('have', 'hoarseness?'),         positive('have', 'oral ulcers?'),         positive('have', 'conjuctivitis?').       is_disease('Flu') :-         positive('have', 'fever?'),         positive('have', 'cough?'),         positive('have', 'sore throat?'),         positive('have', 'runny or stuffy nose?'),         positive('have', 'muscle or body aches?'),         positive('have', 'headaches?'),         positive('have', 'fatigue?').       is_disease('Common Cold') :-         positive('have', 'runny or stuffy nose?'),         positive('have', 'sore throat?'),         positive('have', 'headaches?').       is_disease('Pneumonia') :-         positive('have', 'fever and chills'),         positive('have', 'cough?'),         positive('have', 'rappid breathing or difficulty breathing?'),         positive('have', 'chest pain?').       is_disease('Menigitis') :-         positive('have', 'stiff neck?'),         positive('have', 'fever?'),         positive('have', 'headache?'),         positive('have', 'photophobia (eyes being more sensitive to light)?'),         positive('have', 'confusion?').   end implement diagnoseRun
Harrison Pratt
VIP Member
Posts: 439
Joined: 5 Nov 2000 0:01

Re: Code Improvement

Unread post by Harrison Pratt »

Your code appears to be functionally incomplete. For example, xpositive/2 and xnegative/2 are asserted but not used and the run/0 clauses don't work like I think they ought to.

As a starting point, I would suggest that you try the following:

Specify your own domains to make your code easier to follow.

Code: Select all

domains         diseaseID = symbol.         signOrSymptom = have; feel.         observedOrReported = string.   class predicates         disease : ( diseaseID ).         positive : ( signOrSymptom, observedReported).
BTW, what you code as 'have' and 'feel' are called "signs" (what you observe) and "symptoms" (what the patient experiences). It's helpful to use the same terminology that physicians use.

I'd suggest putting your positive/2 information into facts like this:

Code: Select all

class facts         ss : ( diseaseID, signOrSymptom, observedOrReported ).
Since the essence of an "expert system" is being able to easily update the knowledge base it would be appropriate to have the diagnostic facts in a text file that the user (you) can easily edit. An "INI" style file would be appropriate for this as a starting point, or you could use something like Excel and save the data as a CSV file.
Harrison Pratt
VIP Member
Posts: 439
Joined: 5 Nov 2000 0:01

Re: Code Improvement

Unread post by Harrison Pratt »

Take a look at the attached files.

I think a better strategy that you could use would be to do the following:
  • Treat all the signs and symptoms as generic "observations"
  • Put the observations into a consultable fact file (not hard-coded as in test.pro
  • Make disease diagnosis contingent upon finding ALL, SOME, & NONE of various observations.
  • Put the disease criteria in a external file.
If you want to get a jump start on externalizing the knowledge base you should be able to copy/paste the obs/2 facts into a text file that you can consult in your program.

The attached code is not optimized, but should enable you to see the general idea.
Attachments
Test.zip
CL and PRO files for "Diagnosis" test
(1.34 KiB) Downloaded 505 times
Post Reply