コード例 #1
0
def realize_message(message: Message, template: Template,
                    language: str) -> Message:
    rnd = np.random.default_rng(42)

    # Disable logging for a while
    old_log_level = log.level
    log.setLevel(logging.WARNING)

    # Accessing a non-public method, but I can't be bothered to re-engineer the whole class.
    TEMPLATE_SELECTOR._add_template_to_message(message, template, [message])

    # Realize the single message + template pair
    doc_plan = DocumentPlanNode([message])
    (doc_plan, ) = SLOT_REALIZER.run(SERVICE.registry, rnd, language, doc_plan)
    (doc_plan, ) = DATE_REALIZER.run(SERVICE.registry, rnd, language, doc_plan)
    (doc_plan, ) = ENTITY_NAME_RESOLVER.run(SERVICE.registry, rnd, language,
                                            doc_plan)
    (doc_plan, ) = NUMBER_REALIZER.run(SERVICE.registry, rnd, language,
                                       doc_plan)
    (doc_plan, ) = MORPHOLOGICAL_REALIZER.run(SERVICE.registry, rnd, language,
                                              doc_plan)

    # Re-enable logging
    log.setLevel(old_log_level)

    msg: Message = doc_plan.children[0]
    return msg
コード例 #2
0
    def run(
        self,
        registry: Registry,
        random: RandomState,
        language: str,
        core_messages: List[Message],
        expanded_messages: List[Message],
    ):
        """
        Runs this pipeline component.
        """
        template_selector = TemplateSelector()
        slot_realizer = SlotRealizer()
        date_realizer = LanguageSplitComponent(
            {
                "en": EnglishEUDateRealizer(),
                "fi": FinnishEUDateRealizer(),
                "hr": CroatianEUDateRealizer(),
                "de": GermanEUDateRealizer(),
                "ru": RussianEUDateRealizer(),
                "ee": EstonianEUDateRealizer(),
                "sl": SlovenianEUDateRealizer(),
            }
        )
        number_realizer = EUNumberRealizer()
        entity_name_resolver = EUEntityNameResolver()

        root_logger = logging.getLogger()
        original_log_level = root_logger.level
        log.info(
            f"Setting root log level to WARNING (={logging.WARNING}) temporarily (was {original_log_level}), "
            f"because we're going to produce hella spam by running the first half of the pipeline at least a few "
            f"thousand times."
        )
        root_logger.setLevel(logging.WARNING)
        # i = 0
        # start = time.time()
        for msg in itertools.chain(core_messages, expanded_messages):
            doc_plan = DocumentPlanNode([msg])
            template_selector.run(registry, random, language, doc_plan, core_messages)
            slot_realizer.run(registry, random, language, doc_plan)
            date_realizer.run(registry, random, language, doc_plan)
            entity_name_resolver.run(registry, random, language, doc_plan)
            number_realizer.run(registry, random, language, doc_plan)

        root_logger.setLevel(original_log_level)
        log.info(f"Log level restored to {original_log_level}")
        return core_messages, expanded_messages