def test_15(self): text = "?시세 사탕무 : 말세80ㅎ; 사사리75ㅎ; 시라130ㅅ;" config = {HenriqueEntity.Config.Field.LOCALE: "ko-KR"} entity_list = HenriqueEntity.text_extractors2entity_list( text, PriceSkillClique.config2extractors(config), ) hyp = PriceSkillClique.text_entity_list2clique_list(text, entity_list) ref = [{ 'ports': ['Marseilles'], 'rate': 80, 'tradegoods': ['Sugar Beet'], 'trend': 'down' }, { 'ports': ['Sassari'], 'rate': 75, 'tradegoods': ['Sugar Beet'], 'trend': 'down' }, { 'ports': ['Syracuse'], 'rate': 130, 'tradegoods': ['Sugar Beet'], 'trend': 'rise' }] # pprint(hyp) self.assertEqual(hyp, ref)
def packet2response(cls, packet): chatroom = Chatroom.codename2chatroom(KhalaPacket.packet2chatroom(packet)) locale = Chatroom.chatroom2locale(chatroom) lang = LocaleTool.locale2lang(locale) entity_classes = cls.target_entity_classes() text_in = KhalaPacket.packet2text(packet) config = {HenriqueEntity.Config.Field.LOCALE: locale} entity_list = HenriqueEntity.text_classes2entity_list(text_in, entity_classes, config=config) blocks = [cls.entity_lang2response_block(entity, lang) for entity in entity_list] return Rowsblock.blocks2text(blocks)
def test_02(self): text = "조선 서적" config = {HenriqueEntity.Config.Field.LOCALE: "ko-KR"} extractors = [ partial(CultureEntity.text2entity_list, config=config), partial(TradegoodEntity.text2entity_list, config=config), ] hyp = HenriqueEntity.text_extractors2entity_list(text, extractors) ref = [{ 'span': (0, 5), 'text': '조선 서적', 'type': 'henrique.main.document.tradegood.tradegood_entity.TradegoodEntity', 'value': 'Korean Books' }] # pprint(hyp) self.assertEqual(hyp, ref)
def test_01(self): text = "?시세 초롱 : 말세80ㅎ; 사사리75ㅎ; 시라130ㅅ;" config = {HenriqueEntity.Config.Field.LOCALE: "ko-KR"} hyp = HenriqueEntity.text_extractors2entity_list( text, PriceSkillClique.config2extractors(config)) ref = [{ 'span': (4, 6), 'text': '초롱', 'type': 'henrique.main.document.tradegood.tradegood_entity.TradegoodEntity', 'value': 'Paper Lantern' }, { 'span': (9, 11), 'text': '말세', 'type': 'henrique.main.document.port.port_entity.PortEntity', 'value': 'Marseilles' }, { 'span': (11, 13), 'text': '80', 'type': 'henrique.main.document.price.rate.rate_entity.RateEntity', 'value': 80 }, { 'span': (13, 14), 'text': 'ㅎ', 'type': 'henrique.main.document.price.trend.trend_entity.TrendEntity', 'value': 'down' }, { 'span': (16, 19), 'text': '사사리', 'type': 'henrique.main.document.port.port_entity.PortEntity', 'value': 'Sassari' }, { 'span': (19, 21), 'text': '75', 'type': 'henrique.main.document.price.rate.rate_entity.RateEntity', 'value': 75 }, { 'span': (21, 22), 'text': 'ㅎ', 'type': 'henrique.main.document.price.trend.trend_entity.TrendEntity', 'value': 'down' }, { 'span': (24, 26), 'text': '시라', 'type': 'henrique.main.document.port.port_entity.PortEntity', 'value': 'Syracuse' }, { 'span': (26, 29), 'text': '130', 'type': 'henrique.main.document.price.rate.rate_entity.RateEntity', 'value': 130 }, { 'span': (29, 30), 'text': 'ㅅ', 'type': 'henrique.main.document.price.trend.trend_entity.TrendEntity', 'value': 'rise' }] # pprint(hyp) self.assertEqual(hyp, ref)
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)
def packet2rowsblocks(cls, packet): from henrique.main.skill.price.price_skill_clique import PriceSkillClique as Clique Param = PriceSkillParameter text = KhalaPacket.packet2text(packet) server_codename = HenriquePacket.packet2server(packet) chatroom = Chatroom.codename2chatroom( KhalaPacket.packet2chatroom(packet)) config = { HenriqueEntity.Config.Field.LOCALE: Chatroom.chatroom2locale(chatroom) } extractors = Clique.config2extractors(config) entity_list = HenriqueEntity.text_extractors2entity_list( text, extractors) clique_list = Clique.text_entity_list2clique_list(text, entity_list) clique_list_update = lfilter( lambda x: Clique.clique2type(x) == Clique.Type.UPDATE, clique_list) # raise Exception({"entity_list": entity_list, # "clique_list": clique_list, # "clique_list_update":clique_list_update, # }) if clique_list_update: Clique.clique_list2update_mongodb(packet, clique_list_update) h_port2indexes = Clique.cliques2dict_port2indexes(clique_list) h_tradegood2indexes = Clique.cliques2dict_tradegood2indexes( clique_list) def _groupby_paramter_type(): entity_list_portlike = lfilter( lambda x: FoxylibEntity.entity2type(x) in Portlike. entity_types(), entity_list) entity_list_tradegood = lfilter( lambda x: FoxylibEntity.entity2type(x) == TradegoodEntity. entity_type(), entity_list) if not entity_list_portlike: return Param.Type.TRADEGOOD if not entity_list_tradegood: return Param.Type.PORTLIKE if len(h_port2indexes) > 1: return Param.Type.TRADEGOOD if len(h_tradegood2indexes) > 1: return Param.Type.PORTLIKE span_portlike = FoxylibEntity.entity2span(entity_list_portlike[0]) span_tradegood = FoxylibEntity.entity2span( entity_list_tradegood[0]) if span_portlike[0] < span_tradegood[0]: return Param.Type.PORTLIKE else: return Param.Type.TRADEGOOD groupby_parameter_type = _groupby_paramter_type() port_tradegood_list = lchain( *map(Clique.clique2port_tradegood_iter, clique_list)) price_dict = MarketpriceDict.port_tradegood_iter2price_dict( server_codename, port_tradegood_list) # raise Exception({"port_tradegood_list": port_tradegood_list, # "price_dict": price_dict, # "groupby_parameter_type": groupby_parameter_type, # }) chatroom = Chatroom.codename2chatroom( KhalaPacket.packet2chatroom(packet)) lang = LocaleTool.locale2lang(Chatroom.chatroom2locale(chatroom)) block_list = cls.port_tradegood_lists2blocks(port_tradegood_list, price_dict, lang, groupby_parameter_type) return block_list