当前位置: 首页>>代码示例>>Python>>正文


Python Node.getWord方法代码示例

本文整理汇总了Python中Node.Node.getWord方法的典型用法代码示例。如果您正苦于以下问题:Python Node.getWord方法的具体用法?Python Node.getWord怎么用?Python Node.getWord使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Node.Node的用法示例。


在下文中一共展示了Node.getWord方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MergeDuplicate

# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import getWord [as 别名]
def MergeDuplicate(nodes,edges,lemmatizer):
	new_nodes = [];
	eli_nodes = [];
	new_edges = [];
	eli_edges = [];
	for node1 in nodes:
		word1 = node1.getWord();
		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 node2 in nodes:
			word2 = node2.getWord();
			if (word1 != word2 and (node1 not in eli_nodes) and (node2 not in eli_nodes) and (word1.lower() == word2.lower() or lemmatizer.lemmatize(word1.lower()) == lemmatizer.lemmatize(word2.lower()))):
				# Start merging
				#	initialize new node
				new_node = Node(word1.title());
				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;
				new_nodes.append(new_node);
				#	redirect 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()):
						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);
				eli_nodes = eli_nodes +[node1,node2];
				eli_edges = eli_edges + node1_in_edges + node1_out_edges + node2_in_edges + node2_out_edges;
	# Now interfere the data !!!!!!!!!!!!!!!!
	for ed in eli_nodes:
		edges.discard(ed);
	for nd in eli_nodes:
		nodes.discard(nd);

	edges.update(new_edges);
	nodes.update(new_nodes);
	return (nodes,edges);		
开发者ID:nduy,项目名称:graphbased-headlines-topic-track,代码行数:53,代码来源:functions.py

示例2: BigramNodeMerge

# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import getWord [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);
开发者ID:nduy,项目名称:graphbased-headlines-topic-track,代码行数:102,代码来源:functions.py


注:本文中的Node.Node.getWord方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。