예제 #1
0
    def test_01(self):
        config = {HenriqueEntity.Config.Field.LOCALE: "ko"}
        entity_list = RelativeTimedeltaEntity.text2entity_list("+3일",
                                                               config=config)
        ref = [{
            'span': (0, 3),
            'text': '+3일',
            'type':
            'henrique.main.tool.entity.datetime.timedelta.timedelta_entity.RelativeTimedeltaEntity',
            'value': {
                'sign': '+',
                'timedelta': {
                    'span': (1, 3),
                    'text': '3일',
                    'type':
                    'henrique.main.tool.entity.datetime.timedelta.timedelta_entity.TimedeltaEntity',
                    'value': [{
                        'quantity': 3,
                        'span': (1, 3),
                        'unit': 'day'
                    }]
                }
            }
        }]

        # pprint(hyp)
        self.assertEqual(entity_list, ref)
        self.assertEqual(
            RelativeTimedeltaEntity.entity2relativedelta(
                l_singleton2obj(entity_list)),
            relativedelta.relativedelta(days=3),
        )
예제 #2
0
    def test_04(self):
        config = {HenriqueEntity.Config.Field.LOCALE: "ko"}
        entity_list = RelativeTimedeltaEntity.text2entity_list("-10 mins",
                                                               config=config)
        # pprint(entity_list)

        hyp = RelativeTimedeltaEntity.entity2relativedelta(
            l_singleton2obj(entity_list))
        ref = -1 * relativedelta.relativedelta(minutes=10)
        self.assertEqual(hyp, ref)
예제 #3
0
    def test_02(self):
        config = {HenriqueEntity.Config.Field.LOCALE: "ko"}
        entity_list = RelativeTimedeltaEntity.text2entity_list("+20 초",
                                                               config=config)
        # pprint(entity_list)

        hyp = RelativeTimedeltaEntity.entity2relativedelta(
            l_singleton2obj(entity_list))
        ref = relativedelta.relativedelta(seconds=20)
        self.assertEqual(hyp, ref)
예제 #4
0
    def test_07(self):
        config = {HenriqueEntity.Config.Field.LOCALE: "ko"}
        entity_list = RelativeTimedeltaEntity.text2entity_list("+2분 1초",
                                                               config=config)
        # pprint(entity_list)

        ref = [{
            'span': (0, 6),
            'text': '+2분 1초',
            'type':
            'henrique.main.tool.entity.datetime.timedelta.timedelta_entity.RelativeTimedeltaEntity',
            'value': {
                'sign': '+',
                'timedelta': {
                    'span': (1, 6),
                    'text':
                    '2분 1초',
                    'type':
                    'henrique.main.tool.entity.datetime.timedelta.timedelta_entity.TimedeltaEntity',
                    'value': [{
                        'quantity': 2,
                        'span': (1, 3),
                        'unit': 'minute'
                    }, {
                        'quantity': 1,
                        'span': (4, 6),
                        'unit': 'second'
                    }]
                }
            }
        }]
        self.assertEqual(entity_list, ref)
예제 #5
0
    def test_03(self):
        config = {HenriqueEntity.Config.Field.LOCALE: "ko"}
        entity_list = RelativeTimedeltaEntity.text2entity_list("-1개월 6일",
                                                               config=config)
        # pprint(entity_list)

        ref = [{
            'span': (0, 7),
            'text': '-1개월 6일',
            'type':
            'henrique.main.tool.entity.datetime.timedelta.timedelta_entity.RelativeTimedeltaEntity',
            'value': {
                'sign': '-',
                'timedelta': {
                    'span': (1, 7),
                    'text':
                    '1개월 6일',
                    'type':
                    'henrique.main.tool.entity.datetime.timedelta.timedelta_entity.TimedeltaEntity',
                    'value': [{
                        'quantity': 1,
                        'span': (1, 4),
                        'unit': 'month'
                    }, {
                        'quantity': 6,
                        'span': (5, 7),
                        'unit': 'day'
                    }]
                }
            }
        }]
        self.assertEqual(entity_list, ref)

        reldelta = RelativeTimedeltaEntity.entity2relativedelta(
            l_singleton2obj(entity_list))
        reldelta_ref = -1 * relativedelta.relativedelta(months=1, days=6)
        self.assertEqual(reldelta, reldelta_ref)
예제 #6
0
    def packet2response(cls, packet):
        logger = HenriqueLogger.func_level2logger(cls.packet2response,
                                                  logging.DEBUG)
        logger.debug({"packet": packet})

        server_codename = HenriquePacket.packet2server(packet)
        chatroom = Chatroom.codename2chatroom(
            KhalaPacket.packet2chatroom(packet))
        locale = Chatroom.chatroom2locale(chatroom) or HenriqueLocale.DEFAULT
        lang = LocaleTool.locale2lang(locale)
        tz = pytz.timezone(HenriqueLocale.lang2tzdb(lang))
        dt_now = datetime.now(tz)

        text_in = KhalaPacket.packet2text(packet)
        config = {HenriqueEntity.Config.Field.LOCALE: locale}
        # entity_list = RelativeTimedeltaEntity.text2entity_list(text_in, config=config)

        entity_list = HenriqueEntity.text_extractors2entity_list(
            text_in,
            cls.config2extractors(config),
        )
        logger.debug({
            "len(entity_list)": len(entity_list),
            "entity_list": entity_list,
        })

        if not entity_list:
            return cls.server_lang2lookup(server_codename, lang)

        if len(entity_list) != 1:
            return  # Invalid request

        entity = l_singleton2obj(entity_list)
        if FoxylibEntity.entity2type(
                entity) == RelativeTimedeltaEntity.entity_type():
            reldelta = RelativeTimedeltaEntity.entity2relativedelta(entity)
            dt_in = cls.relativedelta2nanban_datetime(
                server_codename,
                reldelta,
            )

            # raise Exception({"dt_in":dt_in, "reldelta":reldelta})

            if dt_in is None:
                msg_error = NanbanSkillError.codename_lang2text(
                    NanbanSkillError.Codename.NO_PREV_NANBAN_ERROR, lang)
                raise HenriqueCommandError(msg_error)

            logger.debug({
                "reldelta": reldelta,
            })

        elif FoxylibEntity.entity2type(entity) == TimeEntity.entity_type():
            time_in = TimeEntity.value2datetime_time(
                FoxylibEntity.entity2value(entity))
            dt_in = PytzTool.localize(datetime.combine(dt_now.date(), time_in),
                                      tz)
            logger.debug({
                "time_in": time_in,
                "dt_in": dt_in,
            })
        else:
            raise RuntimeError({
                "Invalid entity type: {}".format(
                    FoxylibEntity.entity2type(entity))
            })

        dt_nearest = DatetimeTool.datetime2nearest(dt_in, dt_now,
                                                   NanbanTimedelta.period(),
                                                   Nearest.COMING)

        logger.debug({
            "text_in": text_in,
            "dt_now": dt_now,
            "dt_in": dt_in,
            "dt_nearest": dt_nearest,
        })

        cls.nanban_datetime2upsert_mongo(packet, dt_nearest)
        return cls.server_lang2lookup(server_codename, lang)
예제 #7
0
 def test_06(self):
     config = {HenriqueEntity.Config.Field.LOCALE: "en"}
     entity_list = RelativeTimedeltaEntity.text2entity_list("-10분",
                                                            config=config)
     self.assertFalse(entity_list, )