def disting(self,text): sptok = tokenizer(text);tokens = sptok['tokens'] entities= sptok['entities']; types=sptok['types'] dicfin = {'tokens':tokens,'entities':entities,'types':types, 'words':[],'times':[],'numbers':[],'splits':[]} for i in tokens: timefinder = finder(r'[-|/|:|@|\\]',i) if timefinder.found(): dicfin['times'].append(i) wordfinder = finder(r'\w+',i) numberfinder = finder(r'\d+',i) symbolfinder = finder(r',|\.',i) timefinder2 = finder(r'\d(th|st|rd)',i.lower()) if wordfinder.found() and numberfinder.found(): if timefinder2.found(): dicfin['times'].append(i) elif len(wordfinder.result())!=len(numberfinder.result()): dicfin['splits'].append(i) if numberfinder.found() and symbolfinder.found(): dicfin['numbers'].append(i) if numberfinder.found() and (timefinder.found()==False) and (timefinder2.found()==False): if (i in dicfin['splits']) is False and (symbolfinder.found()==False): dicfin['numbers'].append(i) if wordfinder.found() and (numberfinder.found()==False) and (symbolfinder.found()==False): dicfin['words'].append(i) self.history.append(dicfin) self.current = dicfin
def find_Chunks(self,word): chunks=[] for i in range(1,len(self.chunk_list)): findword = finder(r'{0}'.format(word),unicode(self.chunk_list[i])) if findword.found(): chunks.append(self.chunk_list[i]) if len(chunks) > 0: return chunks else: return False
def run(self): hlp = Helper();br=True;results={};resultsort=[];correcting = False while br: if correcting or self.matchgeo: pass else: command = raw_input("SMS in: ") if not self.matchgeo: t,option,word = self.process(unicode(command)) words = t[0];disting = t[1][-1] if option and len(resultsort)==0: self.option = option;self.match = word if len(self.display) > 0 : self.last_Display = self.display self.display = Chunks(self.option.print_Options()) self.display.create_Chunks(heading="By ({0}) Did you mean: ".format(word), footing="Please choose.") print self.display.goto_Chunk(); continue if len(self.option)>0 and len(words[0])==1: ch = self.option.select_Option(words[0]) if ch!=None and len(resultsort)==0: self.main.correctedHistory[self.match]=self.option[ch][2] disting = t[1][-2]; k = disting['tokens'].index(self.match) disting['tokens'][k] = self.option[ch][2] try: k = disting['words'].index(self.match) disting['words'][k] = self.option[ch][2] words = disting['words'] except: disting['words'].append(self.option[ch][2]) words = disting['words'] if self.option[ch][2] == "find": word = "find";self.option = Options() else: self.option = Options();correcting = True command = " ".join(disting['tokens']); continue if ch!=None and type(resultsort)!=unicode: text = "{0} - Ratings : {1}".format(resultsort[ch][0],resultsort[ch][1]) text += "\n" +" Telephone Number: " + results[resultsort[ch][0]][1] text += "\n Address: "+results[resultsort[ch][0]][0] self.display = Chunks(text) self.display.create_Chunks() print self.display.goto_Chunk() self.option = Options();continue if ch!=None and type(resultsort)==unicode: self.matchgeo = [results[ch]]; self.location_History[resultsort] = [results[ch]] disting = t[1][-2]; words = disting['words'];self.option = Options() continue correcting = False if word == "find" or "find" in words: if word == "find": self.find = self.last_Display else: self.find = self.display expand = self.find.find_Chunks(" ".join([i for i in disting['tokens'] if i!="find"])) if expand!=False: self.find.chunk_list = expand print self.find.goto_Chunk() else: print "No results found" continue for k in range(len(words)): dirfin = finder(r'directory|places',words[k]) if dirfin.found(): if "list" in words: self.display = Chunks(",".join(hlp.dirtypes()).replace("_"," ")) self.display.create_Chunks(heading="List of types of places:") print self.display.goto_Chunk(); break direc=Directory([i for i in words if i!="directory" or i!="places"]) if len(self.matchgeo) > 0: direc.locs=self.matchgeo; self.matchgeo = "" results,resultsort = direc.run(disting,self.location_History) if results == None: break elif type(resultsort) == unicode: for i in results: self.option.add_Option(content="{0}".format(i[0].encode('utf-8'))) self.display = Chunks(self.option.print_Options()) self.display.create_Chunks(heading="By {0} did you mean:".format(str(resultsort)), footing="Please choose a location. ") print self.display.goto_Chunk(); break for i in resultsort: self.option.add_Option(content="{0} - Ratings : {1}".format(i[0].encode('utf-8'),str(i[1]))) self.display = Chunks(self.option.print_Options()) self.display.create_Chunks(heading="Nearby places:", footing="Choose for more details. ") print self.display.goto_Chunk(); break outfin = finder(r'outline',words[k]) if outfin.found(): with open("textblock.txt","rb") as f: textblock= f.read().decode("string_escape") for i in range(len(textblock)): if textblock[i:i+2] == "\u": textblock= textblock[:i]+unichr(int(textblock[i+2:i+6],base=16)).encode("utf-8") +textblock[i+6:] f.close() self.table.add_TxtBlock(textblock) if self.table.run([i for i in words if i !="outline"],disting['numbers']) != False: self.display = self.table.run([i for i in words if i !="outline"],disting['numbers']) print self.display.goto_Chunk(); break exifin = finder(r'exit',words[k]) if exifin.found(): br=False;break helpfin = finder(r'help',words[k]) if helpfin.found(): print hlp;break if "next" in words[k]: print self.display.next_Chunk(); break if "goto" in words[k]: try: n = disting['numbers'][0] num = int(n) except: num = -1 print self.display.goto_Chunk(num); break
def time(words,times,current='time'): locs = Corrector('locations');locations = [] while True: found = False;terminate = False for k in words: if locs.match(k) >= 2.33: locations+=k if len(words) ==2 and 'now' in words: print datetime.now(pytz.timezone(timezonelist[location])).strftime(fmt) continue for i in words: finddiff = finder(r'^diff',i) findbet = finder(r'^betw',i) if 'between' in words or finddiff.found(): showtimedate = datetime.now(pytz.timezone(timezonelist[i])).strftime('%Y%m%d') try: loctime1 = datetime.now(pytz.timezone(timezonelist[i])).strftime('%H') loctime2 = datetime.now(pytz.timezone(timezonelist[location])).strftime('%H') final = int(loctime1)-int(loctime2) except: print("error with cities entered") continue finderdate = finder(r'\b[0-9]{8}\b',command) if finderdate.found(): finderdatestring = finderdate.result() try: showtime = datetime(int(finderdatestring[4:]),int(finderdatestring[2:4]), int(finderdatestring[:2]),int(finderstring[:2])-final, int(finderstring[2:]),0,0,pytz.timezone(timezonelist[location])) finderdatestring = finderdatestring[:2] + "/" + finderdatestring[2:4] + "/" + finderdatestring[4:] except ValueError: print("incorrect date entered") found = True continue else: showtime = datetime(int(showtimedate[:4]),int(showtimedate[4:6]), int(showtimedate[6:]),int(finderstring[:2])-final, int(finderstring[2:]),0,0,pytz.timezone(timezonelist[location])) finderdatestring = "" finalshowtime = finderstring + " " + finderdatestring print("{0} in {1} is {2} in {3}".format(finalshowtime,i,showtime.strftime(fmt),location)) if 'help' in words: if 'schedule' in words: print("""entering [city name] will change current location and scheduling is \n based on current location. send [schedule in <4 digit time 1650> ] to schedule \n reminder in 16 hours and 50 mins. send [schedule at <hhmm eg. 1650> \n <ddmmyyyy eg. 25052016]>] to schedule reminder at 16:50 on 25/5/2016 \n add a <text> to either of the above commands to include a message in your reminder""") found = True continue else: print("""Usage: send [<city name>] to set current location and see the time or \n [between <city> <city>] to see the time difference between two cities. Use [<city name> <4 digit \n time hhmm>] to show a specific time in a different city converted to local time \n more help with reminders available with [help schedule]""") found = True continue if 'schedule' in words: if 'at' in words: year = int(datetime.now(pytz.timezone('Europe/London')).strftime('%Y')) findertime1 = finder(r'\b[0-9]{4}\b',command) if findertime1.found()==False: print("The entered time to schedule reminder for should be 4 digits <hhmm>") continue try: assert int(findertime1.result()[2:]) <= 59 and int(findertime1.result()[2:]) >= 0 except: print("The entered minutes must be between 00 and 59") continue try: assert int(findertime1.result()[:2]) <= 23 and int(findertime1.result()[:2]) >=0 except: print("The entered hours must be between 00 and 23") continue finderdate1 = finder(r'\b[0-9]{8}\b',command) if finderdate1.found(): try: assert int(finderdate1.result()[:2]) <= 31 and int(finderdate1.result()[:2]) >=1 except: print("The entered day must be between 01 and 31") continue try: assert int(finderdate1.result()[2:4]) <=12 and int(finderdate1.result()[2:4]) >=1 except: print("The entered month must be between 01 and 12") continue try: assert int(finderdate1.result()[4:]) <= year+5 and int(finderdate1.result()[4:]) >= year except: print("The entered year can only be 5 years in the future") continue reminder = datetime(int(finderdate1.result()[4:]),int(finderdate1.result()[2:4]), int(finderdate1.result()[:2]),int(findertime1.result()[:2]), int(findertime1.result()[2:]),0,0,pytz.timezone(timezonelist[location])) else: finderdate11 = datetime.now(pytz.timezone(timezonelist[location])).strftime('%d%m%Y') reminder = datetime(int(finderdate11[4:]),int(finderdate11[2:4]), int(finderdate11[:2]),int(findertime1.result()[:2]), int(findertime1.result()[2:]),0,0,pytz.timezone(timezonelist[location])) for i in words: if i!='schedule' and re.search(r'[0-9]{4}',i)==None: if (i in grammar) == False: if re.search(r'[0-9]{8}',i)==None: print("reminder ( {0} ) set for {1}".format(i,reminder.strftime(fmt))) terminate=True continue if terminate==False: print("reminder set for {0}".format(reminder.strftime(fmt))) found = True continue elif 'in' in words: findertime2 = finder(r'\b[0-9]{4}\b',command) now = datetime.now(pytz.timezone(timezonelist[location])) if findertime2.found()==False: print("The reminder time <hhmm> must be four digits") continue try: assert int(findertime2.result()[2:]) <=59 except: print("The reminder minutes must be smaller than 60") continue try: assert int(findertime2.result()[:2]) <= 23 and int(findertime2.result()[:2]) >=0 except: print("The entered hours must be between 00 and 23") continue reminder1 = timedelta(hours=int(findertime2.result()[:2]), minutes=int(findertime2.result()[2:])) + now for i in words: if re.search(r'[0-9]{4}',i)==None: if (i in grammar) == False: if re.search(r'[0-9]{8}',i)==None: print("reminder ( {0} ) set for {1}".format(i,reminder1.strftime(fmt))) terminate=True continue if terminate==False: print("reminder set for {0}".format(reminder1.strftime(fmt))) found = True continue if 'between' in words: locs=[] for i in words: if i in locations: locs.append(i) try: loctime1 = datetime.now(pytz.timezone(timezonelist[locs[0]])).strftime('%H') loctime2 = datetime.now(pytz.timezone(timezonelist[locs[1]])).strftime('%H') final = int(loctime1)-int(loctime2) except: print("error with entered cities") continue if final<0: print("{0} is {1} hours {2} {3}".format(locs[0],str(abs(final)),"behind",locs[1])) else: print( "{0} is {1} hours {2} {3}".format(locs[0],str(abs(final)),"ahead of",locs[1])) found = True continue if found == False: print("command not recognized; check spelling/syntax or send [help] for support")