Index: graphspell-js/dawg.js ================================================================== --- graphspell-js/dawg.js +++ graphspell-js/dawg.js @@ -225,11 +225,11 @@ countNodes () { this.nNode = this.dMinimizedNodes.size; } countArcs () { - this.nArc = 0; + this.nArc = this.oRoot.arcs.size; for (let oNode of this.dMinimizedNodes.values()) { this.nArc += oNode.arcs.size; } } Index: graphspell/dawg.py ================================================================== --- graphspell/dawg.py +++ graphspell/dawg.py @@ -230,11 +230,11 @@ "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 " @@ -607,26 +607,34 @@ "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 " self.arcs = collections.OrderedDict(sorted(self.arcs.items(), key=lambda t: dValOccur.get(t[0], 0), reverse=True))