Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

graph_invites.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from million.model.message import Message
  2. import million.parse.fb_exports as fb
  3. import re
  4. import math
  5. import igraph as ig
  6. DATA_PATH = './data/'
  7. export = fb.parse_dirfiles(DATA_PATH)
  8. def extraire_nom(entree: Message):
  9. if entree.content is None:
  10. return None
  11. regex = r"(.+) a(?:vez)? ajouté (.+)(?: et (.+))? au groupe."
  12. match = re.match(regex, entree.content)
  13. if match is not None:
  14. return (
  15. match.group(1) if match.group(1) != "Vous" else entree.sender_name,
  16. *tuple(match.group(2).split(" et "))
  17. )
  18. else:
  19. return None
  20. invites = []
  21. for message in export.messages:
  22. noms = extraire_nom(message)
  23. if noms is None:
  24. continue
  25. invites.append((message, noms))
  26. print(f"Total invites: {len(invites)}")
  27. edges = []
  28. for invite in invites:
  29. for invitee in invite[1]:
  30. edges.append((invite[0].sender_name, invitee))
  31. vertices_as_names = list(set([edge[0] for edge in edges] + [edge[1] for edge in edges]))
  32. # Create a graph
  33. nb_vertices = len(vertices_as_names)
  34. vertices_as_indexes = {name: i for i, name in enumerate(vertices_as_names)}
  35. edges_as_indexes = [(vertices_as_indexes[edge[0]], vertices_as_indexes[edge[1]]) for edge in edges]
  36. g = ig.Graph(nb_vertices, edges_as_indexes)
  37. g.vs["name"] = vertices_as_names
  38. # Plot the graph
  39. visual_style = {
  40. "vertex_size": 2,
  41. "vertex_label": g.vs["name"],
  42. "vertex_label_size": 10,
  43. "vertex_color": "blue",
  44. "edge_width": 0.5,
  45. "edges_arrow_width": 20,
  46. "layout": g.layout("rt_circular"),
  47. "bbox": (2160, 1920),
  48. "margin": 100,
  49. "vertex_label_angle": math.pi * 1.5,
  50. "vertex_label_dist": 10,
  51. }
  52. ig.plot(g, "output/invite_graph.png", **visual_style)