Grammalecte  Diff

Differences From Artifact [0d3ea50a3a]:

To Artifact [8854c10a7e]:


     4      4   RULE GRAPH BUILDER
     5      5   """
     6      6   
     7      7   # by Olivier R.
     8      8   # License: MPL 2
     9      9   
    10     10   import re
    11         -import traceback
    12         -
    13     11   
    14     12   
    15     13   class DARG:
    16     14       """DIRECT ACYCLIC RULE GRAPH"""
    17     15       # This code is inspired from Steve Hanov’s DAWG, 2011. (http://stevehanov.ca/blog/index.php?id=115)
    18     16   
    19     17       def __init__ (self, lRule, sLangCode):
................................................................................
    53     51               nCommonPrefix += 1
    54     52   
    55     53           # Check the lUncheckedNodes for redundant nodes, proceeding from last
    56     54           # one down to the common prefix size. Then truncate the list at that point.
    57     55           self._minimize(nCommonPrefix)
    58     56   
    59     57           # add the suffix, starting from the correct node mid-way through the graph
    60         -        if len(self.lUncheckedNodes) == 0:
           58  +        if not self.lUncheckedNodes:
    61     59               oNode = self.oRoot
    62     60           else:
    63     61               oNode = self.lUncheckedNodes[-1][2]
    64     62   
    65     63           iToken = nCommonPrefix
    66     64           for sToken in aRule[nCommonPrefix:]:
    67     65               oNextNode = Node()
................................................................................
   125    123           dKeyTrans = {}
   126    124           for i, nKey in enumerate(dGraph):
   127    125               dKeyTrans[nKey] = i
   128    126           # replace keys
   129    127           dNewGraph = {}
   130    128           for nKey, dVal in dGraph.items():
   131    129               dNewGraph[dKeyTrans[nKey]] = dVal
   132         -        for nKey, dVal in dGraph.items():
          130  +        for _, dVal in dGraph.items():
   133    131               for sArc, val in dVal.items():
   134         -                if type(val) is int:
          132  +                if isinstance(val, int):
   135    133                       dVal[sArc] = dKeyTrans[val]
   136    134                   else:
   137         -                    for sArc, nKey in val.items():
   138         -                        val[sArc] = dKeyTrans[nKey]
          135  +                    for sArc2, nKey in val.items():
          136  +                        val[sArc2] = dKeyTrans[nKey]
   139    137           return dNewGraph
   140    138   
   141    139       def _sortActions (self, dGraph):
   142    140           "when a pattern is found, several actions may be launched, and it must be performed in a certain order"
   143         -        for nKey, dVal in dGraph.items():
          141  +        for _, dVal in dGraph.items():
   144    142               if "<rules>" in dVal:
   145         -                for sLineId, nKey in dVal["<rules>"].items():
          143  +                for _, nKey in dVal["<rules>"].items():
   146    144                       # we change the dictionary of actions in a list of actions (values of dictionary all points to the final node)
   147    145                       if isinstance(dGraph[nKey], dict):
   148    146                           dGraph[nKey] = sorted(dGraph[nKey].keys())
   149    147   
   150    148       def _checkRegexes (self, dGraph):
   151    149           "check validity of regexes"
   152    150           aRegex = set()
   153         -        for nKey, dVal in dGraph.items():
          151  +        for _, dVal in dGraph.items():
   154    152               if "<re_value>" in dVal:
   155    153                   for sRegex in dVal["<re_value>"]:
   156    154                       if sRegex not in aRegex:
   157    155                           self._checkRegex(sRegex)
   158    156                           aRegex.add(sRegex)
   159    157               if "<re_morph>" in dVal:
   160    158                   for sRegex in dVal["<re_morph>"]:
................................................................................
   169    167               sPattern, sNegPattern = sRegex.split("¬")
   170    168               try:
   171    169                   if not sNegPattern:
   172    170                       print("# Warning! Empty negpattern:", sRegex)
   173    171                   re.compile(sPattern)
   174    172                   if sNegPattern != "*":
   175    173                       re.compile(sNegPattern)
   176         -            except:
          174  +            except re.error:
   177    175                   print("# Error. Wrong regex:", sRegex)
   178    176                   exit()
   179    177           else:
   180    178               try:
   181    179                   if not sRegex:
   182    180                       print("# Warning! Empty pattern:", sRegex)
   183    181                   re.compile(sRegex)
   184         -            except:
          182  +            except re.error:
   185    183                   print("# Error. Wrong regex:", sRegex)
   186    184                   exit()
   187    185   
   188    186   
   189    187   class Node:
   190    188       """Node of the rule graph"""
   191    189