def diagnose(self, symptoms, age_group=None): """Returns a list of possible diagnosis, if any, given a list of symptoms. For example, if german measles has the symptoms 'runny nose', 'fever', 'headache' and 'rash', the system will return a list containing a dict with the diagnosis. symptoms = ['runnynose', 'fever', 'headache', 'rash'] doctor = Doctor('medical') results = doctor.diagnose(symptoms) results >>> [{'Diagnosis': 'germanmeasles'}] The diagnosis can be accessed with the key 'Diagnosis'. NOTE: The capital D is important. diagnosis = results[0] diagnosis['Diagnosis'] >>> 'germanmeasles' """ prolog = Prolog() if age_group is not None: prolog.assertz('age_group(patient,%s)' % age_group) for symptom in symptoms: prolog.assertz('symptom(patient,%s)' % symptom) prolog.consult(self.prolog_file) return list(prolog.query('hypothesis(patient,Diagnosis)'))
def modified_run(self): try: from pyswip.prolog import Prolog p = Prolog() p.assertz('setup_prolog(1)') list(p.query('setup_prolog(1)')) except: print('----------------------') print('There is a problem with SWI-Prolog Installation') print('Please, check if you have it installed with shared Libraries') print('Installation guide: https://github.com/yuce/pyswip/blob/master/INSTALL') print('----------------------') orig_run(self)
def modified_run(self): try: from pyswip.prolog import Prolog p = Prolog() p.assertz('setup_prolog(1)') list(p.query('setup_prolog(1)')) except: print('----------------------') print('There is a problem with SWI-Prolog Installation') print( 'Please, check if you have it installed with shared Libraries') print( 'Installation guide: https://github.com/yuce/pyswip/blob/master/INSTALL' ) print('----------------------') orig_run(self)
def main(): # Initialize the prolog code prolog = Prolog() prolog.consult("talking_to_kid.pl") item = 'slides' stop = False first = True positive = True # Main program loop while not stop: #Ask someone a question based on their previous answer if first: # If this is the first question, we have a neutral statement first = False response = query(item, 'Hello!', '?') else: if positive: # We pick a random positive prefix and suffix if they answered positively previously response = query(item, random.choice(positivePrefix), random.choice(positiveSuffix)) else: # We pick a random negative prefix and suffix if they answered 'no' to the previous queries response = query(item, random.choice(negativePrefix), random.choice(negativeSuffix)) if response: #If the response is True, ask a followup followup = list(prolog.query('followup({}, Y)'.format(item))) # Retrieve a random followup question related to the item (Since some items can have multiple followups) followup_item = random.choice(followup)['Y'] followup_true = askFollow(followup_item) # If the followup is true, then we can say that they like the item. Otherwise they dislike it # We add the item to the knowledge base as something that is liked or disliked by asserting this to the prolog script. if followup_true: prolog.assertz('like({})'.format(item)) positive = True # Remember the positivity of the statement to change the statement else: prolog.assertz('dislike({})'.format(item)) positive = False else:
def main(): registerForeign(hello) prolog = Prolog() prolog.assertz("father(michael,john)") prolog.assertz("father(michael,gina)") list(prolog.query("father(michael,X), hello(X)"))