# 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:= `graph = TextStructure["Time flies like an arrow.", "ConstituentGraph"]`
Out= Compute the matrix of distances between all the vertices of this graph.

In:= ```distancemat1 = GraphDistanceMatrix[First[graph]]; MatrixForm[distancemat1]```
Out//MatrixForm= Proceed similarly for another sentence.

In:= ```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:= `distancemat1 == distancemat2`
Out= 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:= ```processWikiPage[article_] := Select[TextCases[WikipediaData[article], "Sentences"], WordCount[#] < 5 &]; genStructure[article_] := Flatten[TextStructure[#, "ConstituentGraph"] & /@ processWikiPage[article]];```
In:= ```phrasestruct1 = genStructure["Philosophy"]; phrasestruct2 = genStructure["History"];```

Compute all distance matrices.

In:= ```adj1 = GraphDistanceMatrix /@ phrasestruct1; adj2 = GraphDistanceMatrix /@ phrasestruct2;```

Compare the sentences of the different articles two by two.

In:= `comparison = Outer[Equal, adj1, adj2, 1];`

These are the pairs of sentences with the same structure.

In:= ```pickedSentences = Flatten[Pick[Outer[List, phrasestruct1, phrasestruct2], comparison, True], 1];```

This is the first pair.

In:= `First[pickedSentences]`
Out= 