def get_absolute_year(self, entity, commonParser=None): matcher = self.absolute_year_pat.match(entity) if matcher: if matcher.group(1): year_ = digitconv.getNumFromHan(matcher.group(1)) if commonParser: if year_ < 100: year_ple_tw = 2000 + year_ year_ple_ni = 1900 + year_ if abs(commonParser.date.year - year_ple_tw) >= abs(commonParser.date.year - year_ple_ni): commonParser.date = commonParser.date.replace( year=year_ple_ni) else: commonParser.date = commonParser.date.replace( year=year_ple_tw) else: commonParser.date = commonParser.date.replace( year=year_) commonParser.timeUnit[1] = True return year_ else: matcher = self.mix_year_pat.match(entity) if matcher: year_ = digitconv.getNumFromHan(matcher.group("year")) if commonParser: if year_ < 100: year_ple_tw = 2000 + year_ year_ple_ni = 1900 + year_ if abs(commonParser.date.year - year_ple_tw) >= abs(commonParser.date.year - year_ple_ni): commonParser.date = commonParser.date.replace( year=year_ple_ni) else: commonParser.date = commonParser.date.replace( year=year_ple_tw) else: commonParser.date = commonParser.date.replace( year=year_) commonParser.timeUnit[1] = True if matcher.group("month"): month = digitconv.getNumFromHan(matcher.group("month")) commonParser.date = commonParser.date.replace( month=month) commonParser.timeUnit[2] = True if matcher.group("day"): day = digitconv.getNumFromHan(matcher.group("day")) commonParser.date = commonParser.date.replace( day=day) commonParser.timeUnit[3] = True return year_ return None
def get_dev_numlist(self, numberlist): """ Function: entity has the number list "一、二、三...." Parameters: 1.numberlist: list, '0-9零一二三四五六七八九十' Return: start_month, end_month """ season_start = digitconv.getNumFromHan(numberlist[0]) - 1 season_end = digitconv.getNumFromHan(numberlist[-1]) - 1 start_month = self.season_dict[season_start][0] end_month = self.season_dict[season_end][1] return start_month, end_month
def frequency_output(self, prefix_word, number, suffix_word): """ Function: Process the time frequency, and get the output of the frequency. Parameters: 1.prefix_word: str, "每|每隔" 2.number: str, "个|0-9零一二三四五六七八九|半" 3.suffix_word: str, "年|寒暑假|季度|月|周|星期|礼拜|日|晚上|时|分|秒" Return: 1.time_fre_list: list, represent the "年、月、周、日、时、分、秒" """ time_fre_list = [0, 0, 0, 0, 0, 0, 0] if (prefix_word == u'每逢'): time_indx = self.fre_indx_whenever[suffix_word] time_fre_list[time_indx[0]] = time_indx[1] else: shift = 0.0 if (prefix_word == u'每隔'): shift = 1.0 fre_num = 1.0 try: fre_num = int(digitconv.getNumFromHan(number)) except: if (number == u'半' and prefix_word == u'每隔'): fre_num = 0.5 shift = 0.5 elif (number == u'半'): fre_num = 0.5 time_indx = self.fre_indx_dict[suffix_word] time_fre_list[time_indx[0]] = shift + fre_num * time_indx[1] return time_fre_list
def get_relative_year(self, entity, basetime='2018-11-01 12:00:00', commonParser=None): time = arrow.get(basetime) base_year = time.year util = util_tools.Util() matcher = self.relative_year_pat.match(entity) decorate_deviation = '' if matcher: if matcher.group(1): decorate_deviation = matcher.group(1) year_shift = util.rel_deviation(matcher.group(2), decorate_deviation) if commonParser: commonParser.timeUnit[1] = True commonParser.date = commonParser.date.shift(years=year_shift) return year_shift + base_year else: matcher = self.relative_year_diviation_pat.match(entity) if matcher: year_ = 1 if matcher.group(1): year_ = digitconv.getNumFromHan(matcher.group(1)) year_shift = util.rel_deviation_num(matcher.group(2), year_) if commonParser: commonParser.timeUnit[7] = True commonParser.date = commonParser.date.shift( years=year_shift) return base_year + year_shift return None
def get_dev_up2down(self, basemonth, deviation): """ Function: process the sample like "上*", "下*" and "这" Parameters: 1.basemonth: str, current month of basetime 2.deviation: str, get the value according to the pattern "[1-4一二三四|下|上|这]" """ #get the now season according to base month shift_season = 0 basemonth = int(basemonth) for season in self.season_dict.keys(): if (basemonth >= self.season_dict[season][0] and basemonth <= self.season_dict[season][1]): shift_season = season break if u'上' in deviation: for i in range(len(deviation)): shift_season -= 1 elif u'下' in deviation: for i in range(len(deviation)): shift_season += 1 elif deviation in u'1-4一二三四': shift_season = digitconv.getNumFromHan(deviation) - 1 #get the shift of year and season year = shift_season / 4 shift_season = shift_season % 4 return year, shift_season
def get_dev_aroundlist(self, year, season_number, around_text): """ Function: entity has the around_text "去|今|明" "前|后" Parameters: 1.year: int 2.season_number: str, '0-9零一二三四五六七八九十' 3.around_text: list, ["去|今", "前|后"] Return: start_year, end_year, start_month, end_month """ start_month = 0 end_month = 0 start_year = 0 end_year = 0 if (around_text[0] == u'去'): start_year = year - 1 end_year = year - 1 elif (around_text[0] == u'今'): start_year = year end_year = year elif (around_text[0] == u'明'): start_year = year + 1 end_year = year + 1 shift = digitconv.getNumFromHan(season_number) - 1 if (around_text[1] == u'前'): start_month = 1 end_month = 3 + 3 * shift elif (around_text[1] == u'后'): start_month = 10 - 3 * shift end_month = 12 return start_year, end_year, start_month, end_month
def get_relative_month(self, entity, basetime='2018-11-01 12:00:00', commonParser=None): matcher = self.relative_month_pat.match(entity) decorate_deviation,deviation = '','' util = util_tools.Util() time = arrow.get(basetime) base_month = time.month if matcher: if matcher.group(1): decorate_deviation = matcher.group(1) shift_month = util.rel_deviation(matcher.group(2), decorate_deviation) if commonParser: commonParser.timeUnit[2] = True if shift_month != 0: commonParser.date = commonParser.date.shift(months=shift_month) return base_month + shift_month else: matcher = self.relative_month_diviation_pat.match(entity) if matcher: month_ = digitconv.getNumFromHan(matcher.group(1)) shift_month = util.rel_deviation_num(matcher.group(2), month_) if commonParser: commonParser.timeUnit[7] = True commonParser.date = commonParser.date.shift(months=shift_month) return shift_month + base_month return None
def get_absolute_second(self, entity, commonParser=None): matcher = self.absolute_second_pat.match(entity) if matcher: chsecond = matcher.group(1) second_ = digitconv.getNumFromHan(chsecond) if commonParser: commonParser.timeUnit[7] = True commonParser.date = commonParser.date.replace(second=second_) return second_ return None
def get_absolute_minute(self, entity, commonParser=None): matcher = self.absolute_minute_pat.match(entity) ke_matcher = self.ke_pat.match(entity) if matcher: minute_ = digitconv.getNumFromHan(matcher.group(1)) if commonParser: if matcher.group(2): commonParser.timeUnit[7] = True else: commonParser.timeUnit[6] = True if minute_ == 60: commonParser.date = commonParser.date.replace( hour=commonParser.date.hour + 1).replace(minute=0) elif 0 <= minute_ < 60: commonParser.date = commonParser.date.replace( minute=minute_) return None else: commonParser.timeFormatInfo = u'时间表述异常,分钟应该在[0,60]范围内!' return None return minute_ elif ke_matcher: minuteToParse = digitconv.getNumFromHan(ke_matcher.group("minute")) minute_ = minuteToParse * 15 if commonParser: if matcher.group(2): commonParser.timeUnit[7] = True else: commonParser.timeUnit[6] = True if minute_ == 60: commonParser.date = commonParser.date.replace( hour=commonParser.date.hour + 1).replace(minute=0) elif 0 <= minute_ < 60: commonParser.date = commonParser.date.replace( minute=minute_) return None else: commonParser.timeFormatInfo = u'时间表述异常,分钟应该在[0,60]范围内!' return None return minute_ return None
def get_absolute_day(self, entity, commonParser=None): matcher = self.absolute_day_pat.match(entity) if matcher: if matcher.group("day"): day_ = matcher.group("day")[1:] day = digitconv.getNumFromHan(day_) if commonParser: commonParser.date = commonParser.date.replace(day=day) commonParser.timeUnit[3] = True commonParser.lunar = True return day_ return None
def get_absolute_day(self, entity, commonParser=None): matcher = self.absolute_day_pat.match(entity) if matcher: if matcher.group(1): day_ = digitconv.getNumFromHan(matcher.group(1)) if commonParser: commonParser.timeUnit[3] = True if 1 <= day_ <= 31: commonParser.date = commonParser.date.replace(day=day_) else: commonParser.timeFormatInfo = u'时间表述异常,日应该在[1,31]范围内!' return None return day_ return None
def get_absolute_month(self, entity, commonParser=None): matcher = self.absolute_month_pat.match(entity) if matcher: if matcher.group(1): month_ = digitconv.getNumFromHan(matcher.group(1)) if commonParser: commonParser.timeUnit[2] = True if 1 <= month_ <= 12: commonParser.date = commonParser.date.replace(month=month_) else: commonParser.timeFormatInfo = u'时间表述异常,月份应该在[1,12]范围内!' return None return month_ return None
def duration_output(self, number, suffix_word): """ Function: Process the time duration, and get the output of the duration. Parameters: 1.number: str, samples like the "2"; list, [number1, number2] and samples like "2.3"; list, [number1, '半'] and samples like the "一个半" 2.suffix_word: str, "世纪|年|周年|月|星期|礼拜|周末|周|天|工作日|夜|晚|小时|分钟|秒" Return: 1.time_dur_list: list, represent the "年、月、周、日、时、分、秒" """ time_dur_list = [0, 0, 0, 0, 0, 0, 0] shift = 0.0 if (type(number) == list): if (number[1] == u'个半'): #process the "一个半" shift = digitconv.getNumFromHan(number[0]) + 0.5 elif (number[1] == u'整'): shift = digitconv.getNumFromHan(number[0]) else: shift = str(digitconv.getNumFromHan(number[0])) + "." + \ str(digitconv.getNumFromHan(number[1])) shift = float(shift) else: if (number == u'半'): shift = 0.5 elif (number == u'整'): shift = 1.0 else: shift = float(digitconv.getNumFromHan(number)) time_indx = self.duration_dict[suffix_word] time_dur_list[time_indx[0]] = shift * time_indx[1] return time_dur_list
def season_end(self, basetime, season_number, season_word): """ Function: process the "季度末" Parameter: 1.basetime: str 2.season_number: str, '[1-4一二三四]'; 3.season_word: str, "季度末" """ year = arrow.get(basetime).year if (season_word == u'季度末'): season_number = digitconv.getNumFromHan(season_number) start_month = self.season_dict[season_number - 1][1] end_month = self.season_dict[season_number - 1][1] return year, year, start_month, end_month
def get_dev_around(self, year, basemonth, season_number, around_text): """ Function: entity has the around_text "前|后" Parameters: 1.year: int 2.basemonth: str, 3.season_number: str, '0-9零一二三四五六七八九十' or "上下" 4.around_text: str, "前|后" or "去" Return: start_year, end_year, start_month, end_month """ basemonth = int(basemonth) start_month = 0 end_month = 0 for season in self.season_dict.keys(): if (basemonth >= self.season_dict[season][0] and basemonth <= self.season_dict[season][1]): start_month = self.season_dict[season][0] end_month = self.season_dict[season][1] break shift = 0 #if the season_number have the "上下" if (u'上' in season_number or u'下' in season_number): if (u'上' in season_number): shift = -(len(season_number) + 1) end_month = start_month - 1 start_month = start_month + shift * 3 if (u'下' in season_number): shift = len(season_number) + 1 start_month = (end_month + 1) % 12 end_month = end_month + shift * 3 else: if (around_text == u'前' or around_text == u'上'): shift = -digitconv.getNumFromHan(season_number) end_month = start_month - 1 start_month = start_month + shift * 3 elif (around_text == u'后' or around_text == u'下'): shift = digitconv.getNumFromHan(season_number) start_month = (end_month + 1) % 12 end_month = end_month + shift * 3 elif (around_text == u'去'): shift = digitconv.getNumFromHan(season_number) - 1 start_month = self.season_dict[shift][0] end_month = self.season_dict[shift][1] year = year - 1 return year, year, start_month, end_month #carry and abdicate the year start_year = year end_year = year if (start_month <= 0 and end_month >= 0): start_month += -1 start_year = year - (abs(start_month) / 12 + 1) end_year = year start_month = 12 - abs(start_month + 1) % 12 elif (start_month >= 0 and end_month >= 12): start_year = year + (end_month / 12) end_year = year + (end_month / 12) end_month = end_month % 12 #when the end_month=12, the value of it will be 0. so reset the value of end_month if (end_month == 0): end_month = 12 end_year -= 1 return start_year, end_year, start_month, end_month
def get_absolute_hour(self, entity, commonParser=None): hour_xu_matcher = self.hour_xu_pat.match(entity) qunima_matcher = self.qunima_pat.match(entity) gundan_matcher = self.gundan_pat.match(entity) hour_minute_guo_matcher = self.hour_minute_guo_pat.match(entity) hour_guo_ke_matcher = self.hour_guo_ke_pat.match(entity) hour_minute_matcher = self.hour_minute_pat.match(entity) hour_ke_matcher = self.hour_ke_pat.match(entity) hour_matcher = self.hour_pat.match(entity) apm = Apm() if qunima_matcher: minuteToParse = digitconv.getNumFromHan(qunima_matcher.group("minute")) chhour = qunima_matcher.group(4) hour_ = digitconv.getNumFromHan(chhour) minute_ = digitconv.getNumFromHan(minuteToParse) if qunima_matcher.group("guo"): guocha = qunima_matcher.group("guo") if guocha == u'差': hourcha = minute_ / 60; hour_ = hour_ - hourcha - 1; minute_ = 60 - minute_ % 60; return None if commonParser: if commonParser.timeUnit[4]: hour_ = apm.adjustHours(entity, hour_, commonParser) commonParser.timeUnit[5] = True commonParser.timeUnit[6] = True if minute_ == 60: hour_ = hour_ + 1 elif 0 <= minute_ < 60: commonParser.date = commonParser.date.replace(hour=hour_).replace(minute=minute_) return None else: commonParser.timeFormatInfo = u'时间表述异常,分钟应该在[0,60]范围内!' return None if hour_ == 24: commonParser.date = commonParser.date.replace(days=commonParser.date.day + 1).replace( hour=0).replace(minute=0) else: if 0 <= hour_ < 24: commonParser.date = commonParser.date.replace( hour=hour_).replace(minute=0) else: commonParser.timeFormatInfo = u'时间表述异常,小时应该在[0,24]范围内!' return None return hour_ elif gundan_matcher: minuteToParse = digitconv.getNumFromHan(qunima_matcher.group("minute")) if minuteToParse != 0: minute_ = minuteToParse * 15 chhour = qunima_matcher.group(4) hour_ = digitconv.getNumFromHan(chhour) if gundan_matcher.group("guo"): guocha = qunima_matcher.group("guo") if guocha == u'差': hourcha = minute_ / 60 hour_ = hour_ - hourcha - 1 minute_ = 60 - minute_ % 60 return None if commonParser: if commonParser.timeUnit[4]: hour_ = apm.adjustHours(entity, hour_, commonParser) commonParser.timeUnit[5] = True commonParser.timeUnit[6] = True if minute_ == 60: hour_ = hour_ + 1 elif 0 <= minute_ < 60: commonParser.date = commonParser.date.replace(hour=hour_).replace(minute=minute_) return None else: commonParser.timeFormatInfo = u'时间表述异常,分钟应该在[0,60]范围内!' return None if hour_ == 24: commonParser.date = commonParser.date.replace(days=commonParser.date.day + 1).replace( hour=0).replace(minute=0) else: if 0 <= hour_ < 24: commonParser.date = commonParser.date.replace( hour=hour_).replace(minute=0) else: commonParser.timeFormatInfo = u'时间表述异常,小时应该在[0,24]范围内!' return None return hour_ elif hour_minute_guo_matcher: hourToParse = hour_minute_guo_matcher.group(1) hour_ = digitconv.getNumFromHan(hourToParse) minute_ = digitconv.getNumFromHan(hour_minute_guo_matcher.group("minute")) if hour_minute_guo_matcher.group("guo"): guocha = hour_minute_guo_matcher.group("guo") if guocha == u"差" or guocha == u"少": hourcha = minute_ / 60 hour_ = hour_ - hourcha-1 minute_ = 60-minute_ % 60 if commonParser: if commonParser.timeUnit[4]: hour_ = apm.adjustHours(entity, hour_, commonParser) commonParser.timeUnit[5] = True commonParser.timeUnit[6] = True if minute_ == 60: hour_ = hour_ + 1 elif 0 <= minute_ < 60: commonParser.date = commonParser.date.replace(hour=hour_).replace(minute=minute_) return None else: commonParser.timeFormatInfo = u'时间表述异常,分钟应该在[0,60]范围内!' return None if hour_ == 24: commonParser.date = commonParser.date.replace(days=commonParser.date.day + 1).replace( hour=0).replace(minute=0) else: if 0 <= hour_ < 24: commonParser.date = commonParser.date.replace( hour=hour_).replace(minute=0) else: commonParser.timeFormatInfo = u'时间表述异常,小时应该在[0,24]范围内!' return None return hour_ elif hour_guo_ke_matcher: chhour = hour_guo_ke_matcher.group(1) hour_ = digitconv.getNumFromHan(chhour) minuteToParse = digitconv.getNumFromHan(hour_guo_ke_matcher.group("minute")) minute_ = minuteToParse * 15 if hour_guo_ke_matcher.group("guo"): guocha = hour_guo_ke_matcher.group("guo") if guocha == u"差" or guocha == u"少": hourcha = minute_ / 60 hour_ = hour_ - hourcha - 1 minute_ = 60 - minute_ % 60 if commonParser: if commonParser.timeUnit[4]: hour_ = apm.adjustHours(entity, hour_, commonParser) commonParser.timeUnit[5] = True commonParser.timeUnit[6] = True if minute_ == 60: hour_ = hour_ + 1 elif 0 <= minute_ < 60: commonParser.date = commonParser.date.replace(hour=hour_).replace(minute=minute_) return None else: commonParser.timeFormatInfo = u'时间表述异常,分钟应该在[0,60]范围内!' return None if hour_ == 24: commonParser.date = commonParser.date.replace(days=commonParser.date.day + 1).replace( hour=0).replace(minute=0) else: if 0 <= hour_ < 24: commonParser.date = commonParser.date.replace( hour=hour_).replace(minute=0) else: commonParser.timeFormatInfo = u'时间表述异常,小时应该在[0,24]范围内!' return None return hour_ elif hour_minute_matcher: chhour = hour_minute_matcher.group(1) hour_ = digitconv.getNumFromHan(chhour) chminute = hour_minute_matcher.group("minute") minute_ = digitconv.getNumFromHan(chminute) if commonParser: if commonParser.timeUnit[4]: hour_ = apm.adjustHours(entity, hour_, commonParser) commonParser.timeUnit[5] = True commonParser.timeUnit[6] = True if minute_ == 60: hour_ = hour_+1 elif 0 <= minute_ < 60: commonParser.date = commonParser.date.replace(hour=hour_).replace(minute=minute_) return None else: commonParser.timeFormatInfo = u'时间表述异常,分钟应该在[0,60]范围内!' return None if hour_ == 24: commonParser.date = commonParser.date.replace(days=commonParser.date.day+1).replace(hour=0).replace(minute=0) else: if 0 <= hour_ < 24: commonParser.date = commonParser.date.replace( hour=hour_).replace(minute=0) else: commonParser.timeFormatInfo = u'时间表述异常,小时应该在[0,24]范围内!' return None return hour_ elif hour_ke_matcher: hour_ = digitconv.getNumFromHan(hour_ke_matcher.group(1)) chminute = hour_ke_matcher.group("minute") minute_ = digitconv.getNumFromHan(chminute) * 15 if commonParser: if commonParser.timeUnit[4]: hour_ = apm.adjustHours(entity, hour_, commonParser) commonParser.timeUnit[5] = True commonParser.timeUnit[6] = True if minute_ == 60: hour_ = hour_+1 elif 0 <= minute_ < 60: commonParser.date = commonParser.date.replace(hour=hour_).replace(minute=minute_) return None else: commonParser.timeFormatInfo = u'时间表述异常,分钟应该在[0,60]范围内!' return None if hour_ == 24: commonParser.date = commonParser.date.replace(days=commonParser.date.day+1).replace(hour=0).replace(minute=0) else: if 0 <= hour_ < 24: commonParser.date = commonParser.date.replace( hour=hour_).replace(minute=0) else: commonParser.timeFormatInfo = u'时间表述异常,小时应该在[0,24]范围内!' return None return hour_ elif hour_matcher: chhour = hour_matcher.group(1) hour_ = digitconv.getNumFromHan(chhour) minuteToParse = hour_matcher.group(2) hour_ = apm.adjustHours(entity, hour_, commonParser) if minuteToParse == u"半": minute_ = 30 else: minute_ = digitconv.getNumFromHan(minuteToParse) if commonParser: commonParser.timeUnit[5] = True commonParser.timeUnit[6] = True commonParser.date = commonParser.date.replace(hour=hour_).replace(minute=minute_) return hour_ elif hour_xu_matcher: chhour = hour_xu_matcher.group(1) hour_ = digitconv.getNumFromHan(chhour) if commonParser: if commonParser.timeUnit[4]: hour_ = apm.adjustHours(entity, hour_, commonParser) commonParser.timeUnit[5] = True if u'许' in entity or u'整' in entity: commonParser.timeUnit[6] = True commonParser.timeUnit[7] = True if hour_ == 24: commonParser.date = commonParser.date.shift(days=1).replace(hour=0) else: commonParser.date = commonParser.date.replace(hour=hour_) return hour_ return None