from itertools import combinations
import re
sentence = sentences[2]
nouns = []
for i, chunk in enumerate(sentence.chunks):
if [morph for morph in chunk.morphs if morph.pos == "名詞"]:
nouns.append(i)
for i, j in combinations(nouns, 2):
path_I = []
path_J = []
while i != j:
if i < j: #文節iの構文木経路上に文節jが存在する場合
path_I.append(i)
i = sentence.chunks[i].dst
else: #文節iの構文木経路上に文節jがない場合
path_J.append(j)
j = sentence.chunks[j].dst
if len(path_J) == 0: # 文節Iの構文木上に文節Jが存在する場合
X = "X" + "".join([morph.surface for morph in sentence.chunks[path_I[0]].morphs if morph.pos != "名詞" and morph.pos != "記号"])
Y = "Y" + "".join([morph.surface for morph in sentence.chunks[i].morphs if morph.pos != "名詞" and morph.pos != "記号"])
chunk_X = re.sub("X+", "X", X)
chunk_Y = re.sub("Y+", "Y", Y)
path_ItoJ = [chunk_X] + ["".join(morph.surface for n in path_I[1:] for morph in sentence.chunks[n].morphs)] + [chunk_Y]
print(" -> ".join(path_ItoJ))
else: # 文節Iの構文木上に文節Jが存在しない場合
X = "X" + "".join([morph.surface for morph in sentence.chunks[path_I[0]].morphs if morph.pos != "名詞" and morph.pos != "記号"])
Y = "Y" + "".join([morph.surface for morph in sentence.chunks[path_J[0]].morphs if morph.pos != "名詞" and morph.pos != "記号"])
chunk_X = re.sub("X+", "X", X)
chunk_Y = re.sub("Y+", "Y", Y)
chunk_k = "".join([morph.surface for morph in sentence.chunks[i].morphs if morph.pos != "記号"])
path_X = [chunk_X] + ["".join(morph.surface for n in path_I[1:] for morph in sentence.chunks[n].morphs if morph.pos != "記号")]
path_Y = [chunk_Y] + ["".join(morph.surface for n in path_J[1: ]for morph in sentence.chunks[n].morphs if morph.pos != "記号")]
print(" | ".join(["->".join(path_X), "->".join(path_Y), chunk_k]))