Module rule |
[hide private]
[frames] | no frames]

# Source Code for Module rule

     1  from ruleset import *
     2  from constants import *
     3  import math
     4
     5  __docformat__='epytext'
     6
     7 -class Rule:
     8      MAX_COST = 1000000  #cost to return if there is no rule
     9
    10 -    def __init__(self,name=None,add_coverage=0,add_confidence=0):
    11          """
    12          Construct a new rule.
    13          """
    14          self.exceptions = Ruleset()
    15          self.name = name
    16          self.additional_coverage = add_coverage
    17          self.additional_confidence = add_confidence
    18          self.coverage = 0
    19          self.confidence = 0
    20          self.length = 0.0
    21          self.error = 0.0
    22          self.lenEH = 0.0
    23          self.sub_data = {}
    24          self.sub_pos_data = {}
    25
    26 -    def __repr__(self):
    27          """
    28          String representation of the ruleset.
    29          """
    30          return self.name + str(self.sub_rules)
    31
    32 -    def append(self,rule):
    33          """
    34          Append a rule to the ruleset.
    35
    36          @type  rule: L{Rule}
    37          @param rule: rule to be appended.
    38          """
    39          if(not isinstance(rule, Rule)):
    40              raise "Not a rule!"
    41          self.sub_rules.append(rule)
    42
    43 -    def set_exceptions(self, rules):
    44          """
    45          Add exceptions to the rule.
    46
    47          @type  rules: L{Ruleset}
    48          @param rules: exceptions to be added.
    49          """
    50          self.exceptions = rules
    51          self.length = self.length + 1 + rules.length
    52          self.error = rules.error
    53 -    def get_error(self):
    54          """
    55          Calculate error percentage.
    56
    57          @rtype:   float
    58          @return:  error percentage of the rule.
    59          """
    60          return (self.error / self.coverage) * 100
    61 -    def get_single_error(self):
    62          """
    63          Calculate error percentage without taking exceptions into account.
    64
    65          @rtype:   float
    66          @return:  error percentage of the rule without exceptions.
    67          """
    68          return ((self.coverage - self.confidence) / self.coverage) * 100
    69
    70 -    def shorter_than(self, rule2, c):
    71          """
    72          Compare rule's description length with another rule.
    73
    74          @type  rule2: L{Rule}
    75          @param rule2: rule to compare with.
    76          @rtype:   boolean
    77          @return:  True if rule has shorter description length than rule2.
    78          """
    79          if rule2.name == None:
    80              return True
    81          desc_length = c * self.length + self.lenEH
    82          desc_length2 = c * rule2.length + rule2.lenEH
    83          return (desc_length < desc_length2 or
    84              (desc_length == desc_length2 and self.confidence/self.coverage > rule2.confidence/rule2.coverage) or
    85              (desc_length == desc_length2 and self.confidence/self.coverage == rule2.confidence/rule2.coverage) and
    86                  self.confidence > rule2.confidence)
    87
    88 -    def cost(self):
    89          """
    90          Find the cost per element for the rule.
    91          """
    92          if self.additional_confidence == 0 or self.name is None:
    93              return Rule.MAX_COST
    94          return (self.length + self.error) / self.additional_confidence
    95