# Wolfram Mathematica

## Compare the Structure of Sentences

The structure of two sentences can be compared by computing and processing their respective constituent graphs.

Display the constituent tree of a sentence as a graph.

In[1]:=
graph = TextStructure["Time flies like an arrow.", "ConstituentGraph"]
Out[1]=

Compute the matrix of distances between all the vertices of this graph.

In[2]:=
distancemat1 = GraphDistanceMatrix[First[graph]]; MatrixForm[distancemat1]
Out[2]//MatrixForm=

Proceed similarly for another sentence.

In[3]:=
graph2 = TextStructure["I fly in the sky.", "ConstituentGraph"]; distancemat2 = GraphDistanceMatrix[First[graph2]];

Compare the structure of the two sentences by comparing their distance matrices.

In[4]:=
distancemat1 == distancemat2
Out[4]=

The two sentences have the same structure.

Find sentences with identical structures in two different Wikipedia articles. First, extract sentences with a given word count and generate the constituent graph for each of them.

In[5]:=
processWikiPage[article_] := Select[TextCases[WikipediaData[article], "Sentences"], WordCount[#] < 5 &]; genStructure[article_] := Flatten[TextStructure[#, "ConstituentGraph"] & /@ processWikiPage[article]];
In[6]:=
phrasestruct1 = genStructure["Philosophy"]; phrasestruct2 = genStructure["History"];

Compute all distance matrices.

In[7]:=

Compare the sentences of the different articles two by two.

In[8]:=