from pyknp import Juman,Jumanpp,KNP

"""
Explanation: Library Exploration: KNP
Load Juman and KNP
End of explanation
"""


# Create instance of Juman
juman = Juman()

str = "巨大なマーライオンがセントーサ島にいた"
result = juman.analysis(str)

for mp in result.mrph_list():
    print("見出し:{}, 読み:{}, 原形:{}, 品詞:{}, 品詞細分類:{}, 活用型:{}, 活用形:{}, 意味情報:{}, 代表表記:{}".format(
    mp.midasi, mp.yomi, mp.genkei, mp.hinsi, mp.bunrui, mp.katuyou1, mp.katuyou2, mp.imis, mp.repname))

import pandas as pd

mps = [(mp.midasi, mp.yomi, mp.genkei, mp.hinsi, mp.bunrui, mp.katuyou1, mp.katuyou2, mp.imis, mp.repname) for mp in result.mrph_list()]
clmns = ('見出し','読み', '原形', '品詞', '品詞細分類', '活用型', '活用形', '意味情報', '代表表記') 
pd.DataFrame(mps,columns=clmns)

"""
Explanation: Morphological analysis using Juman
End of explanation
"""


# Create instance of KNP
knp = KNP(option = '-tab -anaphora')

# Generates phrase objects
result = knp.parse(str)
ph_objs = result.bnst_list()

# Create phrase dictionary
ph_dic = {}
for ph_obj in ph_objs:
    phrase_list = []
    for mrph in ph_obj._mrph_list._mrph:
        phrase_list.append(mrph.midasi)
    ph_dic[ph_obj.bnst_id] = {"phrase_list":phrase_list, "phrase":''.join(phrase_list), "parent_id": ph_obj.parent_id}

# Show dependency of phrases
for p in ph_dic.items():
    if p[1]['parent_id'] > 0:
        print(p[1]['phrase'], '=>', ph_dic[p[1]['parent_id']]['phrase'])

# Show dependency of phrases (list representation)
for p in ph_dic.items():
    if p[1]['parent_id'] > 0:
        print(p[1]['phrase_list'], '=>', ph_dic[p[1]['parent_id']]['phrase_list'])

"""
Explanation: Dependency analysis using KNP
End of explanation
"""


