【发布时间】:2017-01-08 01:57:22
【问题描述】:
我使用 networkx 制作了一个图表,该图表具有三个属性(id、publication_authors、publication title)。 我想检查节点属性(作者),如果两个节点之间的公共节点属性大于一个,我想添加这两个节点属性并从图中删除第二个节点。例如,我有以下图表:
[(0, {'publication_authors': {'Bob Johnson', 'Stephen Michell', 'Andy J. Wellings', 'Jorg Kienzle', 'Thomas Wolf', 'Bo Sanden'}, 'title': 'Object-Oriented Programming and Protected Objects in Ada 95'}), (1, {'publication_authors': {'Bob Johnson'}, 'title': 'UNIX Metrics: Is The Data In Open Systems The Same From Platform To Platform?'}), (2, {'publication_authors': {'Bob Johnson'}, 'title': 'Triad Of Computing In The 21st Century Or, Back To The Future Again'}), (3, {'publication_authors': {'Bob Johnson'}, 'title': 'User-centeredness, situatedness, and designing the media of computer documentation'}), (4, {'publication_authors': {'Bob Johnson'}, 'title': "Introduction to commentaries on 'Spurious Coin: A History of Science, Management, and Technical Writing' by Bernadette Longo"}), (5, {'publication_authors': {'Brian Lawrence', 'Bob Johnson'}, 'title': 'Manager: The Project Scoping Gamble'}), (6, {'publication_authors': {'Bob Johnson', 'Stephen Michell', 'Andy J. Wellings', 'Jorg Kienzle', 'Thomas Wolf', 'Bo Sanden'}, 'title': 'Integrating object-oriented programming and protected objects in Ada 95'}), (7, {'publication_authors': {'Robert Johnson', 'Michael Hackett', 'Bob Johnson', 'Hung Quoc Nguyen'}, 'title': 'Testing Applications on the Web: Test Planning for Mobile and Internet-Based Systems, 2 edition'}), (8, {'publication_authors': {'Bob Johnson'}, 'title': 'The Wired Neighborhood: An Extended Multimedia Conversation'}), (9, {'publication_authors': {'Bob Johnson'}, 'title': 'Introduction to the book commentaries'}), (10, {'publication_authors': {'Bob Johnson'}, 'title': 'The cult of ISDN'})]
我想获取第一个节点并将其与剩余的 10 个节点进行比较,如果任何两个节点(0、6)之间的“publication_authors”大于 1,那么我想修改第一个节点(在本例中为 0)并将节点6的属性合并为0。然后从图中删除节点6。我已经实现了以下代码,但它给了我错误。请有人帮我改正。提前致谢。
import networkx as nx
ground_truth_file = 'C:\\Bob Johnson.txt'
G = nx.DiGraph()
f = open(ground_truth_file, mode='r')
lines = f.readlines()
i=0
for line in lines:
line.strip()
pub_authors = set()
tokens = line.split('<>')
authors = tokens[1]
title = tokens[2]
venue = tokens[3]
num_of_authors = authors.split(',')
for author in num_of_authors:
pub_authors.add(author)
G.add_node(i,publication_authors=pub_authors, title=title)
i=i+1
num_nodes =G.number_of_nodes()
for node in range (num_nodes-1):
for next_node in range (node+1,num_nodes):
a = G.node[node]['publication_authors']
b = G.node[next_node]['publication_authors']
common_authors = a.intersection(b)
if (len(common_authors)>1):
c = G.node[node]['title']
d = G.node[next_node]['title']
cluster_authors = a.union(b)
cluster_title = c+d
G.node[node]['publication_authors']=cluster_authors
G.node[node]['title']=cluster_title
G.remove_node(next_node)
else:
print ('Not enough common authors')
print(G.nodes(data=True))`
我的txt文件是
0<>Andy J. Wellings,Bob Johnson,Bo Sanden,Jorg Kienzle,Thomas Wolf,Stephen Michell<>Object-Oriented Programming and Protected Objects in Ada 95<>Ada-Europe<>2000<>null
1<>Bob Johnson<>UNIX Metrics: Is The Data In Open Systems The Same From Platform To Platform?<>Int. CMG Conference<>1995<>null
1<>Bob Johnson<>Triad Of Computing In The 21st Century Or, Back To The Future Again<>Int. CMG Conference<>1995<>null
2<>Bob Johnson<>User-centeredness, situatedness, and designing the media of computer documentation<>SIGDOC<>1990<>Miami University of Ohio
3<>Bob Johnson<>Introduction to commentaries on 'Spurious Coin: A History of Science, Management, and Technical Writing' by Bernadette Longo<>ACM Journal of Computer Documentation<>2001<>Michigan Technological University
4<>Brian Lawrence,Bob Johnson<>Manager: The Project Scoping Gamble<>IEEE Software<>1997<>null
0<>Andy J. Wellings,Bob Johnson,Bo Sanden,Jorg Kienzle,Thomas Wolf,Stephen Michell<>Integrating object-oriented programming and protected objects in Ada 95<>ACM Trans. Program. Lang. Syst.<>2000<>null
5<>Hung Quoc Nguyen,Bob Johnson,Robert Johnson,Michael Hackett<>Testing Applications on the Web: Test Planning for Mobile and Internet-Based Systems, 2 edition<>null<>2002<>null
2<>Bob Johnson<>The Wired Neighborhood: An Extended Multimedia Conversation<>ACM SIGDOC Asterisk Journal of Computer Documentation<>1997<>Miami University, Oxford, OH
2<>Bob Johnson<>Introduction to the book commentaries<>ACM SIGDOC Asterisk Journal of Computer Documentation<>1998<>Miami University, Oxford, OH
6<>Bob Johnson<>The cult of ISDN<>PC/Computing<>1989<>null
【问题讨论】:
-
如果你用最小的图表来重现你的问题,那么帮助你会容易得多。不可能读取整个数据库(如果是纯文本则更糟)来尝试找出您需要的内容。
-
0ABC 1AGF 2JT 3U 4A 5ABTF 6AFT 第一个节点具有属性 ABC & if 在此的任何其他节点中图两个字符匹配然后我们必须将这些字符合并到这个节点并删除另一个节点。例如,节点 0 具有 ABC 属性,节点 5 具有 ABTF,因此在这两个节点中 AB 是通用的,因此我们必须将这些节点合并到 0 中,并从 networkx 图中删除节点 5。合并后将变为: 0ABCTF 1AGF 2JHT 3UJHG 4A 6AFT 现在节点 0 与节点 6 具有三个公共。因此节点 0 将被修改,而节点 6 将被修改已删除。
-
@Imanol Luengo 0ABC, 1AGF, 2JT, 3U, 4A, 5ABTF, 6 这些是networkx 图。
-
请人回复