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