本文整理汇总了Python中Node.Node.toString方法的典型用法代码示例。如果您正苦于以下问题:Python Node.toString方法的具体用法?Python Node.toString怎么用?Python Node.toString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Node.Node
的用法示例。
在下文中一共展示了Node.toString方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: copy
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import toString [as 别名]
class Tree:
root = None
emptyTree = None
treeHashNum = None
def copy(self):
newTree = Tree()
newTree.root = self.root.copy()
return newTree
def getTreeHashNum(self):
if self.treeHashNum == None:
self.treeHashNum = str(time()) + "." + str(rnd.random())
return self.treeHashNum
def getHashNum(self):
if not self.root == None: return self.root.getHashNum()
else: return 0
@classmethod
def getEmptyTree(cls):
return Tree()
def __init__(self):
self.root = None
@classmethod
def loadFromFile(cls, filename):
trees = []
f = open(filename, "r")
for line in f.xreadlines():
if not line.strip()[0] == '#': #ignore comments where line begins with '#'
tree = Tree().load(line.strip())
trees.append(tree)
return trees
def initHashNum(self):
self.root.initHashNum()
def load(self, string):
self.root = Node(string[0])
self.root.loadChildren(string[2:-1])
self.root.initHashNum()
return self
def getSize(self):
if not self.root == None:
return self.root.getSize()
else:
return 0
def getVertices(self):
if not self.root == None:
return self.root.getVertices()
else: return []
def getRoot(self):
return self.root
def toString(self):
return self.root.toString()
def generateRandomTree(self, alphabet, deg, pvals):
rootIdx = rnd.random_integers(0, len(alphabet)-1, 1)
self.root = Node(alphabet[int(rootIdx)]).generateRandom(alphabet, deg, pvals)
self.root.initHashNum()
return self
def getStatistics(self):
return self.root.getStatistics()
def createGraph(self, name=""):
import pydot
if name != "":
graph = pydot.Subgraph(name, graph_type="digraph", strict=True, label=name)
else:
graph = pydot.Subgraph(graph_type="digraph", strict=True)
rootNode = pydot.Node(str(time()) + str(rnd.random()), label = str(self.root.getLabel()))
graph.add_node(rootNode)
self.root.visualize(rootNode, graph)
return graph
示例2: BigramNodeMerge
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import toString [as 别名]
def BigramNodeMerge(nodes,edges,classifier):
# Find mergable nodes pair
mergeable_list =[];
co_left = Counter(); # (A,x) Counter to compute number of its second element, from 1 to n. Key is A, value id 1..n
co_right = Counter(); # (x,A) Counter to compute number of its first element, from 1 to n. Key is A, value id 1..n
for node1 in nodes:
word1 = node1.getWord();
for node2 in nodes:
word2 = node2.getWord();
if (word1==word2 or word1.find('_')>=0 or word2.find('_')>=0): continue;
if (classifier.classify({'word1':word1, 'word2':word2})): # Merge able
mergeable_list.append((node1,node2));
co_left[node1] +=1;
co_right[node2] +=1;
print 'mergeable_list: ' +str(mergeable_list);
# Deal with overlap - CURRENTLY NOT APPLICABLE
A_1eft_list = [];
A_right_list = [];
# get elements that count ==1
for e in co_left.elements():
if (co_left[e]==1): A_1eft_list.append(e);
for e in co_right.elements():
if (co_right[e]==1): A_right_list.append(e);
print 'co_left: ' +str(co_left);
print 'co_right: ' +str(co_right);
print 'A_1eft_list: ' +str(A_1eft_list);
print 'A_right_list: ' +str(A_right_list);
final_mergeable_set = [];
for node_left in A_1eft_list:
local_mergeable_list = [];
print ' In merge node: ' + node_left.toString();
for node_right in A_right_list:
if (node_left.getword() == node_right.getword()):
print ' mergeable escape: duplicate label';
continue;
if ((node_left,node_right) in mergeable_list):
local_mergeable_list.append((node_left,node_right));
# if ((node_right,node_left) in mergeable_list):
# print ' mergeable escape: contradiction';
# continue;
# else:
# local_mergeable_list.append((node_left,node_right));
print ' local_mergeable_list: ' + str(local_mergeable_list);
if (local_mergeable_list == []): continue;
else: final_mergeable_set.add(random.choice(local_mergeable_list));
#Merging items in set
print 'final_mergeable_set: ' + str(final_mergeable_set);
for (node1,node2) in final_mergeable_set:
# disregard if any node node in node list
if ((node1 not in nodes) or (node2 not in nodes)): continue;
node1_in_edges = []; # list of edges that point TO node1
node1_out_edges= []; # list of edges that point FROM node1
node2_in_edges = []; # list of edges that point TO node2
node2_out_edges= []; # list of edges that point FROM node2
for ed in edges:
# delete connection betweent these node, if exist
if (ed.getWords() == (node1.getWord(),node2.getWord())):
edges.remove(ed);
# identify incoming nodes to node1
if (ed.getWord0() == node1.getWord()): node1_out_edges.append(ed);
if (ed.getWord1() == node1.getWord()): node1_in_edges.append(ed);
if (ed.getWord0() == node2.getWord()): node2_in_edges.append(ed);
if (ed.getWord1() == node2.getWord()): node2_out_edges.append(ed);
# Start merging
# initialize new node
new_node = Node(node1.getWord() + '_' + node2.getWord());
new_node.setwfreq(node1.getfreq() + node2.getfreq());
if (node1.gettimeAdded() < node2.gettimeAdded()):
new_node.setTime(node2.gettimeAdded());
else:
new_node.setTime(node1.gettimeAdded());
if (new_node.isDump()): continue;
# redirect edges:
new_edges = [];
# Incomming nodes
for ed in (node1_in_edges+node2_in_edges):
newEdge = Edge(ed.getWord0()+ " " + new_node.getWord());
newEdge.setefreq(ed.getfreq());
newEdge.setTime(ed.gettimeAdded());
if not(newEdge.isDump()):
res.add(newEdge);
new_edges.append(newEdge);
# Outgoing nodes
for ed in (node1_out_edges+node2_out_edges):
newEdge = Edge(new_node.getWord()+ " " + ed.getWord1());
newEdge.setefreq(ed.getfreq());
newEdge.setTime(ed.gettimeAdded());
if not(newEdge.isDump()):
new_edges.append(newEdge);
# Now interfere the data !!!!!!!!!!!!!!!!
edges = edges.difference(set(node1_in_edges + node1_out_edges + node2_in_edges + node2_out_edges));
nodes = nodes.difference(set([node1,node2]));
edges.update(new_edges);
nodes.add(new_node);
print '@@@' + new_node.toString();
print '* Merged ' + node1.getWord() + ' and ' + node2.getWord();
return (nodes,edges);