def build24HourTime(s, chrono_id, chrono_list, flags): boo, val, idxstart, idxend = has24HourTime(s, flags) ref_Sspan, ref_Espan = s.getSpan() if boo and not flags["loneDigitYear"]: ## assume format of hhmm or hhmmzzz try: hour = int(val[0:2]) minute = int(val[2:4]) except ValueError: # print("Skipping, not a 24hour time") return chrono_list, chrono_id, flags # hour = w2n.number_formation(val[0:2]) # minute = w2n.word_to_num(val[2:4]) # print("TIME ZONE: {}".format(val)) # tz = hasTimeZone(s) # my_tz_entity = chrono.ChronoTimeZoneEntity(str(chrono_id) + "entity", start_span=tz.span(0)[0] + ref_Sspan, # end_span=tz.span(0)[1] + ref_Sspan) # chrono_list.append(my_tz_entity) # chrono_id = chrono_id + 1 # return chrono_list, chrono_id # #search for time zone # ## Identify if a time zone string exists # tz = hasTimeZone(s) # if tz is not None: # my_tz_entity = chrono.ChronoTimeZoneEntity(str(chrono_id) + "entity", start_span =tz.span(0)[0] + ref_Sspan, end_span=tz.span(0)[1] + ref_Sspan) # chrono_list.append(my_tz_entity) # chrono_id = chrono_id + 1 # else: # my_tz_entity = None ## build minute entity min_entity = chrono.ChronoMinuteOfHourEntity( entityID=str(chrono_id) + "entity", start_span=ref_Sspan + idxstart + 2, end_span=ref_Sspan + idxstart + 4, value=minute) # print("24Minute Value Added: " + str(min_entity.get_value())) chrono_list.append(min_entity) chrono_id = chrono_id + 1 # if my_tz_entity is not None: # hour_entity = chrono.ChronoHourOfDayEntity(entityID=str(chrono_id) + "entity", start_span=ref_Sspan + idxstart, end_span=ref_Sspan + idxstart + 2, value=hour, time_zone=my_tz_entity.get_id()) # else: hour_entity = chrono.ChronoHourOfDayEntity( entityID=str(chrono_id) + "entity", start_span=ref_Sspan + idxstart, end_span=ref_Sspan + idxstart + 2, value=hour) # print("24Hour Value Added: " + str(hour_entity.get_value())) hour_entity.set_sub_interval(min_entity.get_id()) chrono_list.append(hour_entity) chrono_id = chrono_id + 1 return chrono_list, chrono_id, flags
def buildHourOfDay(s, chrono_id, chrono_list, flags): b, text, startSpan, endSpan = hasHourOfDay(s) if b and not flags["hour"]: # print("Found Hour in buildChronoHour") flags["hour"] = True ref_StartSpan, ref_EndSpan = s.getSpan() abs_StartSpan = ref_StartSpan + startSpan abs_EndSpan = abs_StartSpan + abs(endSpan - startSpan) chrono_entity = chrono.ChronoHourOfDayEntity(entityID=str(chrono_id) + "entity", start_span=abs_StartSpan, end_span=abs_EndSpan, value=int(text)) chrono_list.append(chrono_entity) chrono_id = chrono_id + 1 return chrono_list, chrono_id, flags
def build2DigitYear(s, chrono_id, chrono_list, flags): b, text, startSpan, endSpan = has2DigitYear(s) if b and not flags["fourdigityear"]: # In most cases this will be at the end of the Span ref_StartSpan, ref_EndSpan = s.getSpan() abs_StartSpan = ref_StartSpan + startSpan abs_EndSpan = abs_StartSpan + abs(endSpan - startSpan) chrono_2_digit_year_entity = chrono.ChronoTwoDigitYearOperator( entityID=str(chrono_id) + "entity", start_span=abs_StartSpan, end_span=abs_EndSpan, value=text) chrono_id = chrono_id + 1 # Check for Month in same element bMonth, textMonth, startSpanMonth, endSpanMonth = hasMonthOfYear(s) if bMonth and not flags["month"]: flags["month"] = True abs_StartSpanMonth = ref_StartSpan + startSpanMonth abs_EndSpanMonth = abs_StartSpanMonth + abs(endSpanMonth - startSpanMonth) m = utils.getMonthNumber(textMonth) if (m <= 12): chrono_month_entity = chrono.chronoMonthOfYearEntity( entityID=str(chrono_id) + "entity", start_span=abs_StartSpanMonth, end_span=abs_EndSpanMonth, month_type=calendar.month_name[m]) chrono_id = chrono_id + 1 chrono_2_digit_year_entity.set_sub_interval( chrono_month_entity.get_id()) # Check for Day in same element bDay, textDay, startSpanDay, endSpanDay = hasDayOfMonth(s) if bDay and not flags["day"]: flags["day"] = True abs_StartSpanDay = ref_StartSpan + startSpanDay abs_EndSpanDay = abs_StartSpanDay + abs(endSpanDay - startSpanDay) if (int(textDay) <= 31): chrono_day_entity = chrono.ChronoDayOfMonthEntity( entityID=str(chrono_id) + "entity", start_span=abs_StartSpanDay, end_span=abs_EndSpanDay, value=int(textDay)) chrono_id = chrono_id + 1 chrono_month_entity.set_sub_interval( chrono_day_entity.get_id()) # Check for Hour in same element bHour, textHour, startSpanHour, endSpanHour = hasHourOfDay(s) if bHour and not flags["hour"]: flags["hour"] = True ref_StartSpan, ref_EndSpan = s.getSpan() abs_StartSpanHour = ref_StartSpan + startSpanHour abs_EndSpanHour = abs_StartSpanHour + abs(endSpanHour - startSpanHour) if (int(textHour) <= 24): chrono_hour_entity = chrono.ChronoHourOfDayEntity( entityID=str(chrono_id) + "entity", start_span=abs_StartSpanHour, end_span=abs_EndSpanHour, value=int(textHour)) chrono_id = chrono_id + 1 chrono_day_entity.set_sub_interval( chrono_hour_entity.get_id()) # Check for Minute in same element bMinute, textMinute, startSpanMinute, endSpanMinute = hasMinuteOfHour( s) if bMinute and not flags["minute"]: flags["minute"] = True ref_StartSpan, ref_EndSpan = s.getSpan() abs_StartSpanMinute = ref_StartSpan + startSpanMinute abs_EndSpanMinute = abs_StartSpanMinute + abs( endSpanMinute - startSpanMinute) if (int(textMinute) <= 60): chrono_minute_entity = chrono.ChronoMinuteOfHourEntity( entityID=str(chrono_id) + "entity", start_span=abs_StartSpanMinute, end_span=abs_EndSpanMinute, value=int(textMinute)) chrono_id = chrono_id + 1 chrono_hour_entity.set_sub_interval( chrono_minute_entity.get_id()) # Check for Second in same element bSecond, textSecond, startSpanSecond, endSpanSecond = hasSecondOfMinute( s) if bSecond and not flags["second"]: flags["second"] = True ref_StartSpan, ref_EndSpan = s.getSpan() abs_StartSpanSecond = ref_StartSpan + startSpanSecond abs_EndSpanSecond = abs_StartSpanSecond + abs( endSpanSecond - startSpanSecond) if (int(textSecond) <= 60): chrono_second_entity = chrono.ChronoSecondOfMinuteEntity( entityID=str(chrono_id) + "entity", start_span=abs_StartSpanSecond, end_span=abs_EndSpanSecond, value=int(textSecond)) chrono_list.append(chrono_second_entity) chrono_id = chrono_id + 1 chrono_minute_entity.set_sub_interval( chrono_second_entity.get_id()) chrono_list.append(chrono_minute_entity) chrono_list.append(chrono_hour_entity) chrono_list.append(chrono_day_entity) chrono_list.append(chrono_month_entity) chrono_list.append(chrono_2_digit_year_entity) return chrono_list, chrono_id, flags
def buildAMPM(s, chrono_id, chrono_list, flags): am_flag = True ref_Sspan, ref_Espan = s.getSpan() ## Identify if a time zone string exists # tz = hasTimeZone(s) # if tz is not None: # my_tz_entity = chrono.ChronoTimeZoneEntity(str(chrono_id) + "entity", start_span =tz.span(0)[0] + ref_Sspan, end_span=tz.span(0)[1] + ref_Sspan) # chrono_list.append(my_tz_entity) # chrono_id = chrono_id + 1 # else: # my_tz_entity = None boo, val, idxstart, idxend = hasAMPM(s) if boo: if val == "PM": am_flag = False abs_Sspan = ref_Sspan + idxstart abs_Espan = ref_Sspan + idxend my_AMPM_entity = chrono.ChronoAMPMOfDayEntity(entityID=str(chrono_id) + "entity", start_span=abs_Sspan, end_span=abs_Espan, ampm_type=val) chrono_id = chrono_id + 1 chrono_list.append(my_AMPM_entity) print("In AMPM") #check to see if it has a time associated with it. We assume the time comes before the AMPM string #We could parse out the time from the TimePhrase normalized value. The problem is getting the correct span. #idx_start is the first index of the ampm. If there are any characters before it, it will be greater than 0. if idxstart > 0 and not flags['hour']: substr = s.getText()[0:idxstart] m = re.search('([0-9]{1,4})', substr) if m is not None : time_val = m.group(0) if len(time_val) <=2: if int(time_val) <= 12: abs_Sspan = ref_Sspan + m.span(0)[0] abs_Espan = ref_Sspan + m.span(0)[1] #print("Adding Hour in AMPM") my_hour_entity = chrono.ChronoHourOfDayEntity(entityID=str(chrono_id) + "entity", start_span=abs_Sspan, end_span=abs_Espan, value=time_val, ampm=my_AMPM_entity.get_id()) chrono_id = chrono_id + 1 chrono_list.append(my_hour_entity) flags["hour"] = True elif len(time_val) == 3: print("My Time_val: " + time_val) k = re.search('([0-9])([0-9]{2})', time_val) print("K0: " + k.group(0)) print("K1: " + k.group(1)) print("K2: " + k.group(2)) if int(k.group(2)) < 60: abs_Sspan1 = ref_Sspan + k.span(2)[0] abs_Espan1 = ref_Sspan + k.span(2)[1] print("Adding Minute in AMPM") my_minute_entity = chrono.ChronoMinuteOfHourEntity(entityID=str(chrono_id) + "entity", start_span=abs_Sspan1, end_span=abs_Espan1, value=k.group(2)) chrono_id = chrono_id + 1 chrono_list.append(my_minute_entity) flags["minute"] = True if int(k.group(1)) <= 12: abs_Sspan = ref_Sspan + k.span(1)[0] abs_Espan = ref_Sspan + k.span(1)[1] print("Adding Hour in AMPM") my_hour_entity = chrono.ChronoHourOfDayEntity(entityID=str(chrono_id) + "entity", start_span=abs_Sspan, end_span=abs_Espan, value=k.group(1), ampm=my_AMPM_entity.get_id(), sub_interval=my_minute_entity) chrono_id = chrono_id + 1 chrono_list.append(my_hour_entity) flags["hour"] = True elif len(time_val) == 4: k = re.search('([0-9]{2})([0-9]{2})', time_val) if int(k.group(2)) < 60: abs_Sspan1 = ref_Sspan + k.span(2)[0] abs_Espan1 = ref_Sspan + k.span(2)[1] print("Adding Minute in AMPM") my_minute_entity = chrono.ChronoMinuteOfHourEntity(entityID=str(chrono_id) + "entity", start_span=abs_Sspan1, end_span=abs_Espan1, value=k.group(2)) chrono_id = chrono_id + 1 chrono_list.append(my_minute_entity) flags["minute"] = True if int(k.group(1)) <= 12: abs_Sspan = ref_Sspan + k.span(1)[0] abs_Espan = ref_Sspan + k.span(1)[1] print("Adding Hour in AMPM") my_hour_entity = chrono.ChronoHourOfDayEntity(entityID=str(chrono_id) + "entity", start_span=abs_Sspan, end_span=abs_Espan, value=k.group(1), ampm=my_AMPM_entity.get_id(), sub_interval=my_minute_entity) chrono_id = chrono_id + 1 chrono_list.append(my_hour_entity) flags["hour"] = True #else search for a text number else: texNumVal = utils.getNumberFromText(substr) if texNumVal is not None: #create the hour entity if not flags['hour']: my_hour_entity = chrono.ChronoHourOfDayEntity(entityID=str(chrono_id) + "entity", start_span=ref_Sspan, end_span=ref_Sspan + (idxstart - 1), value=texNumVal, ampm=my_AMPM_entity.get_id()) chrono_id = chrono_id + 1 chrono_list.append(my_hour_entity) flags["hour"] = True return chrono_list, chrono_id
def buildAMPM(s, chrono_id, chrono_list, flags): am_flag = True ref_Sspan, ref_Espan = s.getSpan() ## Identify if a time zone string exists # tz = hasTimeZone(s) # if tz is not None: # my_tz_entity = chrono.ChronoTimeZoneEntity(str(chrono_id) + "entity", start_span =tz.span(0)[0] + ref_Sspan, end_span=tz.span(0)[1] + ref_Sspan) # chrono_list.append(my_tz_entity) # chrono_id = chrono_id + 1 # else: # my_tz_entity = None boo, val, idxstart, idxend = hasAMPM(s) if boo: if val == "PM": am_flag = False abs_Sspan = ref_Sspan + idxstart abs_Espan = ref_Sspan + idxend my_AMPM_entity = chrono.ChronoAMPMOfDayEntity(entityID=str(chrono_id) + "entity", start_span=abs_Sspan, end_span=abs_Espan, ampm_type=val) chrono_id = chrono_id + 1 chrono_list.append(my_AMPM_entity) #check to see if it has a time associated with it. We assume the time comes before the AMPM string #We could parse out the time from the TimePhrase normalized value. The problem is getting the correct span. #idx_start is the first index of the ampm. If there are any characters before it, it will be greater than 0. if idxstart > 0 and not flags['hour']: substr = s.getText()[0:idxstart] m = re.search('([0-9]{1,2})', substr) if m is not None: hour_val = m.group(0) abs_Sspan = ref_Sspan + m.span(0)[0] abs_Espan = ref_Sspan + m.span(0)[1] my_hour_entity = chrono.ChronoHourOfDayEntity( entityID=str(chrono_id) + "entity", start_span=abs_Sspan, end_span=abs_Espan, value=hour_val, ampm=my_AMPM_entity.get_id()) chrono_id = chrono_id + 1 chrono_list.append(my_hour_entity) #else search for a text number else: texNumVal = utils.getNumberFromText(substr) if texNumVal is not None: #create the hour entity if not flags['hour']: my_hour_entity = chrono.ChronoHourOfDayEntity( entityID=str(chrono_id) + "entity", start_span=ref_Sspan, end_span=ref_Sspan + (idxstart - 1), value=texNumVal, ampm=my_AMPM_entity.get_id()) chrono_id = chrono_id + 1 chrono_list.append(my_hour_entity) return chrono_list, chrono_id