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]:=
![Click for copyable input](assets.en/compare-the-structure-of-sentences/In_97.png)
graph = TextStructure["Time flies like an arrow.", "ConstituentGraph"]
Out[1]=
![](assets.en/compare-the-structure-of-sentences/O_65.png)
Compute the matrix of distances between all the vertices of this graph.
In[2]:=
![Click for copyable input](assets.en/compare-the-structure-of-sentences/In_98.png)
distancemat1 = GraphDistanceMatrix[First[graph]];
MatrixForm[distancemat1]
Out[2]//MatrixForm=
![](assets.en/compare-the-structure-of-sentences/O_66.png)
Proceed similarly for another sentence.
In[3]:=
![Click for copyable input](assets.en/compare-the-structure-of-sentences/In_99.png)
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]:=
![Click for copyable input](assets.en/compare-the-structure-of-sentences/In_100.png)
distancemat1 == distancemat2
Out[4]=
![](assets.en/compare-the-structure-of-sentences/O_67.png)
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]:=
![Click for copyable input](assets.en/compare-the-structure-of-sentences/In_101.png)
processWikiPage[article_] :=
Select[TextCases[WikipediaData[article], "Sentences"],
WordCount[#] < 5 &];
genStructure[article_] :=
Flatten[TextStructure[#, "ConstituentGraph"] & /@
processWikiPage[article]];
In[6]:=
![Click for copyable input](assets.en/compare-the-structure-of-sentences/In_102.png)
phrasestruct1 = genStructure["Philosophy"];
phrasestruct2 = genStructure["History"];
Compute all distance matrices.
In[7]:=
![Click for copyable input](assets.en/compare-the-structure-of-sentences/In_103.png)
adj1 = GraphDistanceMatrix /@ phrasestruct1;
adj2 = GraphDistanceMatrix /@ phrasestruct2;
Compare the sentences of the different articles two by two.
In[8]:=
![Click for copyable input](assets.en/compare-the-structure-of-sentences/In_104.png)
comparison = Outer[Equal, adj1, adj2, 1];
These are the pairs of sentences with the same structure.
In[9]:=
![Click for copyable input](assets.en/compare-the-structure-of-sentences/In_105.png)
pickedSentences =
Flatten[Pick[Outer[List, phrasestruct1, phrasestruct2], comparison,
True], 1];
This is the first pair.
In[10]:=
![Click for copyable input](assets.en/compare-the-structure-of-sentences/In_106.png)
First[pickedSentences]
Out[10]=
![](assets.en/compare-the-structure-of-sentences/O_68.png)