Function Repository Resource:

CharacterInteractionGraph

Source Notebook

Create a graph representing the interactions among characters in a story

Contributed by: Jofre Espigule-Pons

ResourceFunction["CharacterInteractionGraph"][story]

returns an undirected graph representing the interactions among characters in a story.

Details and Options

ResourceFunction["CharacterInteractionGraph"] relies on a large language model (LLM) and requires an API key.
You can check whether an API key is set by evaluating SystemCredential["your keyname"]. If needed, the key can be set using SystemCredential["your keyname"]="your-key-here".
The story can be a string or "Book" or "Movie" entities. It can be just the title of a well-known story or alternatively, it can be text summarizing or explaining the plot of the a story.
The story should not exceed the maximum number of tokens, which will vary depending on the LLM used.
There is no guarantee of the accuracy of the results. The LLM might hallucinate or forget about certain characters and/or interactions among them. If the story was not part of the training data of the LLM, provide a detailed plot of the story as text.
The vertex sizes depend on the logarithmic scale of the vertice's degrees relative to the max vertex degree.
ResourceFunction["CharacterInteractionGraph"] supports Graph options. The default ImageSize is 600 points unless otherwise specified.
ResourceFunction["CharacterInteractionGraph"] supports the option LLMEvaluator and supports the same LLM models as LLMConfiguration.
ResourceFunction["CharacterInteractionGraph"] uses the default temperature of LLMFunction, zero. Sampling temperature and other sampling properties can be specified using LLMEvaluator.

Examples

Basic Examples (6) 

Create a character interaction graph for the novel 'Dracula':

In[1]:=
ResourceFunction["CharacterInteractionGraph"]["Dracula"]
Out[1]=

Create a character interaction graph for a recent film that might not be in the training data of the LLM using WikipediaData:

In[2]:=
text = First@
   StringCases[WikipediaData["Oppenheimer_(film)"], "== Plot ==" ~~ plot__ ~~ "== Cast ==" :> plot];
In[3]:=
ResourceFunction["CharacterInteractionGraph"][text]
Out[3]=

Change the base style and edge style of the graph:

In[4]:=
ResourceFunction["CharacterInteractionGraph"]["Lord of the Rings", BaseStyle -> Darker@Green, EdgeStyle -> Dashed]
Out[4]=

Compare graphs generated using different LLMs (gpt-3.5-turbo - vs - gpt-4):

In[5]:=
ResourceFunction[
 "CharacterInteractionGraph"]["Guillermo del Toro's Pinocchio"]
Out[5]=
In[6]:=
ResourceFunction[
 "CharacterInteractionGraph"]["Guillermo del Toro's Pinocchio", LLMEvaluator -> {"Model" -> {"OpenAI", "GPT-4"}}]
Out[6]=

Generate a different graph by modifying the sampling temperature:

In[7]:=
ResourceFunction[
 "CharacterInteractionGraph"]["Guillermo del Toro's Pinocchio", LLMEvaluator -> {"Model" -> {"OpenAI", "GPT-4"}, "Temperature" -> 0.5}]
Out[7]=

Create a graph for a random "Movie" entity:

In[8]:=
RandomEntity["Movie"]
Out[8]=
In[9]:=
ResourceFunction["CharacterInteractionGraph"][
 Entity["Movie", "Cremaster3::68wv2"]]
Out[9]=

Neat Examples (2) 

Create a CommunityGraphPlot directly from CharacterInteractionGraph:

In[10]:=
CommunityGraphPlot@
 ResourceFunction["CharacterInteractionGraph"][
  "Mowgli: Legend of the Jungle"]
Out[10]=

Some "Movie" entities have information about the list of characters (roles):

In[11]:=
ResourceFunction["CharacterInteractionGraph"][
 ToString[
  Values[Entity["Movie", "CharlieAndTheChocolateFactory::x66ds"][
    "CastAndRoles"]]]]
Out[11]=

Publisher

Jofre Espigule-Pons

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 22 March 2024

Related Resources

License Information