Grammalecte  Diff

Differences From Artifact [49bc8d2039]:

To Artifact [9b17f8d6af]:


     1         -#!python3
     2         -
     3      1   """
     4      2   RULE GRAPH BUILDER
     5      3   """
     6      4   
     7      5   # by Olivier R.
     8      6   # License: MPL 2
     9      7   
................................................................................
    11      9   
    12     10   
    13     11   class DARG:
    14     12       """DIRECT ACYCLIC RULE GRAPH"""
    15     13       # This code is inspired from Steve Hanov’s DAWG, 2011. (http://stevehanov.ca/blog/index.php?id=115)
    16     14   
    17     15       def __init__ (self, lRule, sLangCode):
    18         -        print(" > DARG", end="")
    19         -
    20     16           # Preparing DARG
    21     17           self.sLangCode = sLangCode
    22     18           self.nRule = len(lRule)
    23     19           self.aPreviousRule = []
    24     20           Node.resetNextId()
    25     21           self.oRoot = Node()
    26     22           self.lUncheckedNodes = []  # list of nodes that have not been checked for duplication.
................................................................................
    31     27           # build
    32     28           lRule.sort()
    33     29           for aRule in lRule:
    34     30               self.insert(aRule)
    35     31           self.finish()
    36     32           self.countNodes()
    37     33           self.countArcs()
    38         -        self.displayInfo()
    39     34   
    40     35       # BUILD DARG
    41     36       def insert (self, aRule):
    42     37           "insert a new rule (tokens must be inserted in order)"
    43     38           if aRule < self.aPreviousRule:
    44     39               exit("# Error: tokens must be inserted in order.")
    45     40   
................................................................................
    94     89   
    95     90       def countArcs (self):
    96     91           "count arcs within the whole graph"
    97     92           self.nArc = len(self.oRoot.dArcs)
    98     93           for oNode in self.lMinimizedNodes:
    99     94               self.nArc += len(oNode.dArcs)
   100     95   
   101         -    def displayInfo (self):
           96  +    def __str__ (self):
   102     97           "display informations about the rule graph"
   103         -        print(": {:>10,} rules,  {:>10,} nodes,  {:>10,} arcs".format(self.nRule, self.nNode, self.nArc))
           98  +        return " > DARG: {:>10,} rules,  {:>10,} nodes,  {:>10,} arcs".format(self.nRule, self.nNode, self.nArc)
   104     99   
   105    100       def createGraph (self):
   106    101           "create the graph as a dictionary"
   107    102           dGraph = { 0: self.oRoot.getNodeAsDict() }
   108    103           for oNode in self.lMinimizedNodes:
   109    104               sHashId = oNode.__hash__()
   110    105               if sHashId not in dGraph: