Сравните структуру предложений
Сравнение структуры двух предложений возможно через вычисление и обработку графика их соответствующих элементов.
Изобразите дерево элементов предложения в виде графика.
![Click for copyable input](assets.ru/compare-the-structure-of-sentences/In_97.png)
graph = TextStructure["Time flies like an arrow.", "ConstituentGraph"]
![](assets.ru/compare-the-structure-of-sentences/O_65.png)
Рассчитайте дистанционную матрицу между всеми вершинами этого графика.
![Click for copyable input](assets.ru/compare-the-structure-of-sentences/In_98.png)
distancemat1 = GraphDistanceMatrix[First[graph]];
MatrixForm[distancemat1]
![](assets.ru/compare-the-structure-of-sentences/O_66.png)
Выполните эти же действия для другого предложения.
![Click for copyable input](assets.ru/compare-the-structure-of-sentences/In_99.png)
graph2 = TextStructure["I fly in the sky.", "ConstituentGraph"];
distancemat2 = GraphDistanceMatrix[First[graph2]];
Сравните структуру двух предложений через сравнение дистанционных матриц.
![Click for copyable input](assets.ru/compare-the-structure-of-sentences/In_100.png)
distancemat1 == distancemat2
![](assets.ru/compare-the-structure-of-sentences/O_67.png)
Два предложения имеют одинаковую структуру.
Найдите предложения с идентичной структурой в двух различных статьях Wikipedia. Начните с извлечения предложений с заданной частотой употребления слов и сгенерируйте график элементов для каждого из них.
![Click for copyable input](assets.ru/compare-the-structure-of-sentences/In_101.png)
processWikiPage[article_] :=
Select[TextCases[WikipediaData[article], "Sentences"],
WordCount[#] < 5 &];
genStructure[article_] :=
Flatten[TextStructure[#, "ConstituentGraph"] & /@
processWikiPage[article]];
![Click for copyable input](assets.ru/compare-the-structure-of-sentences/In_102.png)
phrasestruct1 = genStructure["Philosophy"];
phrasestruct2 = genStructure["History"];
Вычислите все дистанционные матрицы.
![Click for copyable input](assets.ru/compare-the-structure-of-sentences/In_103.png)
adj1 = GraphDistanceMatrix /@ phrasestruct1;
adj2 = GraphDistanceMatrix /@ phrasestruct2;
Попарно сравните предложения из различных статей.
![Click for copyable input](assets.ru/compare-the-structure-of-sentences/In_104.png)
comparison = Outer[Equal, adj1, adj2, 1];
Получаем пары предложений с одинаковой структурой
![Click for copyable input](assets.ru/compare-the-structure-of-sentences/In_105.png)
pickedSentences =
Flatten[Pick[Outer[List, phrasestruct1, phrasestruct2], comparison,
True], 1];
Первая пара.
![Click for copyable input](assets.ru/compare-the-structure-of-sentences/In_106.png)
First[pickedSentences]
![](assets.ru/compare-the-structure-of-sentences/O_68.png)