如何将其表示为网络?
How to represent this as a Network?
我想做一个可视化来显示一个子字符串属于多少个单词。一些子串可能属于同一组单词。例如,子字符串 tion
和 ten
都是单词 detention
和 attention
.
的子字符串
我考虑过树形表示,但在我的实际程序中有数百个 parent 到 child 关系,并且由于两个或三个 parent 可能具有相同的 child 它会变得非常复杂。因此,我认为网络可以工作。
这是设置它的代码。
from collections import defaultdict
words = ['mention', 'detention', 'attention', 'iteraction', 'interception', 'solution', 'iteraction',
'reiteration', 'determination', 'tension', 'tentative', 'intention', 'solution',
'tentative', 'concatenation', 'alternative', 'bitter', 'asterisk']
substring_dict = defaultdict(list)
ter = 'ter'
tion = 'tion'
ten = 'ten'
for entry in words:
if ter in entry:
substring_dict[ter].append(entry)
if tion in entry:
substring_dict[tion].append(entry)
if ten in entry:
substring_dict[ten].append(entry)
substring_dict
是一个列表字典,其中键是子字符串,值是子字符串所属的单词列表。
我如何在视觉上表现它?我在想我也可以对节点进行颜色编码。
您可以使用 networkx 可视化您的图表。
首先让我们对您的预处理做一个小改动:
words = ['mention', 'detention', 'attention', 'iteraction', 'interception', 'solution', 'iteraction','reiteration', 'determination', 'tension', 'tentative', 'intention', 'solution', 'tentative', 'concatenation', 'alternative', 'bitter', 'asterisk']
subs = ['ter','tion','ten']
edges = []
for word in words:
for sub in subs:
if sub in word:
edges.append( (word, sub) )
print edges[0:6]
# prints [('mention', 'tion'), ('detention', 'tion'), ('detention', 'ten'), ('attention', 'tion'), ('attention', 'ten'), ('iteraction', 'ter')]
让我们开始绘图:
import networkx as nx
import matplotlib.pyplot as plt
g = nx.Graph()
g.add_nodes_from(subs)
g.add_nodes_from(words)
g.add_edges_from(edges)
pos=nx.spring_layout(g)
nx.draw_networkx_nodes(g, pos,
nodelist=subs,
node_color='r',
node_size=1000,
alpha=0.8)
nx.draw_networkx_nodes(g, pos,
nodelist=words,
node_color='b',
node_size=1000,
alpha=0.8)
nx.draw_networkx_edges(g, pos, width=1.0, alpha=0.5)
nx.draw_networkx_labels(g, pos, dict(zip(subs,subs)) )
nx.draw_networkx_labels(g, pos, dict(zip(words,words)) )
它产生:
备注:
- 您可能想要处理节点的放置,现在我们使用的是
nx.spring_layout
,应该更改。
- 调整节点的大小,使标签不会延伸到外面。
我想做一个可视化来显示一个子字符串属于多少个单词。一些子串可能属于同一组单词。例如,子字符串 tion
和 ten
都是单词 detention
和 attention
.
我考虑过树形表示,但在我的实际程序中有数百个 parent 到 child 关系,并且由于两个或三个 parent 可能具有相同的 child 它会变得非常复杂。因此,我认为网络可以工作。
这是设置它的代码。
from collections import defaultdict
words = ['mention', 'detention', 'attention', 'iteraction', 'interception', 'solution', 'iteraction',
'reiteration', 'determination', 'tension', 'tentative', 'intention', 'solution',
'tentative', 'concatenation', 'alternative', 'bitter', 'asterisk']
substring_dict = defaultdict(list)
ter = 'ter'
tion = 'tion'
ten = 'ten'
for entry in words:
if ter in entry:
substring_dict[ter].append(entry)
if tion in entry:
substring_dict[tion].append(entry)
if ten in entry:
substring_dict[ten].append(entry)
substring_dict
是一个列表字典,其中键是子字符串,值是子字符串所属的单词列表。
我如何在视觉上表现它?我在想我也可以对节点进行颜色编码。
您可以使用 networkx 可视化您的图表。
首先让我们对您的预处理做一个小改动:
words = ['mention', 'detention', 'attention', 'iteraction', 'interception', 'solution', 'iteraction','reiteration', 'determination', 'tension', 'tentative', 'intention', 'solution', 'tentative', 'concatenation', 'alternative', 'bitter', 'asterisk']
subs = ['ter','tion','ten']
edges = []
for word in words:
for sub in subs:
if sub in word:
edges.append( (word, sub) )
print edges[0:6]
# prints [('mention', 'tion'), ('detention', 'tion'), ('detention', 'ten'), ('attention', 'tion'), ('attention', 'ten'), ('iteraction', 'ter')]
让我们开始绘图:
import networkx as nx
import matplotlib.pyplot as plt
g = nx.Graph()
g.add_nodes_from(subs)
g.add_nodes_from(words)
g.add_edges_from(edges)
pos=nx.spring_layout(g)
nx.draw_networkx_nodes(g, pos,
nodelist=subs,
node_color='r',
node_size=1000,
alpha=0.8)
nx.draw_networkx_nodes(g, pos,
nodelist=words,
node_color='b',
node_size=1000,
alpha=0.8)
nx.draw_networkx_edges(g, pos, width=1.0, alpha=0.5)
nx.draw_networkx_labels(g, pos, dict(zip(subs,subs)) )
nx.draw_networkx_labels(g, pos, dict(zip(words,words)) )
它产生:
备注:
- 您可能想要处理节点的放置,现在我们使用的是
nx.spring_layout
,应该更改。 - 调整节点的大小,使标签不会延伸到外面。