Wolfram Research

Function Repository Resource:

KeywordsGraph (1.0.0) current version: 2.0.0 »

Source Notebook

A weighted graph connecting frequently used keywords of a text that are sequential neighbors and thus visualizing the flow and clustering of ideas in the text

Contributed by: Vitaliy Kaurov


finds a given number of most used words in text (keywords) and builds a graph with such keywords as vertices where any two vertices are connected by an edge if one of the keywords follows the other directly in text.

ResourceFunction["KeywordsGraph"][text, number, blist]

builds a graph with blacklisted strings blist removed from the text.

Details and Options

The function returns a Graph expression.
All options of Graph can be applied. The only additional option to those of Graph are "StopWords" and "LowerCase".
Default setting "StopWords"True automatically applies DeleteStopwords and hence no stop words can appear as keywords. Use "StopWords"False to keep the stop words.
Default setting "LowerCase"True automatically applies ToLowerCase to remove unwanted capitalization (for example, at the beginning of sentences) that might lead to incorrect graphs. Use "LowerCase"False to keep the capital letters in text, for example, to distinguish some abbreviations.
VertexWeight is set for every vertex to the number of times the corresponding keyword is encountered in text.
EdgeWeight is set for every edge to the number of times an edge connection is made. Among other applications, this also help to build more meaningful CommunityGraphPlot as some of its methods take EdgeWeight in account.
By default an undirected Graph is returned. Use DirectedEdgesTrue to get a directed graph that shows the sequential order in text of connected keywords.
By default VertexLabelsAutomatic to show keywords on the graph. Use option VertexLabelsNone to remove them.
Large texts require longer time to compute.


Basic Examples (3) 

Consider an English tongue twister:

text = "Betty Botter bought some butter
But she said the butter\[CloseCurlyQuote]s bitter
If I put it in my batter, it will make my batter bitter
But a bit of better butter will make my batter better
So \[OpenCurlyQuote]twas better Betty Botter bought a bit of better butter";

Find the nine most frequently used words (not counting stop words) and see which words are directly next to each other in the text:

ResourceFunction["KeywordsGraph"][text, 9]

You can also find the order in which words follow each other:

ResourceFunction["KeywordsGraph"][text, 9, DirectedEdges -> True]

Scope (5) 

Get the text of the book Alice In Wonderland and build a keywords graph for the top eleven keywords:

text = ExampleData[{"Text", "AliceInWonderland"}];
ResourceFunction["KeywordsGraph"][text, 11]

Exclude the unwanted words by forming a blacklist. You can also apply any option of Graph. For instance, you can restyle your graph and resize vertices in accordance with their properties:

blist = {"came", "said", "like", "just", "went"};
g = ResourceFunction["KeywordsGraph"][text, 11, blist, VertexSize -> "VertexWeight", GraphStyle -> "Prototype"]

Because KeywordsGraph yields a Graph expression, you can apply any functions to it that you can apply to a Graph. For instance, you can find clustering by displaying community structure (note, because edges are weighted they might influence how the clustering is computed):


VertexWeight and EdgeWeight are set to the numbers of times keywords and their next-neighbor pairs are met in a text:

PropertyValue[g, {VertexWeight, EdgeWeight}]

The order of numbers of VertexWeight corresponds to the order of VertexList:

  VertexList[g] -> PropertyValue[g, VertexWeight]],
 PlotTheme -> "Detailed", ScalingFunctions -> "Log"]

Options (6) 

LowerCase (3) 

Consider a text where capitalization matters. For instance, here "us" and "US" are different terms:

text = "A few of us have recently immigrated to the US and found spouses here. 
To us the US seems a gret place to raise a family.";

By default ToLowerCase is applied and "us" is not distinguished from "US":

blist = {"of", "a", "the", "to", "and"};
ResourceFunction["KeywordsGraph"][text, 6, blist, "StopWords" -> False, DirectedEdges -> True]

Use option "LowerCase"False to distinguish capitalized cases:

ResourceFunction["KeywordsGraph"][text, 6, blist, "LowerCase" -> False, "StopWords" -> False, DirectedEdges -> True]

StopWords (3) 

Sometimes you might need to keep some stop words. For example, consider "us" and "US" here:

text = "A few of us have recently immigrated to the US and found spouses here. 
To us the US seems a great place to raise a family.";

By default "us" and "US" will be removed by DeleteStopwords:

ResourceFunction["KeywordsGraph"][text, 6]

Use option "StopWords"False to retain some stopwords and make your own blacklist of words to remove:

blist = {"of", "a", "the", "to", "and"};
ResourceFunction["KeywordsGraph"][text, 6, blist, "StopWords" -> False]

Applications (4) 

Get the dataset for presidential inaugural addresses from the Wolfram Data Repository and order it by time:

inaugural = SortBy[ResourceData["Presidential Inaugural Addresses"], "Date"];

Extract the text of inaugural addresses for the two last presidents as of 2019 - Barack Obama and Donald Trump:

obama = inaugural[-2]["Text"];
trump = inaugural[-1]["Text"];

Define graph styles:

styles = {
   VertexLabelStyle -> Directive[GrayLevel[.8], 14],
   EdgeStyle -> Opacity[.5],
   VertexSize -> "VertexWeight",
   VertexStyle -> Opacity[.7],
   GraphStyle -> "Prototype",
   Background -> Black};

Build KeywordsGraph for Barack Obama and Donald Trump using 30 keywords. You can get the notion of key ideas without actually reading the texts:

 ResourceFunction["KeywordsGraph"][obama, 30, {}, styles],
 PlotLabel -> Style["Barack Obama", 30, Lighter@Blue, FontFamily -> "Phosphate"],
 CommunityBoundaryStyle -> Directive[Yellow, Dashed, Opacity[.5]]]
 ResourceFunction["KeywordsGraph"][trump, 30, {}, styles],
 PlotLabel -> Style["Donald Trump", 30, Lighter@Red, FontFamily -> "Phosphate"],
 CommunityBoundaryStyle -> Directive[Yellow, Dashed, Opacity[.5]]]

Possible Issues (1) 

The second argument (number of keywords in graph) should not exceed the total number of keywords in the text:

text = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?
He would chuck, he would, as much as he could, and chuck as much wood,
as a woodchuck would if a woodchuck could chuck wood.";
ResourceFunction["KeywordsGraph"][text, 6, VertexLabels -> Automatic]

Neat Examples (3) 

Get the dataset for presidential inaugural addresses from the Wolfram Data Repository and order it by time:

inaugural = SortBy[ResourceData["Presidential Inaugural Addresses"], "Date"];
allTEXT = Normal[inaugural[All, "Text"]];
allNAME = Normal[inaugural[All, DateString[#Date, "Year"] <> " " <> CommonName[#Name] &]];

Define graph styles:

styles = {
   VertexLabels -> None,
   EdgeStyle -> Opacity[.5],
   VertexSize -> "VertexWeight",
   VertexStyle -> Opacity[.7],
   GraphStyle -> "Prototype",
   Background -> Black};

Build a keywords graph for each address using 30 keywords and arrange them in a grid:

allGRAPH = MapThread[
     ResourceFunction["KeywordsGraph"][#1, 30, {}, styles],
     PlotLabel -> Style[#2, White, 13],
     CommunityBoundaryStyle -> Directive[Yellow, Dashed, Opacity[.5]]] &,
   {allTEXT, allNAME}];
Grid[Partition[allGRAPH, 4], Spacings -> {0, 0}, Background -> Black]

Version History

  • 2.0.0 – 06 September 2022
  • 1.0.0 – 03 October 2019

Related Resources

License Information