UNIX电子邮件#

从一个Unix邮箱创建一个有向图,允许多个边和自循环。节点是电子邮件地址,其中包含从发送者指向接收者的链接。边缘数据是包含所有电子邮件数据的python email.message对象。

这个例子展示了 DiGraph 保存任意python对象的边缘数据(在本例中是电子邮件消息列表)。

示例unix电子邮件邮箱“unix_email.mbox”可以在以下位置找到:

plot unix email

出:

From: alice@edu To: bob@gov Subject: NetworkX
From: bob@gov To: alice@edu Subject: Re: NetworkX
From: bob@gov To: ted@com Subject: Re: Graph package in Python?
From: ted@com To: bob@gov Subject: Graph package in Python?
From: ted@com To: bob@gov Subject: get together for lunch to discuss Networks?
From: ted@com To: carol@gov Subject: get together for lunch to discuss Networks?
From: ted@com To: alice@edu Subject: get together for lunch to discuss Networks?

from email.utils import getaddresses, parseaddr
import mailbox

import matplotlib.pyplot as plt
import networkx as nx

# unix mailbox recipe
# see https://docs.python.org/3/library/mailbox.html


def mbox_graph():
    mbox = mailbox.mbox("unix_email.mbox")  # parse unix mailbox

    G = nx.MultiDiGraph()  # create empty graph

    # parse each messages and build graph
    for msg in mbox:  # msg is python email.Message.Message object
        (source_name, source_addr) = parseaddr(msg["From"])  # sender
        # get all recipients
        # see https://docs.python.org/3/library/email.html
        tos = msg.get_all("to", [])
        ccs = msg.get_all("cc", [])
        resent_tos = msg.get_all("resent-to", [])
        resent_ccs = msg.get_all("resent-cc", [])
        all_recipients = getaddresses(tos + ccs + resent_tos + resent_ccs)
        # now add the edges for this mail message
        for (target_name, target_addr) in all_recipients:
            G.add_edge(source_addr, target_addr, message=msg)

    return G


G = mbox_graph()

# print edges with message subject
for (u, v, d) in G.edges(data=True):
    print(f"From: {u} To: {v} Subject: {d['message']['Subject']}")

pos = nx.spring_layout(G, iterations=10, seed=227)
nx.draw(G, pos, node_size=0, alpha=0.4, edge_color="r", font_size=16, with_labels=True)
ax = plt.gca()
ax.margins(0.08)
plt.show()

Total running time of the script: ( 0 minutes 0.057 seconds)

Gallery generated by Sphinx-Gallery