228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
-
+
|
def countNodes (self):
"count the number of nodes of the whole word graph"
self.nNode = len(self.lMinimizedNodes)
def countArcs (self):
"count the number of arcs in the whole word graph"
self.nArc = 0
self.nArc = len(self.oRoot.arcs)
for oNode in self.lMinimizedNodes:
self.nArc += len(oNode.arcs)
def sortNodeArcs (self, dValOccur):
"sort arcs of each node according to <dValOccur>"
print(" > Sort node arcs")
self.oRoot.sortArcs(dValOccur)
|
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
|
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
+
|
def setPos (self): # version 2
"define a position for node (version 2)"
self.pos = DawgNode.NextPos
DawgNode.NextPos += 1
def __str__ (self):
s = "Node " + str(self.i) + " @ " + str(self.addr) + (" [final]" if self.final else "") + "\n"
for arc, node in self.arcs.items():
s += " " +str(arc)
s += " > " + str(node.i)
s += " @ " + str(node.addr) + "\n"
return s
def __repr__ (self):
# Caution! this function is used for hashing and comparison!
sFinalChar = "1" if self.final else "0"
l = [sFinalChar]
for (key, node) in self.arcs.items():
l.append(str(key))
for arc, node in self.arcs.items():
l.append(str(arc))
l.append(str(node.i))
return "_".join(l)
def __hash__ (self):
# Used as a key in a python dictionary.
return self.__str__().__hash__()
return self.__repr__().__hash__()
def __eq__ (self, other):
# Used as a key in a python dictionary.
# Nodes are equivalent if they have identical arcs, and each identical arc leads to identical states.
return self.__str__() == other.__str__()
return self.__repr__() == other.__repr__()
def sortArcs (self, dValOccur):
"sort arcs of node according to <dValOccur>"
self.arcs = collections.OrderedDict(sorted(self.arcs.items(), key=lambda t: dValOccur.get(t[0], 0), reverse=True))
def sortArcs2 (self, dValOccur, lArcVal):
"sort arcs of each node depending on the previous char"
|