def text2entity_list(cls, text_in, config=None): locale = HenriqueEntity.Config.config2locale( config) or HenriqueLocale.DEFAULT lang = LocaleTool.locale2lang(locale) or LocaleTool.locale2lang( HenriqueLocale.DEFAULT) return cls._text2entity_list(text_in, lang)
def test_02(self): with LocaleTool.override("en_US"): """ why translate here? """ self.assertEqual(MyLocale.gettext_ko("hello"), "hello") with LocaleTool.override("ko_KR"): self.assertEqual(MyLocale.gettext_ko("goodbye"), "안녕")
def test_01(self): logger = FoxylibLogger.func_level2logger(self.test_01, logging.DEBUG) logger.debug({"locale.locale_alias.items()":locale.locale_alias.items()}) with LocaleTool.override("en_US.UTF-8", category=locale.LC_ALL): self.assertEqual(MyLocale.gettext("hello"), "hello") with LocaleTool.override("ko_KR.UTF-8", category=locale.LC_ALL): # success # with LocaleTool.override("ko", category=locale.LC_ALL): # fails # with LocaleTool.override("korean", category=locale.LC_ALL): # fails # with LocaleTool.override("ko_KR.eucKR", category=locale.LC_ALL): # fails self.assertEqual(MyLocale.gettext("goodbye"), "안녕")
def text2entity_list(cls, text_in, config=None): locale = HenriqueEntity.Config.config2locale(config) or HenriqueLocale.DEFAULT lang = LocaleTool.locale2lang(locale) or LocaleTool.locale2lang(HenriqueLocale.DEFAULT) matcher = cls.lang2matcher(lang) span_value_list = list(matcher.text2span_value_iter(text_in)) entity_list = [{FoxylibEntity.Field.SPAN: span, FoxylibEntity.Field.TEXT: StringTool.str_span2substr(text_in, span), FoxylibEntity.Field.VALUE: value, FoxylibEntity.Field.TYPE: cls.entity_type(), } for span, value in span_value_list] return entity_list
def packet2response(cls, packet): chatroom = Chatroom.codename2chatroom( KhalaPacket.packet2chatroom(packet)) locale = Chatroom.chatroom2locale(chatroom) lang = LocaleTool.locale2lang(locale) text_in = KhalaPacket.packet2text(packet) config = {HenriqueEntity.Config.Field.LOCALE: locale} def entity2is_valid(entity): if FoxylibEntity.entity2value( entity) != HenriqueSkill.Codename.HELP: return True span = FoxylibEntity.entity2span(entity) if len(str2strip(text_in[:span[0]])) > 1: return True entity_list_skill = lfilter( entity2is_valid, SkillEntity.text2entity_list(text_in, config=config)) def entity_list2codename_list(entity_list): codename_list = luniq( map(SkillEntity.entity2skill_codename, entity_list)) if codename_list: return codename_list return [HenriqueSkill.Codename.HELP] codename_list = entity_list2codename_list(entity_list_skill) clazz_list = lmap(HenriqueSkill.codename2class, codename_list) blocks = [clazz.lang2description(lang) for clazz in clazz_list] return Rowsblock.blocks2text(blocks)
def gettext(cls, *_, **__): loc = locale.getlocale() lang = LocaleTool.locale2lang(loc) t = cls.lang2translation(lang) # t = cls.translation() return t.gettext(*_, **__)
def entity2response_block(cls, entity, packet, ): logger = HenriqueLogger.func_level2logger(cls.packet2response, logging.DEBUG) chatroom = Chatroom.codename2chatroom(KhalaPacket.packet2chatroom(packet)) if Chatroom.chatroom2codename(chatroom) != ChatroomKakaotalk.codename(): return locale = Chatroom.chatroom2locale(chatroom) lang = LocaleTool.locale2lang(locale) v = FoxylibEntity.entity2value(entity) codename = ChatroomuserEntity.value_packet2codename(v, packet) logger.debug({"codename": codename, "entity": entity, "v": v, }) chatroomuser = Chatroomuser.codename2chatroomuser(codename) comments = Chatroomuser.chatroomuser2comments(chatroomuser) comment = choice(comments) if comments else None filepath = os.path.join(FILE_DIR, "tmplt.{}.part.txt".format(lang)) data = {"name": codename, "comment": comment, "str_aliases": ", ".join(Chatroomuser.chatroomuser2aliases(chatroomuser)), } text_out = str2strip(HenriqueJinja2.textfile2text(filepath, data)) return text_out
def port_entity2response( cls, port_entity, j_packet, ): query = EntityTool.F.entity2text(port_entity) j_port = PortEntity.query2j_doc(query) #j_culture = PortDocument.F.j_port2j_culture(j_port) locale = KhalaPacket.j_packet2locale(j_packet) lang = LocaleTool.locale2lang(locale) filepath = os.path.join(FILE_DIR, "port.port.tmplt.txt") j_data = { "port_collection_name": PortCollection.lang2name(lang), "port_name": PortDocument.j_port_lang2name(j_port, lang), "culture_collection_name": "n/a", #CultureCollection.lang2name(lang), "culture_name": "n/a", #CultureDocument.F.j_culture_lang2name(j_culture, lang), "port_status_collection_name": "n/a", #PortStatusCollection.lang2name(lang), "port_status": "n/a", #None, } str_out = Jinja2Toolkit.tmplt_file2str(filepath, j_data) return str_out
def date_locale2str(cls, d, locale): from foxylib.tools.locale.locale_tool import LocaleTool if LocaleTool.locale2lang(locale) == "ko": l = ["월요일","화요일","수요일","목요일","금요일","토요일","일요일"] return l[d.weekday()] l = ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"] return l[d.weekday()]
def text2entity_list(cls, str_in, config=None): locale = cls.Config.config2locale(config) lang = LocaleTool.locale2lang(locale) if lang == "ko": from foxylib.tools.entity.calendar.dayofweek.locale.ko.dayofweek_entity_ko import DayofweekEntityKo return DayofweekEntityKo.text2entity_list(str_in) raise NotImplementedError("Invalid lang: {}".format(lang))
def text2entity_list(cls, text_in, config=None): locale = HenriqueEntity.Config.config2locale( config) or HenriqueLocale.DEFAULT lang = LocaleTool.locale2lang(locale) or LocaleTool.locale2lang( HenriqueLocale.DEFAULT) pattern = cls.lang2pattern(lang) m_list = list(pattern.finditer(text_in)) def match2entity(m): span = m.span() entity = { FoxylibEntity.Field.SPAN: span, FoxylibEntity.Field.TEXT: StringTool.str_span2substr(text_in, span), FoxylibEntity.Field.VALUE: cls.match2value(m), FoxylibEntity.Field.TYPE: cls.entity_type(), } return entity entity_list = lmap(match2entity, m_list) return entity_list
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 tradegood_entity2response( cls, tg_entity, j_packet, ): query = EntityTool.F.entity2text(tg_entity) j_tradegood = TradegoodEntity.query2j_doc(query) #j_culture = TradegoodDocument.F.j_tradegood2j_culture(j_tradegood) locale = KhalaPacket.j_packet2locale(j_packet) lang = LocaleTool.locale2lang(locale) filepath = os.path.join(FILE_DIR, "tradegood.tradegood.tmplt.txt") j_data = { "tradegood_collection_name": TradegoodCollection.lang2name(lang), "tradegood_name": TradegoodDocument.j_tradegood_lang2name(j_tradegood, lang), } str_out = Jinja2Toolkit.tmplt_file2str(filepath, j_data) return str_out
def entity2response_block( cls, packet, entity, ): chatroom = Chatroom.codename2chatroom( KhalaPacket.packet2chatroom(packet)) locale = Chatroom.chatroom2locale(chatroom) lang = LocaleTool.locale2lang(locale) entity_type = FoxylibEntity.entity2type(entity) h_type2func = { PortEntity.entity_type(): partial(PortSkill.entity_lang2response_block, lang=lang), TradegoodEntity.entity_type(): partial(TradegoodSkill.entity_lang2response_block, lang=lang), CultureEntity.entity_type(): partial(CultureSkill.entity_lang2response_block, lang=lang), ChatroomuserEntity.entity_type(): partial( WhoSkill.entity2response_block, packet=packet, ), } assert_equals( set(h_type2func.keys()), smap(lambda c: c.entity_type(), cls.target_entity_classes())) entity2response = h_type2func.get(entity_type) if not entity2response: raise NotImplementedError( "Invalid entity_type: {}".format(entity_type)) return entity2response(entity)
def text2entity_list(cls, text_in, config=None): locale = HenriqueEntity.Config.config2locale( config) or HenriqueLocale.DEFAULT lang = LocaleTool.locale2lang(locale) pattern = cls.lang2pattern(lang) m_list = list(pattern.finditer(text_in)) def match2entity(match): span = match.span() text = StringTool.str_span2substr(text_in, span) codename = cls.lang_alias2codename(lang, text) entity = { FoxylibEntity.Field.VALUE: codename, FoxylibEntity.Field.TEXT: text, FoxylibEntity.Field.SPAN: span, FoxylibEntity.Field.TYPE: cls.entity_type(), } return entity entity_list = lmap(match2entity, m_list) return entity_list
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
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 text2entity_list(cls, text_in, config=None): locale = HenriqueEntity.Config.config2locale( config) or HenriqueLocale.DEFAULT lang = LocaleTool.locale2lang(locale) langs_recognizable = HenriqueLocale.lang2langs_recognizable(lang) if "ko" not in langs_recognizable: return [] match_list = list(cls.pattern_ko().finditer(text_in)) def match2entity_list(match): span = match.span() assert_in(SpanTool.span2len(span), (2, 3)) entity_list = [] s, e = span span_nutmeg = (s, s + 1) entity_nutmeg = { FoxylibEntity.Field.SPAN: span_nutmeg, FoxylibEntity.Field.TEXT: StringTool.str_span2substr(text_in, span_nutmeg), FoxylibEntity.Field.VALUE: "Nutmeg", FoxylibEntity.Field.TYPE: TradegoodEntity.entity_type(), } entity_list.append(entity_nutmeg) span_mace = (s + 1, s + 2) entity_mace = { FoxylibEntity.Field.SPAN: span_mace, FoxylibEntity.Field.TEXT: StringTool.str_span2substr(text_in, span_mace), FoxylibEntity.Field.VALUE: "Mace", FoxylibEntity.Field.TYPE: TradegoodEntity.entity_type(), } entity_list.append(entity_mace) if SpanTool.span2len(span) == 3: span_clove = (s + 2, s + 3) entity_cloves = { FoxylibEntity.Field.SPAN: span_clove, FoxylibEntity.Field.TEXT: StringTool.str_span2substr(text_in, span_clove), FoxylibEntity.Field.VALUE: "Cloves", FoxylibEntity.Field.TYPE: TradegoodEntity.entity_type(), } entity_list.append(entity_cloves) return entity_list entity_list = [ entity for m in match_list for entity in match2entity_list(m) ] return entity_list