<commit_before>from opencog.atomspace import types, TruthValue

import pln.formulas as formulas

import math

'''Some Rules evaluate various kinds of logical links based explicitly on set membership. A set = a ConceptNode.
Other Rules calculate them heuristically, based on set probabilities and logical links.'''

BOOLEAN_LINKS = [types.AndLink, types.OrLink, types.NotLink]
FIRST_ORDER_LINKS = [types.InheritanceLink, types.SubsetLink, types.IntensionalInheritanceLink, types.SimilarityLink, types.ExtensionalSimilarityLink, types.IntensionalSimilarityLink]
HIGHER_ORDER_LINKS = [types.ImplicationLink, types.EquivalenceLink]

class Rule(object):

    def __init__ (self, outputs, inputs, formula):
        '''@outputs is one or more Trees representing the structure of Atom (usually a Link)
    to be produced by this Rule. If it's a variable then any kind of Atom
    can be produced.
    @inputs (list of Trees) specifies what kinds of Atoms
    are necessary to produce it. There should be variables used in the head
    that also appear in the goals. This means they will be the same atom.
    It's OK to reuse the same variable numbers in different Rules - they'll
    be converted to unique variables automatically.
    You can use either @formula or @tv. You specify a formula method from formulas.py;
    it will be called with the TVs of the relevant Atoms, to calculate the TruthValue for
    the resulting Atom.'''
        assert type(outputs) == list
        assert type(inputs) == list

        self._outputs = outputs
        self._inputs = inputs

        self.formula = formula
        self.name = self.__class__.__name__
    
        print self.name
        for atom in self._inputs + self._outputs:
            assert atom.type != 65535 # missing type bug (cython issue?)

    def calculate(self, input_atoms):
        '''Compute the output TV(s) based on the input atoms'''
        tvs = [atom.tv for atom in input_atoms]
        result_tvs = self.formula(tvs)
        if any((tv.mean < 0 or tv.mean > 1 or tv.count == 0) for tv in result_tvs):
            return None
        else:
            return result_tvs<commit_msg>PLN Rule names are less ambiguous<commit_after>from opencog.atomspace import types, TruthValue

import pln.formulas as formulas

import math

'''Some Rules evaluate various kinds of logical links based explicitly on set membership. A set = a ConceptNode.
Other Rules calculate them heuristically, based on set probabilities and logical links.'''

BOOLEAN_LINKS = [types.AndLink, types.OrLink, types.NotLink]
FIRST_ORDER_LINKS = [types.InheritanceLink, types.SubsetLink, types.IntensionalInheritanceLink, types.SimilarityLink, types.ExtensionalSimilarityLink, types.IntensionalSimilarityLink]
HIGHER_ORDER_LINKS = [types.ImplicationLink, types.EquivalenceLink]

class Rule(object):

    def __init__ (self, outputs, inputs, formula):
        '''@outputs is one or more Trees representing the structure of Atom (usually a Link)
    to be produced by this Rule. If it's a variable then any kind of Atom
    can be produced.
    @inputs (list of Trees) specifies what kinds of Atoms
    are necessary to produce it. There should be variables used in the head
    that also appear in the goals. This means they will be the same atom.
    It's OK to reuse the same variable numbers in different Rules - they'll
    be converted to unique variables automatically.
    You can use either @formula or @tv. You specify a formula method from formulas.py;
    it will be called with the TVs of the relevant Atoms, to calculate the TruthValue for
    the resulting Atom.'''
        assert type(outputs) == list
        assert type(inputs) == list

        self._outputs = outputs
        self._inputs = inputs

        self.formula = formula
        self.name = self.__class__.__name__
        self.name+= ' (' +self._get_type_names(inputs)+ ' -> '
        self.name+= self._get_type_names(outputs)+')'

        print self.name

    def _get_type_names(self, templates):
        return ' '.join(template.type_name for template in templates)

    def calculate(self, input_atoms):
        '''Compute the output TV(s) based on the input atoms'''
        tvs = [atom.tv for atom in input_atoms]
        result_tvs = self.formula(tvs)
        if any((tv.mean < 0 or tv.mean > 1 or tv.count == 0) for tv in result_tvs):
            return None
        else:
            return result_tvs
{'fraction_non_alphanumeric': 0.07157417893544733, 'fraction_numerical': 0.002491506228765572, 'mean_word_length': 3.197718631178707, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4605520', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1190, 'n_words': 500}