DaveM@13: #!/usr/bin/env python DaveM@13: DaveM@13: import csv DaveM@13: import time DaveM@13: import unicodedata DaveM@13: from geopy.geocoders import Nominatim DaveM@13: from geopy.exc import GeocoderTimedOut DaveM@13: import random DaveM@13: import pdb DaveM@13: DaveM@19: DEFAULT_TIME = None DaveM@19: # DEFAULT_TIME_M = None DaveM@15: DEAULT_LOCATION = None DaveM@13: DaveM@13: def regulateData(dataDict): DaveM@13: print("Parse %s"%(str(dataDict['ID']))) DaveM@13: p_DOBQ = "What is your partner's date of birth? Please use the format DD/MM/YYYY (for example, 29/03/1981)." DaveM@13: p_TOBQ = "At what exact time were your partner born? Please use the format HHMM (for example, 2204)." DaveM@13: DOB_DQ = "Which day (numeric) have you been born?" DaveM@13: DOB_MQ = "Which month have you been born?" DaveM@13: DOB_YQ = "Year Of Birth" DaveM@13: TOB_Q = "At what exact time were you born? Please use the format HHMM (for example, 2204)." DaveM@13: COB = "What is your place of birth? Please specify city and country (for example, San Francisco, USA)." DaveM@13: p_COB = "What is your partner's place of birth? Please specify city and country (for example, San Francisco, USA)." DaveM@13: dataDict['DOB'] = parseDOB(dataDict[DOB_DQ],dataDict[DOB_MQ],dataDict[DOB_YQ]) DaveM@13: # print (dataDict[DOB_DQ],dataDict[DOB_MQ],dataDict[DOB_YQ]) DaveM@13: # print dataDict['DOB'] DaveM@13: dataDict['TOB'] = parseTOB(dataDict[TOB_Q]) DaveM@13: dataDict['pDOB'] = parsePartnerDOB(dataDict[p_DOBQ]) DaveM@13: dataDict['pTOB'] = parseTOB(dataDict[p_TOBQ]) DaveM@13: # MAKE RANDOM PLACE DaveM@13: # dataDict['COB'] = (random.uniform(-90, 90),random.uniform(-90, 90)) DaveM@13: # dataDict['pCOB'] = (random.uniform(-90, 90),random.uniform(-90, 90)) DaveM@13: dataDict['COB'] = parseBirthTown(dataDict[COB]) DaveM@13: dataDict['pCOB'] = parseBirthTown(dataDict[p_COB]) DaveM@13: return dataDict DaveM@13: DaveM@13: DaveM@13: def parseBirthTown(s): DaveM@13: try: DaveM@13: s = s.encode('ascii') DaveM@13: except UnicodeDecodeError: DaveM@13: # pdb.set_trace() DaveM@13: s = s.decode('latin-1') DaveM@13: # s = unicodedata.normalize('NFKD',s.decode('utf-8')).encode('ascii','ignore') DaveM@13: timeoutTime = 2 DaveM@13: geolocator = Nominatim(timeout=timeoutTime) DaveM@13: while s is not [] and timeoutTime < 60: DaveM@13: try: DaveM@13: location = geolocator.geocode(s) DaveM@13: if location is not None: DaveM@13: # print(location.raw) DaveM@13: # print (location.latitude, location.longitude) DaveM@13: return (location.latitude, location.longitude, location.raw) DaveM@13: else: DaveM@13: s = s.split(' ',1) DaveM@13: if len(s) == 2: DaveM@13: s = s[1] DaveM@13: # print s DaveM@13: else: DaveM@13: s = DEAULT_LOCATION DaveM@13: except: DaveM@13: timeoutTime += 1 DaveM@13: print("Error: geocode failed on input %s, incrementing timeout time to %d"%(s,timeoutTime)) DaveM@13: time.sleep(5) DaveM@13: geolocator = Nominatim(timeout=timeoutTime) DaveM@13: # places = geograpy.get_place_context(text=s) DaveM@13: DaveM@13: def parsePartnerDOB(dob): DaveM@13: # print dob DaveM@13: # pdb.set_trace() DaveM@13: dob = dob.strip() DaveM@13: if(dob.count('-') == 2): DaveM@13: dob = dob.replace('-','/') DaveM@13: if(dob.count(' ') == 2): DaveM@13: dob = dob.replace(' ','/') DaveM@13: dob_ = dob.split('/') DaveM@13: if(len(dob_) != 3): DaveM@13: dob = dob.replace('/','').strip() DaveM@13: dob_ = [] DaveM@13: # print dob DaveM@13: if len(dob) == 8: # ddmmyyyy DaveM@13: dob_.append(dob[:2]) DaveM@13: dob_.append(dob[2:4]) DaveM@13: dob_.append(dob[4:]) DaveM@13: elif len(dob) == 7 and dob[1] == '1' and (dob[2] == '0' or dob[2] == '1' or dob[2] == '2'): # dmmyyyy DaveM@13: dob_.append(dob[0]) DaveM@13: dob_.append(dob[1:3]) DaveM@13: dob_.append(dob[3:]) DaveM@13: elif(len(dob) == 7): DaveM@13: if int(dob[:2]) > 31:# dmmyyyy DaveM@13: dob_.append(dob[0]) DaveM@13: dob_.append(dob[1:3]) DaveM@13: dob_.append(dob[3:]) DaveM@13: elif len(dob) == 7: # ddmyyyy DaveM@13: dob_.append(dob[0:2]) DaveM@13: dob_.append(dob[2]) DaveM@13: dob_.append(dob[3:]) DaveM@13: elif len(dob) == 6 and dob[3:4] != '19': # ddmmyy DaveM@13: dob_.append(dob[:2]) DaveM@13: dob_.append(dob[2:4]) DaveM@13: dob_.append(dob[4:]) DaveM@13: elif len(dob) == 5 and dob[1] == '1' and (dob[2] == '0' or dob[2] == '1' or dob[2] == '2'): # dmmyy DaveM@13: dob_.append(dob[0]) DaveM@13: dob_.append(dob[1:3]) DaveM@13: dob_.append(dob[3:]) DaveM@13: elif len(dob) == 5: # ddmyy DaveM@13: dob_.append(dob[:2]) DaveM@13: dob_.append(dob[2]) DaveM@13: dob_.append(dob[3:]) DaveM@13: elif len(dob) == 4: # dmyy DaveM@13: dob_.append(dob[0]) DaveM@13: dob_.append(dob[1]) DaveM@13: dob_.append(dob[2:]) DaveM@13: else: DaveM@13: if(len(dob) < 4): DaveM@13: return None DaveM@13: # print dob DaveM@13: # print filter(lambda x: x.isdigit(),dob) DaveM@13: print 'no / partnerDOB issue' DaveM@13: # deal with no /'s DaveM@13: try: DaveM@13: d = int(filter(lambda x: x.isdigit(),dob_[0])) DaveM@13: m = int(filter(lambda x: x.isdigit(),dob_[1])) DaveM@13: y = int(filter(lambda x: x.isdigit(),dob_[2])) DaveM@13: if y < 100: DaveM@13: y = y + 1900 DaveM@13: if (m > 12 and d <= 12): DaveM@13: temp = d DaveM@13: d = m DaveM@13: m = temp DaveM@13: if(d > 31 or d < 1 or m > 12 or m < 1 or y > 2017 or y < 1900): DaveM@13: print 'error with DOB '+d+'/'+m+'/'+y DaveM@13: pdb.set_trace() DaveM@13: except TypeError: DaveM@13: return None DaveM@13: # print (d,m,y) DaveM@13: return (d,m,y) DaveM@13: DaveM@13: def monthStringToNum(s): DaveM@13: # print 'inMonthStringToNum' DaveM@13: m = {'jan':1,'feb':2,'mar':3, DaveM@13: 'apr':4,'may':5,'jun':6,'jul':7,'aug':8, DaveM@13: 'sep':9,'oct':10,'nov':11,'dec':12} DaveM@13: s_ = s.strip()[:3].lower() DaveM@13: try: DaveM@13: out = m[s_] DaveM@13: return out DaveM@13: except: DaveM@13: raise ValueError('Not a month') DaveM@13: DaveM@13: def checkMonthDay(d,m): DaveM@13: if d > 31: # take first two digits of day DaveM@13: d = int(str(d)[:2]) DaveM@13: if d > 31: DaveM@13: d = int(str(d)[1]) DaveM@13: if m > 12 and d < 12: # Day and month wrong way round - American DaveM@13: temp = m DaveM@13: m = d DaveM@13: d = temp DaveM@13: if(m == 2): DaveM@13: if d <= 29: DaveM@13: return (True,d,m) DaveM@13: else: DaveM@13: return (False,d,m) DaveM@13: elif m in [4,6,9,11]: DaveM@13: if d <= 30: DaveM@13: return (True,d,m) DaveM@13: else: DaveM@13: return (False,d,m) DaveM@13: elif m <= 12 and d <= 31: DaveM@13: return (True,d,m) DaveM@13: else: DaveM@13: return (False,d,m) DaveM@13: DaveM@13: def parseDOB(d,m,y): DaveM@13: d = int(filter(lambda x: x.isdigit(),d)) DaveM@13: y = int(filter(lambda x: x.isdigit(),y)) DaveM@13: try: DaveM@13: # print m DaveM@13: m = monthStringToNum(m.strip()) DaveM@13: except ValueError: DaveM@13: m = int(m.strip()) DaveM@13: if(y < 100): DaveM@13: y = y + 1900 DaveM@13: (r,d,m) = checkMonthDay(d,m) DaveM@13: if not r: DaveM@13: print 'error with day month' DaveM@13: print (r,d,m) DaveM@13: return (d,m,y) DaveM@13: DaveM@13: def parseTOB(T): DaveM@13: timeFlag = None DaveM@13: T_ = T.replace('.','').lower().strip() DaveM@13: if 'am' in T_: DaveM@13: timeFlag = 0 DaveM@13: T = T_.replace('am','') DaveM@13: if 'pm' in T_: DaveM@13: timeFlag = 1 DaveM@13: T = T_.replace('pm','') DaveM@13: T = T.strip() DaveM@13: if T.count('.') == 1: DaveM@13: T = T.replace('.',':') DaveM@13: try: DaveM@13: if ':' in T: DaveM@13: T_ = T.split(':') DaveM@13: DaveM@13: H = int(T_[0]) DaveM@13: M = int(T_[1]) DaveM@13: else: DaveM@13: if len(T) == 4: DaveM@13: H = int(T[:2]) DaveM@13: M = int(T[2:]) DaveM@13: elif int(T) <= 24 : DaveM@13: H = int(T) DaveM@13: M = 0 DaveM@13: elif int(T) > 100: DaveM@13: H = int(T)/100 DaveM@13: M = int(T)%100 DaveM@19: else: DaveM@19: return None DaveM@13: if timeFlag is not None: DaveM@13: if timeFlag == 0: DaveM@13: H = H%12 DaveM@13: else: DaveM@13: H = H%12 + 12 DaveM@13: except ValueError: DaveM@19: return None DaveM@21: if H > 24 or M > 60: DaveM@21: return None DaveM@21: else: DaveM@21: return (H,M) DaveM@19: DaveM@13: DaveM@13: