As an English teacher, I often referred to the idea of sentence complexity. I urged students to write more complex sentences, as opposed to long run-ons. I pointed out that Hemingway tended to compose simpler sentences while Hawthorne wrote with more complex structures. But I had no precise way to express what I meant by complexity, to measure it, to answer the student who asks “What do you mean?” Here is a computational approach to clarify that idea.

Visualizing a Sentence

As a Graph

In[]:=
txt="The built-in function TextStructure can turn a sentence into a graph of its structure.";​​graph=TextStructure[txt,"ConstituentGraphs"][[1]]
Out[]=
If need be, we can also extract a tree from the graph:

As a Tree

In[]:=
tree=GraphTree[graph,"Sentence1"]
Out[]=

Measuring Complexity

Complexity as Vertex Count

Now that we have a visual representation of the structure, the question remains how we should quantify its complexity. Looking at the graph and tree, a few approaches come to mind.
​
First, more complex sentences will tend to be longer, giving wider graphs. That can be measured by the word count of the sentence, but that seems a bit too simple. A more complex sentence will have more vertices in its graph. Let’s start there:
In[]:=
vert=VertexCount[graph]
Out[]=
40

Complexity as Tree Depth

Okay, nice start; but it doesn’t seem to capture all of what I mean by complexity. Examining the graph above, I see that this example sentence has a noun phrase inside a prepositional phrase inside another noun phrase, etc. The constituents are nested. I could measure that with the tree’s maximum depth:
In[]:=
depth=TreeDepth[tree]
Out[]=
8

Complexity as Graph Diameter

Now we’ve got two different measures that might answer the question, but I think we’re still missing part of what I mean by complexity of the structure of a sentence. To me, a sentence that has a deep nesting in both the subject and the predicate is more complex than one that only has a deep nesting in one part. I need a metric that measures the maximum distance from one node to another. That metric is called a graph’s diameter:
In[]:=
diam=GraphDiameter[graph]
Out[]=
11

Complexity as Vertex Degree

Some of the vertices have more children than others. Looking more closely, it appears to me that this is an indication of complexity as well. A sentence that has many of these highly connected vertices is more complex. The measures we have so far do not take this into consideration. Let’s add it:
In[]:=
vDeg=Mean[VertexDegree[graph]]
Out[]=
39
20

Complexity as Mean Distance

I was almost ready to say that’s enough when one more thought came to mind. The diameter measures the longest path in the graph, but what about a sentence that is dense with deeply nested parts? For that we need the average distance between vertices:
In[]:=
dist=Mean[Mean[GraphDistanceMatrix[graph]]]
Out[]=
4251
800

From 5 to 1

The Goal

Right now we have five numbers that don’t fit together. In the case of the example sentence, {40, 8, 11, 39/20, 4251/800}. Not very useful as is. I envision all these numbers contributing equally to a single metric that I can think of as measuring the sentence’s complexity. Something like this:
Out[]=

Scaling Each Metric

For the numbers to be meaningful relative to each other, they have to be scaled to fall within the same range. Three ranges that come to mind are 0 to 1 (a common scale in Wolfram computations), 1 to 10 (a common scale in casual rankings), and 0 to 100 (the percentage scale common in school, science, etc.). The scale choice is arbitrary, but I want the number to feel comfortable and usable. I think I’ll go with 1 to 10 with 1 being the simplest possible sentence. The upper bound will have to be an approximation because there is no limit to how complex a sentence one may write.
​
Having decided on a scale, I now need to determine how the simplest possible sentence would score if I didn’t rescale the numbers. The simplest sentence in English (if we exclude the imperative mood) is a two-word subject-verb combination, like “He ran.”
Five Metrics for “He ran.”:
In[]:=
sentenceComplexity[s_String]:=Module[{depth,diam,dist,graph,tree,vert,vDeg},​​graph=TextStructure[s,"ConstituentGraphs"][[1]];​​tree=GraphTree[graph,"Sentence1"];​​vert=VertexCount[graph];​​depth=TreeDepth[tree];​​diam=GraphDiameter[graph];​​vDeg=Mean[VertexDegree[graph]];​​dist=Mean[Mean[GraphDistanceMatrix[graph]]];​​{vert,depth,diam,vDeg,dist}​​];​​​​sentenceComplexity["He ran."]
Out[]=
9,3,6,
16
9
,
68
27

For the upper end of the scale I’ll use the exceptionally complex first sentence of Charles Dickens’ A Tale of Two Cities:
Five Metrics for “It was the best of times...”:
In[]:=
dickens="It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way – in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.";​​sentenceComplexity[dickens]
Scaling Each Metric:

Combining the Five Metrics

Forming One Metric from Five:

Testing

Let’s test the function against various sentences. I’ll use initial sentences from famous fiction in the order that I think rises in complexity.
Samples of Increasing Complexity:
These results are reasonable. Samples 4 - 6 have about the same word count, yet the function still can tell which has more depth and density of grammatical structure. The second and third samples show that conjoined structures like “dark and silent” raise the score more than one might expect. Perhaps this is something to improve in the future.

Detailed Feedback

I was just about to write a conclusion for this post when I realized that the student who asked “What do you mean?” hasn’t gotten an answer yet. The answer so far is “Your sentence has a complexity of 3.11702 out of 10.” To give the student a bit more of an answer, the function can return an association with all the individual measures spelled out.
Detailed Output:
Student-Friendly Feedback

Conclusion

Sentence complexity is one of those things that writers can sense intuitively but can’t really nail down objectively. It includes the length of a sentence, of course, but it also involves how deeply the grammatical structures are nested, how complex the component structures are themselves, and how often complex structures appear in the sentence. This function measures five different factors in a sentence’s grammatical structure and combines them into one number for sentence complexity.
​
I intend to submit a polished version of this function soon to the Wolfram Function Respository. My thoughts are to include an option to output just the overall complexity number or the entire report. Writing this post has also got me thinking about other ways to measure language. For instance, could I measure the balance between sentence length and depth? What about measurements that include the semantic qualities of words? Language is an underexplored part of “computational x” — lots of open space and terra incognita.

Extras

You know how some movies show bloopers or additional scenes after the credits, a bonus for those who actually sat and watched all the scrolling names? Well, here is my bonus: The banner images I generated for this article with Dall e 3 directly via the Wolfram Language:
I think it is amusing that, though the prompt said to include the word “sentence,” none of the images actually did.

CITE THIS NOTEBOOK

Measuring sentence complexity via network properties of the text structure tree​
by Mark Greenberg​
Wolfram Community, STAFF PICKS, December 18, 2023
​https://community.wolfram.com/groups/-/m/t/3086185