class TemplateNLG(AbstractTemplateNLG): """\ A simple text-replacement template NLG implementation with the ability to resort to a back-off system if no appropriate template is found. """ def __init__(self, cfg): super(TemplateNLG, self).__init__(cfg) # load templates if 'model' in self.cfg['NLG']['Template']: self.load_templates(self.cfg['NLG']['Template']['model']) # load ontology self.ontology = Ontology() if 'ontology' in self.cfg['NLG']['Template']: self.ontology.load(cfg['NLG']['Template']['ontology']) # initialize pre- and post-processing self.preprocessing = None self.postprocessing = None if 'preprocessing_cls' in self.cfg['NLG']['Template']: if 'preprocessing_config' in self.cfg['NLG']['Template']: self.preprocessing = self.cfg['NLG']['Template'][ 'preprocessing_cls']( self.ontology, self.cfg['NLG']['Template']['preprocessing_config']) else: self.preprocessing = self.cfg['NLG']['Template'][ 'preprocessing_cls'](self.ontology) if 'postprocessing_cls' in self.cfg['NLG']['Template']: self.postprocessing = self.cfg['NLG']['Template'][ 'postprocessing_cls']() def fill_in_template(self, tpl, svs): """\ Simple text replacement template filling. Applies template NLG pre- and postprocessing, if applicable. """ svs_dict = dict(svs) if self.preprocessing is not None: tpl, svs_dict = self.preprocessing.preprocess(tpl, svs_dict) out_text = tpl.format(**svs_dict) if self.postprocessing is not None: return self.postprocessing.postprocess(out_text) return out_text
class TemplateNLG(AbstractTemplateNLG): """\ A simple text-replacement template NLG implementation with the ability to resort to a back-off system if no appropriate template is found. """ def __init__(self, cfg): super(TemplateNLG, self).__init__(cfg) # load templates if 'model' in self.cfg['NLG']['Template']: self.load_templates(self.cfg['NLG']['Template']['model']) # load ontology self.ontology = Ontology() if 'ontology' in self.cfg['NLG']['Template']: self.ontology.load(cfg['NLG']['Template']['ontology']) # initialize pre- and post-processing self.preprocessing = None self.postprocessing = None if 'preprocessing_cls' in self.cfg['NLG']['Template']: self.preprocessing = self.cfg['NLG']['Template']['preprocessing_cls'](self.ontology) if 'postprocessing_cls' in self.cfg['NLG']['Template']: self.postprocessing = self.cfg['NLG']['Template']['postprocessing_cls']() def fill_in_template(self, tpl, svs): """\ Simple text replacement template filling. Applies template NLG pre- and postprocessing, if applicable. """ svs_dict = dict(svs) if self.preprocessing is not None: tpl, svs_dict = self.preprocessing.preprocess(tpl, svs_dict) out_text = tpl.format(**svs_dict) if self.postprocessing is not None: return self.postprocessing.postprocess(out_text) return out_text