Skip to content

Tips and Tricks

Temporary storage of complicated textsearches or graphs

In the research process it can make sense to temporarily store text searches or the creation of complicated graphs. This way, one can focus on the creation of images, without having to repeat the search process. The graph data and the search terms have to stay the same for this to work.

This can be done in a few lines:

  1. Make sure that there is a graph file at the specified path. In this case, this is a .gexf file, which makes sense for the process.
  2. If there is no graph file at the path, create a graph with all its data and write it to the graph file at the specified path.
  3. If there is a graph file at the path, read in the file.
from pathlib import Path
import networkx as nx

pth = Path("./my_graph.gexf")
if pth.exists():
    graph = nx.read_gexf(pth)
else:
    graph = import_your_very_important_graph("snapshot_1234.csv")
    txts = TextSearch("my_slug", token=ENV["MY_TOKEN"], timeout=280)
    txts.search(graph, vocab, summarize=True)
    nx.write_gexf(graph, pth)

The two functions nx.read_gexf() and nx.write_gexf() are central. They read / write the file with the path pth. If pth already exists, the file is read in, otherwise the graph is created.

If you want to force a new calculation, you should manually delete the graph file from the folder (at the path pth).