def infer_unary(self, result, argument): """Attempt to build a unary modifier from existing templates if possible.""" if result == argument: template = self.lookup(argument) if template is not None: taggedcat = template.predarg_category.complete_tags() return self.add_unary_rule( Category.combine(taggedcat, '\\', taggedcat, False), taggedcat) return None
def infer_template(self, category): """Attempt to build a template from existing templates if possible.""" category = category.remove_conj_feature() if category.isfunctor and not self.issupported(category): catArg = category.argument_category() catArgArg = catArg.argument_category() catResult = category.result_category() if category.istype_raised and (self.issupported(catResult) or catResult.isatom) \ and (self.issupported(catArgArg) or catArgArg.isatom): global _logger # If the catgeory is type raised then check if result type exists and build now. # TODO: This should be sent to a log _logger.info('Adding type-raised category %s to TEMPLATES' % category.signature) # Template categories contain predarg info so build new from these if catResult.isfunctor: catResult = self.lookup( catResult).predarg_category.complete_tags() else: catResult = Category(catResult.signature + '_999') # synthesize pred-arg info if catArgArg.isfunctor: # FIXME: Should really check predarg info does not overlap with catResult. Chances are low. catArgArg = self.lookup( catArgArg).predarg_category.complete_tags() else: catArgArg = Category(catArgArg.signature + '_998') # synthesize pred-arg info newcat = Category.combine( catResult, category.slash, Category.combine(catResult, category.argument_category().slash, catArgArg), False) return self.add_template(newcat) elif category.ismodifier and self.issupported(catResult): predarg = self.lookup( catResult).predarg_category.complete_tags() newcat = Category.combine(predarg, category.slash, predarg, False) return self.add_template(newcat) return None
def create_key(result, argument): """Create a rule key from result and argument categories. Args: result: The result category. argument: The argument category. Returns: A string. Remarks: Both categories must NOT include predarg tags. To remove tags do Category.clean(True). """ if not isinstance(result, Category): raise TypeError( 'UnaryRule.create_key() expects a Category instance ') if not isinstance(argument, Category): raise TypeError( 'UnaryRule.create_key() expects a Category instance') return Category.combine(result, '\\', argument)
def __init__(self, result, argument): """Constructor for unary rule `result <- argument`. Args: result: The result category. argument: The argument category. Remarks: Both categories must include predarg tags. """ if not isinstance(result, Category): raise TypeError('UnaryRule expects a result Category') if not isinstance(argument, Category): raise TypeError('UnaryRule expects a argument Category') # We implement unary rules using backward application of the functor below ucat = Category.combine(result.clean(), '\\', argument.clean(), cacheable=False) result, final_tag = result.trim_functor_tag() if final_tag is not None: ucat = Category('(%s)_%s' % (ucat, final_tag)) self._template = FunctorTemplate.create_from_category(ucat)