Ejemplo n.º 1
0
        def product2data(product):
            tg_codename = Product.product2tradegood(product)
            tg = Tradegood.codename2tradegood(tg_codename)

            h = {
                "name": Tradegood.tradegood_lang2name(tg, lang),
            }
            return h
Ejemplo n.º 2
0
    def codename_lang2json(cls, tradegood_codename, lang):
        ports = Port.tradegood2ports(tradegood_codename)
        tradegood = Tradegood.codename2tradegood(tradegood_codename)

        data = {cls.Field.TRADEGOOD_NAME: Tradegood.tradegood_lang2name(tradegood, lang),
                cls.Field.PORT_NAMES: sorted([Port.port_lang2name(port, lang) for port in ports],)
                }
        return data
Ejemplo n.º 3
0
    def tradegood2text(cls, tradegood_codename, port_codename_list, price_dict,
                       lang):

        n = len(port_codename_list)
        tradegood = Tradegood.codename2tradegood(tradegood_codename)
        str_title = cls.tradegood_lang2title(tradegood, lang)

        def port2price(port_codename):
            price = MarketpriceDict.lookup(price_dict, port_codename,
                                           tradegood_codename)
            if price:
                return price

            price_fake = MarketpriceDoc.price_tradegood2doc_fake(
                port_codename, tradegood_codename)
            return price_fake

        price_list = lmap(port2price, port_codename_list)

        i_list_sorted = sorted(
            range(n), key=lambda i: MarketpriceDoc.key_default(price_list[i]))
        # price_list = sorted(price_list_raw, key=MarketpriceDoc.key_default)

        rows_body = [
            cls._price_lang2text(price_list[i], port_codename_list[i], lang)
            for i in i_list_sorted
        ]

        return Rowsblock.rows2text(chain(
            [str_title],
            rows_body,
        ))
Ejemplo n.º 4
0
 def tradegood_lang2title(cls, tradegood, lang):
     filepath = os.path.join(FILE_DIR, "tmplt.{}.part.txt".format(lang))
     data = {
         "tradegood_name": Tradegood.tradegood_lang2name(tradegood, lang),
     }
     title = str2strip(HenriqueJinja2.textfile2text(filepath, data))
     return title
Ejemplo n.º 5
0
    def codename_lang2text(cls, tradegood_codename, lang):
        tradegood = Tradegood.codename2tradegood(tradegood_codename)

        prefer_list = Prefer.tradegood2prefers(tradegood_codename)

        def prefer2culture_name(prefer):
            culture = Culture.codename2culture(Prefer.prefer2culture(prefer))
            culture_name = Culture.culture_lang2name(culture, lang)
            return culture_name

        preferred_culture_list = lmap(prefer2culture_name, prefer_list)
        preferred_cultures = ", ".join(preferred_culture_list)

        filepath = os.path.join(FILE_DIR, "tmplt.{}.part.txt".format(lang))
        data = {
            "tradegood_name": Tradegood.tradegood_lang2name(tradegood, lang),
            "preferred_cultures": preferred_cultures,
        }
        text_out = str2strip(HenriqueJinja2.textfile2text(filepath, data))

        return text_out
Ejemplo n.º 6
0
    def lang2matcher(cls, lang):
        tg_list = Tradegood.list_all()
        langs_recognizable = HenriqueLocale.lang2langs_recognizable(lang)

        def tg2aliases(tg):
            for _lang in langs_recognizable:
                yield from Tradegood.tradegood_lang2aliases(tg, _lang)

        h_value2aliases = merge_dicts(
            [{
                Tradegood.tradegood2codename(tg): list(tg2aliases(tg))
            } for tg in tg_list],
            vwrite=vwrite_no_duplicate_key)

        config = {
            GazetteerMatcher.Config.Key.NORMALIZER:
            cls.text2norm,
            GazetteerMatcher.Config.Key.TEXTS2PATTERN:
            HenriqueEntity.texts2pattern_port_tradegood,
        }
        matcher = GazetteerMatcher(h_value2aliases, config)
        return matcher
Ejemplo n.º 7
0
    def tuple2pair(cls, t):
        logger = HenriqueLogger.func_level2logger(cls.tuple2pair, logging.DEBUG)

        def chatuser_uuid2channel_user(chatuser_uuid, alias=None):
            if not chatuser_uuid:
                return None

            row = ChatuserTable.uuid2row(chatuser_uuid)
            if not row:
                logger.debug({"chatuser_uuid":chatuser_uuid})
                return None

            channel_user = ChatuserTable.row2channel_user(row, alias=alias)
            return channel_user

        j_postgres = t[PortTradegoodStateTable.index_json()]
        sender_name = j_postgres.get("sender_name")

        created_at = DatetimeTool.fromisoformat(j_postgres["created_at"])

        port_name_en = PortTradegoodStateTable.tuple2port_name_en(t)
        port = cls.name_en2port(port_name_en)

        tradegood_name_en = PortTradegoodStateTable.tuple2tradegood_name_en(t)
        tradegood = cls.name_en2tradegood(tradegood_name_en)

        rate = PortTradegoodStateTable.tuple2rate(t)
        trend = PortTradegoodStateTable.tuple2trend(t)

        chatuser_uuid = PortTradegoodStateTable.tuple2chatuser_uuid(t)
        channel_user = chatuser_uuid2channel_user(chatuser_uuid, alias=sender_name)
        channel_user_code = ChannelUser.channel_user2codename(channel_user) if channel_user else None

        server_alias = str2lower(jdown(j_postgres, ["server", "name"]))
        # logger.debug({"server_alias":server_alias})

        server = Server.alias_lang2server(server_alias, "ko")
        assert_true(server,server_alias)

        marketprice = {MarketpriceDoc.Field.CREATED_AT: created_at,
                       MarketpriceDoc.Field.PORT: Port.port2codename(port),
                       MarketpriceDoc.Field.TRADEGOOD: Tradegood.tradegood2codename(tradegood),
                       MarketpriceDoc.Field.RATE: rate,
                       MarketpriceDoc.Field.TREND: trend,

                       MarketpriceDoc.Field.CHANNEL_USER: channel_user_code,
                       MarketpriceDoc.Field.SERVER: Server.server2codename(server),

                       }

        return marketprice, channel_user
Ejemplo n.º 8
0
 def _price_lang2text(cls, price, tradegood_codename, lang):
     price_text = PriceSkill.price_lang2text(price, lang)
     tradegood_name = Tradegood.tradegood_lang2name(
         Tradegood.codename2tradegood(tradegood_codename), lang)
     return "{} {}".format(tradegood_name, price_text)
Ejemplo n.º 9
0
 def tg2aliases(tg):
     for _lang in langs_recognizable:
         yield from Tradegood.tradegood_lang2aliases(tg, _lang)
Ejemplo n.º 10
0
 def _dict_tradegood_en2port(cls):
     return merge_dicts([{cls.name2norm(Tradegood.tradegood_lang2name(tg, "en")): tg}
                         for tg in Tradegood.list_all()],
                        vwrite=vwrite_no_duplicate_key)
Ejemplo n.º 11
0
    def codename_lang2text(cls, tradegood_codename, lang):
        logger = HenriqueLogger.func_level2logger(cls.codename_lang2text,
                                                  logging.DEBUG)

        tradegood = Tradegood.codename2tradegood(tradegood_codename)

        filepath = os.path.join(FILE_DIR, "tmplt.{}.part.txt".format(lang))

        tgt_codename = Tradegood.tradegood2tradegoodtype(tradegood)
        tgt = Tradegoodtype.codename2tradegoodtype(
            tgt_codename) if tgt_codename else None
        category = Tradegoodtype.tradegoodtype2category(tgt) if tgt else None

        ports_selling = Port.tradegood2ports(tradegood_codename)
        prefers = Prefer.tradegood2prefers(tradegood_codename)

        # def prefers2cultures(prefers):
        #     culture_codenames = luniq(map(Prefer.prefer2culture, prefers))
        #     return lmap(Culture.codename2culture, culture_codenames)

        culture_codenames_preferred = luniq(map(Prefer.prefer2culture,
                                                prefers)) if prefers else []
        # culture_list_preferred = prefers2cultures(prefers) if prefers else []
        port_list_preferred = lchain(
            *map(Port.culture2ports, culture_codenames_preferred))

        def port2is_resistant(port):
            if not tgt:
                return False

            products = Product.port2products(Port.port2codename(port))
            tgt_codenames_port = smap(Product.product2tradegoodtype, products)

            logger.debug({
                "tgt": tgt,
                "tgt_codenames_port": tgt_codenames_port
            })
            return Tradegoodtype.tradegoodtype2codename(
                tgt) in tgt_codenames_port

        port_list_preferred_resistant = lfilter(port2is_resistant,
                                                port_list_preferred)

        def ports2str(ports):
            if not ports:
                return None

            return ", ".join(
                [Port.port_lang2name(port, lang) for port in ports])

        def culture_codenames2str(culture_codenames):
            if not culture_codenames:
                return None

            logger.debug({"culture_codenames": culture_codenames})
            cultures = lmap(Culture.codename2culture, culture_codenames)
            return ", ".join([
                Culture.culture_lang2name(culture, lang)
                for culture in cultures
            ])

        logger.debug(
            {"culture_codenames_preferred": culture_codenames_preferred})

        data = {
            "name":
            Tradegood.tradegood_lang2name(tradegood, lang),
            "category":
            Tradegoodcategory.tradegoodcategory2str(category)
            if category else None,
            "tradegoodtype":
            Tradegoodtype.tradegoodtype_lang2name(tgt, lang) if tgt else None,
            "ports_selling":
            ports2str(ports_selling),
            "cultures_preferred":
            culture_codenames2str(culture_codenames_preferred),
            "ports_preferred_resistant":
            ports2str(port_list_preferred_resistant),
        }
        text_out = str2strip(HenriqueJinja2.textfile2text(filepath, data))

        return text_out
Ejemplo n.º 12
0
 def product2tradegoodtype(cls, product):
     from henrique.main.document.tradegood.tradegood import Tradegood
     tradegood = Tradegood.codename2tradegood(cls.product2tradegood(product))
     return Tradegood.tradegood2tradegoodtype(tradegood)
Ejemplo n.º 13
0
 def dict_codename2tradegood(cls):
     tradegood_list_all = cls.tradegood_list_all()
     h = merge_dicts([{Tradegood.tradegood2codename(tradegood): tradegood} for tradegood in tradegood_list_all],
                     vwrite=DictTool.VWrite.f_vwrite2f_hvwrite(vwrite_no_duplicate_key),
                     )
     return h