Esempio n. 1
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# k49.py
import itertools

from chap5.utils import dependencies

if __name__ == '__main__':
    for chunks in dependencies():
        noun_pairs = list(itertools.combinations([chunk for chunk in chunks if chunk.has_pos("名詞")], 2))

        for i, j in noun_pairs:
            noun_i, noun_j = i.get_morphs_by_pos("名詞"), j.get_morphs_by_pos("名詞")
            surface_i, surface_j = noun_i.surface, noun_j.surface
            noun_i.surface, noun_j.surface = "X", "Y"
            path_i, path_j = i.path_to_root(chunks), j.path_to_root(chunks)

            if j in path_i:
                print(" -> ".join(map(lambda chunk: chunk.surface, path_i[0:path_i.index(j) + 1])))
            else:
                union = sorted(set(path_i) & set(path_j), key=path_i.index)
                if union:
                    k = union[0]
                    output = [
                        " -> ".join(map(lambda chunk: chunk.surface, path_i[0:path_i.index(k)])),
                        " -> ".join(map(lambda chunk: chunk.surface, path_j[0:path_j.index(k)])),
                        k.surface
                    ]
                    print(" | ".join(output))
            noun_i.surface, noun_j.surface = surface_i, surface_j
Esempio n. 2
0
File: k40.py Progetto: Cain96/nlp100
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# k40.py
from chap5.utils import dependencies

if __name__ == '__main__':
    for i, sentence in enumerate(dependencies()):
        if i == 2:
            for chunk in sentence:
                for morph in chunk.morphs:
                    print(morph)
            break
Esempio n. 3
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# k44.py
from chap5.utils import dependencies, graph_from_edges

if __name__ == '__main__':
    N = 4
    for i, chunks in enumerate(dependencies()):
        edges = []
        if i == N:
            for j, chunk in enumerate(chunks):
                if chunk.has_depend:
                    dst_chunk = chunks[chunk.dst]
                    if chunk.surface and dst_chunk.surface:
                        edges.append(((j, chunk.surface), (chunk.dst, dst_chunk.surface)))
            break

    if len(edges) > 0:
        graph = graph_from_edges(edges)
        graph.write_png('../data/result.png')