hypochondria

Find your best suited disease.
git clone git://r-36.net/hypochondria
Log | Files | Refs | README | LICENSE

hypochondria (2511B)


      1 #!/usr/bin/env python
      2 # coding=utf-8
      3 #
      4 # Copy me if you can.
      5 # by 20h
      6 #
      7 
      8 import os
      9 import sys
     10 import getopt
     11 import pickle
     12 from prompt_toolkit import prompt
     13 from prompt_toolkit.contrib.completers import WordCompleter
     14 import time
     15 import random
     16 import math
     17 
     18 def usage(app):
     19 	app = os.path.basename(app)
     20 	print("usage: %s [-h]" % (app), file=sys.stderr)
     21 	sys.exit(1)
     22 
     23 def main(args):
     24 	try:
     25 		opts, largs = getopt.getopt(args[1:], "h")
     26 	except getopt.GetoptError as err:
     27 		print(str(err))
     28 		usage(args[0])
     29 	
     30 	for o, a in opts:
     31 		if o == "-h":
     32 			usage(args[0])
     33 		else:
     34 			assert False, "unhandled option"
     35 
     36 	with open("symptoms.pickle", "rb") as f:
     37 		symptoms = pickle.load(f)
     38 
     39 	symptoms_completer = WordCompleter(symptoms["symptoms"])
     40 	symptomsl = prompt("Please enter your symptoms (comma separated)> ",
     41 			completer=symptoms_completer)
     42 	symptoms = symptomsl.split(",")
     43 	symptoms = [s.strip() for s in symptoms]
     44 	
     45 	with open("diseases.pickle", "rb") as f:
     46 		diseases = pickle.load(f)
     47 
     48 	dhash = {}
     49 	for disease in diseases:
     50 		dsymptoms = diseases[disease]["symptoms"]
     51 		for dsymptom in dsymptoms:
     52 			for symptom in symptoms:
     53 				if symptom != dsymptom:
     54 					continue
     55 
     56 				if disease in dhash:
     57 					dhash[disease]["count"] += 1
     58 					dhash[disease]["user_symptoms"].append(symptom)
     59 				else:
     60 					dhash[disease] = diseases[disease]
     61 					dhash[disease]["count"] = 1
     62 					dhash[disease]["user_symptoms"] = [symptom]
     63 
     64 	def symptoms_cmp(d):
     65 		return dhash[d]["count"]
     66 
     67 	selection = list(dhash.keys())
     68 	selection.sort(key=symptoms_cmp)
     69 	if len(selection) == 0:
     70 		final_disease = random.choice(list(diseases.keys()))
     71 	else:
     72 		final_disease = selection[math.floor(random.random() \
     73 				* len(selection))]
     74 
     75 	waitnormal = 1.7
     76 	waitmedium = 0.9
     77 	waitlong = 4.0
     78 	waitfast = 0.55
     79 
     80 	def waitprint(s, t=waitnormal):
     81 		print(s)
     82 		time.sleep(t)
     83 
     84 	waitprint("Analyzing symptoms ...")
     85 	waitprint("Checking the database ...")
     86 	waitprint("Searching for possible diseases ...", waitnormal+0.2)
     87 	waitprint("Hmmm.")
     88 	waitprint("This isn't looking good.")
     89 	waitprint("...")
     90 	waitprint("OH", waitfast)
     91 	waitprint("MY", waitfast)
     92 	waitprint("GOD", waitfast)
     93 	waitprint("YOU", waitfast)
     94 	waitprint("COULD", waitfast)
     95 	waitprint("HAVE", waitfast)
     96 	waitprint(final_disease, waitlong)
     97 	print("Summary: %s" % (diseases[final_disease]["summary"]))
     98 	print("URL: %s" % (diseases[final_disease]["url"]))
     99 	print("All Symptoms: %s" % \
    100 			(", ".join(diseases[final_disease]["symptoms"])))
    101 
    102 	return 0
    103 
    104 if __name__ == "__main__":
    105 	sys.exit(main(sys.argv))
    106