Esempio n. 1
0
    def start_with_minute(self, word):
        if len(word) == 0 or not self.H.touch:
            return word

        f = re.findall(r'^([^分::]+)(:|:|分|分钟)', word)
        if len(f) > 0:
            g = cn2dig.Parse(f[0][0])
            if g >= 0:
                self.maybe_this_hour(0)
                self.M.Is(g)
                word = word[len(f[0][0]) + len(f[0][1]):]
            return word

        if word == '半':
            self.M.Is(30)
            return ''
        f = re.findall(r'^(.+)刻$', word)
        if f:
            g = cn2dig.Parse(f[0])
            if g >= 1 and g <= 3:
                self.M.Is(g * 15)
                return ''
        g = cn2dig.Parse(word)
        if g >= 0 and g <= 59:
            self.M.Is(g)
            return ''
        return word
Esempio n. 2
0
    def start_with_day(self, word):
        if len(word) == 0:
            return word

        f = word.find('日')
        if f < 0:
            f = word.find('号')

        if f > 0:
            g = cn2dig.Parse(word[:f])
            if g >= 0:
                self.maybe_this_month(0)
                self.d.Is(g, word[:f+1])
                word = word[f+1:]

        if self.m.touch and word:
            g = cn2dig.Parse(word)
            if g >= 0:
                self.maybe_this_month(0)
                self.d.Is(g)
                return ''
        else:
            d, ot = start_with(word, wday)
            if d != None:
                self.maybe_this_day(ot, d)
                word = word[len(d):]

        return word
Esempio n. 3
0
    def start_with_second(self, word):
        if len(word) == 0 or not self.M.touch:
            return word

        f = re.findall(r'^([^秒]+)(秒|秒钟)', word)
        if len(f) > 0:
            g = cn2dig.Parse(f[0][0])
            if g >= 0:
                word = word[len(f[0][0]) + len(f[0][1]):]
            return word

        if word == '半':
            return ''
        g = cn2dig.Parse(word)
        if g >= 0 and g <= 59:
            return ''
        return word
Esempio n. 4
0
    def start_with_week(self, word):
        if len(word) == 0:
            return word

        offset = 1 - self.d.weekday # 本周周一的相对偏移

        w, ot = start_with(word, wweek)
        if w != None:
            offset = (1 - self.d.weekday) + 7 * ot # 指定周的周一相对偏移
            word = word[len(w):]

        if len(word) == 0:
            self.maybe_this_day(0, w)
            self.d.offsetrange = [offset, offset+6] # 指定周的周一到周日
            return word

        if w != None and len(word) > 0:
            if word[0] in ['末', '天', '日'] or cn2dig.Parse(word[0]) >= 0:
                word = '周' + word

        if word[:3] in ['上半周', '下半周', '前半周', '后半周']:
            self.maybe_this_day(0)
            if word[:1] in ['上', '前']:
                self.d.offsetrange = [offset, offset+2] # 指定周的周一到周三
            else:
                self.d.offsetrange = [offset+3, offset+6] # 指定周的周四到周日
            return word[3:]

        f = re.findall(r'^(星期|礼拜|周)(.)', word)
        if len(f) == 0:
            return word

        if f[0][1] in ['末']:
            self.maybe_this_day(0)
            self.d.offsetrange = [offset+5, offset+6] # 指定周的周六到周日
            return word[len(f[0][0])+1:]

        if f[0][1] in ['天', '日']:
            g = 7
        else:
            g = cn2dig.Parse(f[0][1])
        if g >= 1 and g <= 7:
            self.maybe_this_day(offset + g - 1)
            return word[len(f[0][0])+1:]

        return word
Esempio n. 5
0
    def start_with_hour(self, word):
        if len(word) == 0:
            return word

        H, ot = start_with(word, whour) # ot 为建议的猜测小时范围
        if H != None:
            self.maybe_this_day(0)
            self.H.Limit(H)
            word = word[len(H):]
            if len(word) == 0:
                return word

        if ot != -1 and (ot[1] - ot[0] < 5):
            ot = (ot[0] + ot[1])/2

        of = 2
        f = word.find('小时')
        if f < 0:
            f = word.find('点钟')
        if f < 0:
            of = 1
        if f < 0:
            f = word.find('时')
        if f < 0:
            f = word.find('点')
        if f < 0:
            f = word.find(':')
        if f < 0:
            f = word.find(':')

        if f > 0:
            ff = re.findall(r'([ap]\.?m\.?)$', word.lower())
            if ff:
                if ff[0][0] == 'a':
                    H, ot = '上午', 10
                else:
                    H, ot = '下午', 15
                self.maybe_this_day(0)
                self.H.Limit(H)
                word = word[:-len(ff[0])]

            g = cn2dig.Parse(word[:f])
            if g >= 0:
                if g <= 12:
                    if H != None: # 存在推断词
                        if ot >= 20:
                            if g <= 5: # 今晚5点,指明天凌晨5点
                                self.d.offset += 1
                            else:
                                g += 12 # 今晚6点,判定指18点
                        elif ot >= 12: # 指明中午/下午
                            if H == '中午' and g > 10: # 中午11点,自然是指上午11点
                                pass
                            else:
                                g += 12
                        else: # 指明早上
                            pass
                    else:
                        if not (f > 1 and word[0] == '0'):
                            self.H.amorpm = True
                self.maybe_this_day(0)
                self.H.Is(g, word[:f+of])
                word = word[f+of:]

        return word
Esempio n. 6
0
    def start_with_month(self, word):
        if len(word) == 0:
            return word

        f = word.find('月')
        if f > 0:
            g = cn2dig.Parse(word[:f])
            if g >= 0:
                self.maybe_this_year(0)
                self.m.Is(g, word[:f+1])
                word = word[f+1:]
        if not self.m.touch:
            m, ot = start_with(word, wmonth)
            if m != None:
                self.maybe_this_month(ot, m)
                word = word[len(m):]

        if len(word) == 0:
            return word

        def limit_a():
            s = start_in(word, ['前半月', '上半月', '前半个月', '上半个月'])
            if s != None:
                return '上半月', len(s)
            s = start_in(word, ['后半月', '下半月', '后半个月', '下半个月'])
            if s != None:
                return '下半月', len(s)
            s = start_in(word, ['上旬'])
            if s != None:
                return '上旬', len(s)
            s = start_in(word, ['月初', '月首', '月头'])
            if s != None:
                return '月初', len(s)
            s = start_in(word, ['中旬', '月中'])
            if s != None:
                return '中旬', len(s)
            s = start_in(word, ['下旬'])
            if s != None:
                return '下旬', len(s)
            s = start_in(word, ['月末', '月底', '月尾'])
            if s != None:
                return '月末', len(s)
            return '', 0

        def limit_b():
            s = start_in(word, ['初', '首', '头'])
            if s != None:
                return '月初', len(s)
            s = start_in(word, ['中'])
            if s != None:
                return '中旬', len(s)
            s = start_in(word, ['末', '底', '尾', '末尾'])
            if s != None:
                return '月末', len(s)
            return '', 0

        l, l2 = limit_a()
        if len(l) > 0:
            if not self.m.touch:
                self.maybe_this_month(0)
            word = word[l2:]
            self.d.Limit(l)
        elif self.m.touch:
            l, l2 = limit_b()
            if len(l) > 0:
                word = word[l2:]
                self.d.Limit(l)

        return word
Esempio n. 7
0
    def start_with_year(self, word):
        if len(word) == 0:
            return word

        f = word.find('年')
        if f > 0:
            g = cn2dig.Parse(word[:f])
            if g >= 0:
                self.Y.Is(g, word[:f+1])
                word = word[f+1:]
        if not self.Y.touch:
            Y, ot = start_with(word, wyear)
            if Y != None:
                self.maybe_this_year(ot, Y)
                word = word[len(Y):]

        if len(word) == 0:
            return word

        def limit_a():
            s = start_in(word, ['上半年', '前半年'])
            if s != None:
                return '上半年', len(s)
            s = start_in(word, ['下半年', '后半年'])
            if s != None:
                return '下半年', len(s)
            s = start_in(word, ['年初',  '年头'])
            if s != None:
                return '年初', len(s)
            s = start_in(word, ['年中'])
            if s != None:
                return '年中', len(s)
            s = start_in(word, ['年末', '年尾', '年终'])
            if s != None:
                return '年末', len(s)
            return '', 0

        def limit_b():
            s = start_in(word, ['初'])
            if s != None:
                return '年初', len(s)
            s = start_in(word, ['中'])
            if s != None:
                return '年中', len(s)
            s = start_in(word, ['终', '末'])
            if s != None:
                return '年末', len(s)
            return '', 0

        l, l2 = limit_a()
        if len(l) > 0:
            if not self.Y.touch:
                self.maybe_this_year(0)
            word = word[l2:]
            self.m.Limit(l)
        elif self.Y.touch:
            l, l2 = limit_b()
            if len(l) > 0:
                word = word[l2:]
                self.m.Limit(l)

        return word
Esempio n. 8
0
    def relative_time(self, actor, word):
        个 = '' if self.reftype == self.REF_UNSPECIFIED else '?'
        f = re.findall(r'^(.+)年(半)?$', word)
        if len(f) > 0:
            if f[0][0] == '半' and f[0][1] == '':
                self.maybe_this_month(6 * actor, word)
                return ''
            x = cn2dig.Parse(f[0][0])
            if x >= 0:
                x = 12 * x
                if len(f[0][1]) > 0:
                    x += 6
                self.maybe_this_month(x * actor, word)
                return ''

        f = re.findall(r'^(.+)个{}(半)?月$'.format(个), word)
        if len(f) > 0:
            if f[0][0] == '半' and f[0][1] == '':
                self.maybe_this_day(15 * actor, word)
                return ''
            x = cn2dig.Parse(f[0][0])
            if x >= 0:
                x = 30 * x
                if len(f[0][1]) > 0:
                    x += 15
                self.maybe_this_day(x * actor, word)
                return ''
        if word == '半月':
            self.maybe_this_day(15 * actor, word)
            return ''

        f = re.findall(r'^(.+)个(半)?(星期|礼拜)(半)?$', word)
        if len(f) > 0:
            if f[0][0] == '半' and f[0][1] == '' and f[0][3] == '':
                self.maybe_this_day(3 * actor, word)
                return ''
            x = cn2dig.Parse(f[0][0])
            if x >= 0:
                x = 7 * x
                if len(f[0][1]) + len(f[0][3]) > 0:
                    x += 3
                self.maybe_this_day(x * actor, word)
                return ''

        f = re.findall(r'^(.+)(周|星期|礼拜)(半)?$', word)
        if len(f) > 0:
            if f[0][0] == '半' and f[0][2] == '':
                self.maybe_this_day(3 * actor, word)
                return ''
            x = cn2dig.Parse(f[0][0])
            if x >= 0:
                x = 7 * x
                if len(f[0][2]) > 0:
                    x += 3
                self.maybe_this_day(x * actor, word)
                return ''

        f = re.findall(r'^(.+)(天|日)(半)?$', word)
        if len(f) > 0:
            if f[0][0] == '半' and f[0][2] == '':
                if actor > 0:
                    if self.H.maybe > 15:
                        self.maybe_this_day(1)
                        self.H.Is(14, word)
                    else:
                        self.maybe_this_day(0)
                        self.H.Is(19, word)
                else:
                    if self.H.maybe > 15:
                        self.maybe_this_day(0)
                        self.H.Is(10, word)
                    else:
                        self.maybe_this_day(-1)
                        self.H.Is(14, word)
                return ''
            x = cn2dig.Parse(f[0][0])
            if x >= 0:
                if len(f[0][2]) > 0:
                    x += 1
                self.maybe_this_day(x * actor, word)
                return ''

        f = re.findall(r'^(.+)个(半)?(小时|钟头)(半)?$', word)
        if not f:
            f = re.findall(r'^(.+)(小时|钟头)(半)?$', word)
        if f:
            h = f[0][0]
            if len(f[0]) == 4:
                m = len(f[0][1]) + len(f[0][3])
            else:
                m = len(f[0][2])
            if h == '半' and m == 0:
                self.maybe_this_minute(30 * actor, word)
                return ''
            try:
                x = int(float(h) * 60)
            except:
                x = cn2dig.Parse(h) * 60
            if x >= 0:
                if m > 0:
                    x += 30
                self.maybe_this_minute(x * actor, word)
                return ''

        f = re.findall(r'^(.+)(分钟|分)$', word)
        if len(f) > 0:
            x = cn2dig.Parse(f[0][0])
            if x >= 0:
                self.maybe_this_minute(x * actor, word)
                return ''

        f = re.findall(r'^(.+)小时(.+)分钟$', word)
        if len(f) > 0:
            h = f[0][0]
            m = f[0][1]
            if h[-1] == '个':
                h = h[:-1]
            h = cn2dig.Parse(h) * 60
            m = cn2dig.Parse(m)
            if h >= 0 and m >= 0:
                self.maybe_this_minute((h+m) * actor, word)
                return ''

        return word
Esempio n. 9
0
    def period(self, word):
        if re.findall(r'^({})([一这](段时间|阵子))?({})?$'.format(self.rew_最近, self.rew_以来), word):
            return '前30天'
        if re.findall(r'^([这近](段时间|阵子))({})?$'.format(self.rew_以来), word):
            return '前30天'
        if word in ['近期']:
            return '前30天'
        if word in ['当前', '现在']:
            return '今天'
        if word in ['过去', '以前', '之前', '曾经']:
            return '0小时前'
        if word in ['未来', '将来', '今后', '以后', '之后']:
            return '0小时后'

        f = self.rec_分.findall(word)
        if f:
            x = f[0][1]
            if x == '几':
                return '前3分钟'
            if cn2dig.Parse(x) >= 0:
                return '前{}分钟'.format(x)

        f = self.rec_时.findall(word)
        if f:
            x = f[0][1]
            if x == '几':
                return '前3个小时'
            if cn2dig.Parse(x) >= 0:
                return '前{}个小时'.format(x)

        f = self.rec_日.findall(word)
        if f:
            x = f[0][1]
            if x == '几':
                return '前7天'
            if cn2dig.Parse(x) >= 0:
                return '前{}天'.format(x)

        f = self.rec_周.findall(word)
        if f:
            x = f[0][1]
            if x == '几':
                return '前3周'
            if cn2dig.Parse(x) >= 0:
                return '前{}周'.format(x)

        f = self.rec_月.findall(word)
        if f:
            x = f[0][1]
            if x == '几':
                return '前3个月'
            if cn2dig.Parse(x) >= 0:
                return '前{}个月'.format(x)

        f = self.rec_年.findall(word)
        if f:
            x = f[0][1]
            if x == '几':
                return '前3年'
            if cn2dig.Parse(x) >= 0:
                return '前{}年'.format(x)

        return word