#!/usr/bin/env python # use compile() for speedup import string, re while 1: print "---------------------" print "hit to abort" raw = raw_input("please input patient details: ") # "#" format patient ID if re.match("^(\s|\t)*#(\d|\s|\t)+$", raw): print "patient ID" print " will return" print " 1) patient based on id" print " 2) " tmp = raw.replace(' ', '') tmp = tmp.replace('\t', '') tmp = tmp.replace('#', '') cmd = "... where id like '%s%%';" % tmp print "query:", cmd continue # "" - patient ID or DOB elif re.match("^(\s|\t)*\d+(\s|\t)*$", raw): print "either patient ID or d.o.b." print " will return" print " 1) patient based on id" print " 2) patient based on d.o.b." print " 3) " tmp = raw.replace(' ', '') tmp = tmp.replace('\t', '') cmd = "... where id like '%s%%';" % tmp print "query:", cmd cmd = "... where date_trunc('day', dob) like (select timestamp '%s');" % raw print "query:", cmd continue # "" - DOB or patient ID elif re.match("^(\d|\s|\t)+$", raw): print "either d.o.b. or patient ID" print " will return" print " 1) patient based on d.o.b." print " 2) patient based on id" print " 3) " cmd = "... where date_trunc('day', dob) like (select timestamp '%s');" % raw print "query:", cmd tmp = raw.replace(' ', '') tmp = tmp.replace('\t', '') cmd = "... where id like '%s%%';" % tmp print "query:", cmd # "*|$<...>" - DOB elif re.match("^(\s|\t)*(\*|\$).+$", raw): print "supposedly d.o.b." print " will return:" print " 1) patient based on d.o.b." print " 2) not found" tmp = raw.replace('*', '') tmp = tmp.replace('$', '') cmd = "... where date_trunc('day', dob) like (select timestamp '%s');" % tmp print "query:", cmd continue # "+<...>" - DOD date of death elif re.match("^(\s|\t)*\+.+$", raw): print "supposedly d.o.d." print " will return:" print " 1) patient based on d.o.d." print " 2) not found" tmp = raw.replace('+', '') cmd = "... where date_trunc('day', identity.deceased) like (select timestamp '%s');" % tmp print "query:", cmd continue else: print "- this is a more complicated pattern" print "- we don't expect patient IDs in complicated patterns" print "- hence, any digits signify a date" parts_list = re.split(",|;", raw) date_parts = [] name_parts = [] name_count = 0 for part in parts_list: # any digits ? if re.search("\d+", part): date_parts.append(part) else: tmp = part.strip() tmp = re.split("\s*|\t*", tmp) name_count = name_count + len(tmp) name_parts.append(tmp) print "total names:", name_count print "name parts :", name_parts print "date parts :", date_parts where1 = [] where2 = [] if (len(name_parts) == 1) and (name_count == 2): # if "karsten hilbert" -> "karsten" is usually first name, # so check this version first where1.append("firstnames ilike '%s%%'" % name_parts[0][0]) where1.append("lastnames ilike '%s%%'" % name_parts[0][1]) where2.append("firstnames ilike '%s%%'" % name_parts[0][1]) where2.append("lastnames ilike '%s%%'" % name_parts[0][0]) elif len(name_parts) == 2: # if "hilbert, karsten" -> "hilbert" is usually last name, # so check this version first where1.append("firstnames ilike '%s%%'" % string.join(name_parts[1], ' ')) where1.append("lastnames ilike '%s%%'" % string.join(name_parts[0], ' ')) where2.append("firstnames ilike '%s%%'" % string.join(name_parts[0], ' ')) where2.append("lastnames ilike '%s%%'" % string.join(name_parts[1], ' ')) else: # big trouble - arbitrary name part information print "uh oh - arbitrary name parts" if len(name_parts) == 1: for part in name_parts[0]: where1.append("firstnames || lastnames ilike '%%%s%%'" % part) where2.append("firstnames || lastnames ilike '%%%s%%'" % part) else: tmp = [] for part in name_parts: tmp.append(string.join(part, ' ')) for part in tmp: where1.append("firstnames || lastnames ilike '%%%s%%'" % part) where2.append("firstnames || lastnames ilike '%%%s%%'" % part) if len(date_parts) == 1: where1.append("date_trunc('day', dob) like (select timestamp '%s')" % date_parts[0]) where2.append("date_trunc('day', dob) like (select timestamp '%s')" % date_parts[0]) elif len(date_parts) > 1: where1.append("date_trunc('day', dob) like (select timestamp '%s')" % date_parts[0]) where1.append("date_trunc('day', identity.deceased) like (select timestamp '%s'" % date_parts[1]) where2.append("date_trunc('day', dob) like (select timestamp '%s')" % date_parts[0]) where2.append("date_trunc('day', identity.deceased) like (select timestamp '%s')" % date_parts[1]) where_clause1 = "... where %s" % string.join(where1, ' and ') where_clause2 = "... where %s" % string.join(where2, ' and ') print "query:", where_clause1 print "query:", where_clause2