Example #1
0
            def numericalPrefix(dayMatch):
                # Grab 'three' of 'three weeks from'
                prefix = input.split(dayMatch.group(1))[0].strip().split(' ')
                prefix.reverse()
                prefix = filter(lambda s: s != 'and', prefix)

                # Generate best guess number
                service = NumberService()
                num = prefix[0]
                if service.isValid(num):
                    for n in prefix[1:]:
                        inc = n + " " + num
                        if service.isValid(inc):
                            num = inc
                        else:
                            break
                    return service.parse(num)
                return 1
Example #2
0
            def convert(n):
                if n in self.__constants__:
                    return self.__constants__[n]

                converter = NumberService()
                return converter.parse(n)
Example #3
0
        def handleMatch(time):
            relative = False

            if not time:
                return None

            # Default times: 10am, 3pm, 8pm
            elif time.group(1) == 'morning':
                h = 10
                m = 0
            elif time.group(1) == 'afternoon':
                h = 15
                m = 0
            elif time.group(1) == 'evening' or time.group(1) == 'tonight' or time.group(1) == 'eve':
                h = 20
                m = 0
            elif time.group(4) and time.group(5):
                # match relative times, e.g. in 5 hours
                h, m = 0, 0

                # Extract hours difference
                converter = NumberService()
                try:
                    diff = converter.parse(time.group(4))
                except:
                    return None

                if time.group(5) == 'hours':
                    h += diff
                else:
                    m += diff

                # Extract minutes difference
                if time.group(6):
                    converter = NumberService()
                    try:
                        diff = converter.parse(time.group(7))
                    except:
                        return None

                    if time.group(8) == 'hours':
                        h += diff
                    else:
                        m += diff

                relative = True
            elif time.group(9) and time.group(10):
                # match hour only time, e.g. 4 pm
                h = int(time.group(9))
                m = 0

                if time.group(10) == 'pm':
                    h += 12

            elif time.group(11) and time.group(12):
                # match phrases like "at 3?"
                h = int(time.group(12))
                m = 0

            else:
                # Convert from "HH:MM pm" format
                t = time.group(2)

                if ':' in t:
                    h, m = int(t.split(':')[0]), int(t.split(':')[1])
                else:
                    h, m = int(t.split('.')[0]), int(t.split('.')[1])

                try:
                    if time.group(3) == 'pm':
                        h = (h % 12) + 12
                except IndexError:
                    pass

            if relative:
                return self.now + datetime.timedelta(hours=h, minutes=m)

            # here we assume that time before 10 is meant to be pm
            # unless time includes 'am' or is in format 08:00 (starts with 0)
            elif h < 10 and time.group(3) != 'am' and time.group(1)[0] != '0':
                h = (h % 12) + 12
                return "%02d:%02d" % (h, m)
            else:
                return "%02d:%02d" % (h, m)
Example #4
0
        def handleMatch(time):
            relative = False

            if not time:
                return None

            # Default times: 8am, 12pm, 7pm
            elif time.group(1) == 'morning':
                h = 8
                m = 0
            elif time.group(1) == 'afternoon':
                h = 12
                m = 0
            elif time.group(1) == 'evening':
                h = 19
                m = 0
            elif time.group(4) and time.group(5):
                h, m = 0, 0

                # Extract hours difference
                converter = NumberService()
                try:
                    diff = converter.parse(time.group(4))
                except:
                    return None

                if time.group(5) == 'hours':
                    h += diff
                else:
                    m += diff

                # Extract minutes difference
                if time.group(6):
                    converter = NumberService()
                    try:
                        diff = converter.parse(time.group(7))
                    except:
                        return None

                    if time.group(8) == 'hours':
                        h += diff
                    else:
                        m += diff

                relative = True
            else:
                # Convert from "HH:MM pm" format
                t = time.group(2)
                h, m = int(t.split(':')[0]) % 12, int(t.split(':')[1])

                try:
                    if time.group(3) == 'pm':
                        h += 12
                except IndexError:
                    pass

            if relative:
                return self.now + datetime.timedelta(hours=h, minutes=m)
            else:
                return datetime.datetime(
                    self.now.year, self.now.month, self.now.day, h, m
                )
Example #5
0
        def handleMatch(time):
            relative = False

            if not time:
                return None

            # Default times: 8am, 12pm, 7pm
            elif time.group(1) == 'morning':
                h = 8
                m = 0
            elif time.group(1) == 'afternoon':
                h = 12
                m = 0
            elif time.group(1) == 'evening':
                h = 19
                m = 0
            elif time.group(4) and time.group(5):
                # match relative times, e.g. in 5 hours
                h, m = 0, 0

                # Extract hours difference
                converter = NumberService()
                try:
                    diff = converter.parse(time.group(4))
                except:
                    return None

                if time.group(5) == 'hours':
                    h += diff
                else:
                    m += diff

                # Extract minutes difference
                if time.group(6):
                    converter = NumberService()
                    try:
                        diff = converter.parse(time.group(7))
                    except:
                        return None

                    if time.group(8) == 'hours':
                        h += diff
                    else:
                        m += diff

                relative = True
            elif time.group(9) and time.group(10):
                # match hour only time, e.g. 4 pm
                h = int(time.group(9))
                m = 0

                if time.group(10) == 'pm':
                    h += 12

            elif time.group(11) and time.group(12):
                # match phrases like "at 3?"
                h = int(time.group(12))
                m = 0

            else:
                # Convert from "HH:MM pm" format
                t = time.group(2)
                h, m = int(t.split(':')[0]), int(t.split(':')[1])

                try:
                    if time.group(3) == 'pm':
                        h = (h % 12) + 12
                except IndexError:
                    pass

            if relative:
                return self.now + datetime.timedelta(hours=h, minutes=m)
            else:
                return datetime.datetime(
                    self.now.year, self.now.month, self.now.day, h, m
                )