# !/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
project_path = os.path.abspath(os.path.join(os.getcwd(), ".."))
sys.path.append(project_path)
import requests, json
from cbot import NLPUtil


class TaskManager(object):

    """
    给模版匹配对应的函数
    """
    @classmethod
    def task_response(cls, task, words, arcs_dict,postags,hed_index):
        answer = ""
        if task == 'task_SBV_ATT_POB':
            answer = cls.answer_SBV_ATT_POB(words, arcs_dict,postags,hed_index)
        if task == 'task_HED':
            answer = cls.answer_HED(words, arcs_dict,postags,hed_index)
        if task == 'task_ATT_HED':
            answer = cls.answer_ATT_HED(words, arcs_dict,postags,hed_index)
        if task == 'task_SBV_VOB':
            answer = cls.answer_SBV_VOB(words, arcs_dict,postags,hed_index)
        if task == 'task_SBV_POB':
            answer = cls.answer_SBV_POB(words, arcs_dict,postags,hed_index)
        if task == 'task_SBV_ATT_VOB':
            answer = cls.answer_SBV_ATT_VOB(words, arcs_dict,postags,hed_index)
        if task == 'task_SBV_ADV':#未实现
            answer = cls.answer_SBV_ADV(words, arcs_dict,postags,hed_index)
        if task == 'task_SBV_VOB_COO':#未实现
            answer = cls.answer_SBV_VOB_COO(words, arcs_dict,postags,hed_index)
        if task == 'task_ATT_SBV_POB':
            answer = cls.answer_ATT_SBV_POB(words, arcs_dict,postags,hed_index)
        if task == 'task_ATT_SBV_VOB':
            answer = cls.answer_ATT_SBV_VOB(words, arcs_dict,postags,hed_index)
        if task == 'task_SBV_HED_SBV_VOB':
            answer = cls.answer_SBV_HED_SBV_VOB(words, arcs_dict, postags, hed_index)
        if task == 'task_ADV_SBV_HED':
            answer = cls.answer_ADV_SBV_HED(words, arcs_dict, postags, hed_index)
        if task == 'task_ADV_HED_DBL_VOB':
            answer = cls.answer_ADV_HED_DBL_VOB(words, arcs_dict, postags, hed_index)
        if task == 'task_ADV_SBV_ADV_HED':
            answer = cls.answer_ADV_SBV_ADV_HED(words, arcs_dict, postags, hed_index)




        return answer

    """
    问句提取出状语（疑问代词），主语以及第二状语（表达状态）和核心词汇（主语的状态）
    eg.会什么恐龙会灭绝
    """
    @classmethod
    def answer_ADV_SBV_ADV_HED(cls, words, arcs_dict, postags, hed_index):
        pass

    """
    问句提取出状语（疑问代词），主语以及核心词汇（动词）和兼语（动词第一对象）宾语（动词第二对象）
    eg.怎么使恐龙灭绝
    """
    @classmethod
    def answer_ADV_HED_DBL_VOB(cls, words, arcs_dict, postags, hed_index):
        pass

    """
    问句提取出状语，主语和核心词汇
    问句的含义是问询主语与核心词汇之间的关系
    eg.为什么恐龙灭绝了
    """
    @classmethod
    def answer_ADV_SBV_HED(cls,words, arcs_dict, postags, hed_index):
        pass

    """
    提取出问句的第一主语和第二主语，以及他们之间的关系（VOB）
    问句的含义是问询第二主语
    eg.红豆是谁唱的
    
    """
    @classmethod
    def answer_SBV_HED_SBV_VOB(cls,words, arcs_dict, postags, hed_index):
        pass

    """
    问句仅提取出代词，主语，动宾
    询问代词的主语属性
    eg.中国的首都是什么
    """
    @classmethod
    def answer_ATT_SBV_VOB(cls, words, arcs_dict, postags, hed_index):
        verb = words[hed_index]
        obj_index = arcs_dict[hed_index]['VOB'][0]
        obj = words[obj_index]
        subj_index = arcs_dict[hed_index]['SBV'][0]
        subj = words[subj_index]
        att_index = arcs_dict[subj_index]['ATT'][0]
        att = words[att_index]

        if postags[att_index] in NLPUtil.noun:

            uri = "https://api.ownthink.com/kg/knowledge?entity=" + att
            r = requests.post(uri)
            if r.json()['message'] == 'success':
                ans_dict = dict(r.json()['data']['avp'])
                keys = ans_dict.keys()
                ans = NLPUtil.get_similarity(subj, keys)
                if ans != None:
                    return ans_dict[ans]
            else:
                return "对不起，我不太明白你在说什么\n"

        else:
            uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj
            r = requests.post(uri)
            if r.json()['message'] == 'success':
                ans = dict(r.json())['data']['desc']
                if ans != None:
                    return ans
            else:
                return "对不起，我不太明白你在说什么\n"


    """
    问句仅提取出代词，主语，介宾
    询问代词的主语属性
    eg.暂时没有想到
    """

    @classmethod
    def answer_ATT_SBV_POB(cls, words, arcs_dict, postags, hed_index):
        prep = words[hed_index]
        obj_index = arcs_dict[hed_index]['POB'][0]
        obj = words[obj_index]
        subj_index = arcs_dict[hed_index]['SBV'][0]
        subj = words[subj_index]
        att_index = arcs_dict[subj_index]['ATT'][0]
        att = words[att_index]

        if postags[att_index] in NLPUtil.noun:

            uri = "https://api.ownthink.com/kg/knowledge?entity=" + att
            r = requests.post(uri)
            if r.json()['message'] == 'success':
                ans_dict = dict(r.json()['data']['avp'])
                keys = ans_dict.keys()
                ans = NLPUtil.get_similarity(subj, keys)
                if ans != None:
                    return ans_dict[ans]
            else:
                return "对不起，我不太明白你在说什么\n"

        else:
            uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj
            r = requests.post(uri)
            if r.json()['message'] == 'success':
                ans = dict(r.json())['data']['desc']
                if ans != None:
                    return ans
            else:
                return "对不起，我不太明白你在说什么\n"

    """
    问句仅提取出核心词汇，以及该核心词的修饰词
    问句的意图:
    1.当ATT为名词性词汇时询问的是att的核心词汇属性
    eg.中国的首都
    2.当ATT非名词性时，询问的是核心词汇的介绍
    eg.美丽的中国
    """

    @classmethod
    def answer_ATT_HED(cls, words, arcs_dict, postags, hed_index):
        word = words[hed_index]
        att_index = arcs_dict[hed_index]['ATT'][0]
        att = words[att_index]
        if postags[att_index] in NLPUtil.noun:
            #print(att,"qqq")

            uri = "https://api.ownthink.com/kg/knowledge?entity=" + att
            r = requests.post(uri)
            if r.json()['message'] == 'success':
                ans_dict = dict(r.json()['data']['avp'])
                keys = ans_dict.keys()
                ans = NLPUtil.get_similarity(word, keys)
                if ans != None:
                    return ans_dict[ans]
            else:
                return "对不起，我不太明白你在说什么\n"

        else:
            uri = "https://api.ownthink.com/kg/knowledge?entity=" + word
            r = requests.post(uri)
            if r.json()['message'] == 'success':
                ans = dict(r.json())['data']['desc']
                if ans != None:
                    return ans
            else:
                return "对不起，我不太明白你在说什么\n"

    """
    问句仅提取出核心词汇
    问句的意图为介绍核心词汇
    eg.中国
    """
    @classmethod
    def answer_HED(cls, words, arcs_dict, postags, hed_index):
        word = words[hed_index]
        uri = "https://api.ownthink.com/kg/knowledge?entity="+word
        r = requests.post(uri)

        if r.json()['message'] == 'success':
            ans = dict(r.json())['data']['desc']
            if ans != None:
                return ans
        else:
            return "对不起，我不太明白你在说什么\n"

    @classmethod
    def answer_SBV_HED_SBV_VOB(cls, words, arcs_dict, postags, hed_index):
        for i in range(len(words)):
            print(words[i],postags[i],arcs_dict[i])


    """
    问句提取出主语、谓语和宾语
    
    1.宾语词性为疑问代词->介绍主语
    eg.iphone是什么
    2.宾语是名词性词汇->确定主语是否有宾语这一属性
    eg.五星红旗是国旗吗
    """
    @classmethod
    def answer_SBV_VOB(cls, words, arcs_dict, postags, hed_index):
        verb = words[hed_index]
        obj_index = arcs_dict[hed_index]['VOB'][0]
        obj = words[obj_index]
        subj_index = arcs_dict[hed_index]['SBV'][0]
        subj = words[subj_index]
        if postags[obj_index] == 'r':
            uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj
            r = requests.post(uri)
            if r.json()['message'] == 'success':
                ans = dict(r.json())['data']['desc']
                if ans != None:
                    return ans
            else:
                return "对不起，我不太明白你在说什么\n"
        elif postags[obj_index] in NLPUtil.noun:
            uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj
            r = requests.post(uri)
            ans_list = list(r.json()['data']['avp'])
            values = [x[1] for x in ans_list]
            ans = NLPUtil.get_similarity(obj,values)
            if ans != None:
                return "是的，"+subj+verb+obj+"\n"
            elif verb=='有':

                return "对不起，"+subj+"没有"+obj+"\n"
            else:
                return "对不起，" + subj + "不"+verb + obj + "\n"
        elif postags[subj_index] == 'r':

            uri = "https://api.ownthink.com/kg/knowledge?entity=" + obj
            r = requests.post(uri)
            if r.json()['message'] == 'success':
                ans = dict(r.json())['data']['desc']
                if ans != None:
                    return ans
            else:
                return "对不起，我不太明白你在说什么\n"

    """
    问句提取出主语、介词和宾语

    1.宾语词性为疑问代词->询问属性
    eg.北京在哪
    2.宾语是名词性词汇->确定主语是否有宾语这一属性
    eg.北京在中国吗

    """

    @classmethod
    def answer_SBV_POB(cls, words, arcs_dict, postags, hed_index):
        prep = words[hed_index]
        obj_index = arcs_dict[hed_index]['POB'][0]
        obj = words[obj_index]
        subj_index = arcs_dict[hed_index]['SBV'][0]
        subj = words[subj_index]
        if postags[obj_index] == 'r':
            uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj
            r = requests.post(uri)
            if r.json()['message'] == 'success':
                ans = dict(r.json())['data']['desc']
                if ans != None:
                    return ans
            else:
                return "对不起，我不太明白你在说什么\n"
        elif postags[obj_index] in NLPUtil.noun and postags[subj_index] != 'r':
            uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj
            r = requests.post(uri)
            ans_list = list(r.json()['data']['avp'])
            values = [x[1] for x in ans_list]
            ans = NLPUtil.get_similarity(obj, values)
            if ans != None:
                return "是的，" + subj + prep + obj + "\n"
            else:
                return "对不起，" + subj + "不" + prep + obj + "\n"
        elif postags[subj_index] == 'r':

            uri = "https://api.ownthink.com/kg/knowledge?entity=" + obj
            r = requests.post(uri)
            if r.json()['message'] == 'success':
                ans = dict(r.json())['data']['desc']
                if ans != None:
                    return ans
            else:
                return "对不起，我不太明白你在说什么\n"


    """
    提取出问句的主语、介词和介词宾语
    问句含义：询问主语的动宾属性
    eg:红豆是谁的歌
    """

    @classmethod
    def answer_SBV_ATT_VOB(cls, words, arcs_dict, postags, hed_index):
        verb = words[hed_index]
        obj_index = arcs_dict[hed_index]['VOB'][0]
        obj = words[obj_index]
        subj_index = arcs_dict[hed_index]['SBV'][0]
        subj = words[subj_index]
        att_index = arcs_dict[obj_index]['ATT'][0]
        att = words[att_index]

        if postags[att_index] == 'r':
            #红豆是谁的歌
            uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj
            r = requests.post(uri)

            if r.json()['message'] == 'success':
                ans_dict = dict(r.json()['data']['avp'])
                keys = ans_dict.keys()
                ans = NLPUtil.get_similarity(obj, keys)
                if ans != None:
                    return ans_dict[ans]
            else:
                return "对不起，我不太明白你在说什么\n"
        elif postags[att_index] in NLPUtil.noun:
            #五星红旗是中华人民共和国国旗吗
            uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj
            r = requests.post(uri)

            if r.json()['message'] == 'success':
                ans_list = list(r.json()['data']['avp'])
                values = [x[1] for x in ans_list]
                ans = NLPUtil.get_similarity(att,values)
                if ans != None:
                    return "是的，"+subj+verb+att+obj+"\n"
                elif verb == '有':
                    return "很抱歉，" + subj + "没" + verb + att + obj + "\n"
                else:
                    return "很抱歉，" + subj + "不" + verb + att + obj + "\n"

    """
    提取出问句的主语、介词和介词宾语
    问句含义：询问主语的介宾属性
    """

    @classmethod
    def answer_SBV_ATT_POB(cls, words, arcs_dict, postags, hed_index):
        pron = words[hed_index]
        obj_index = arcs_dict[hed_index]['POB'][0]
        obj = words[obj_index]
        subj_index = arcs_dict[hed_index]['SBV'][0]
        subj = words[subj_index]
        att_index = arcs_dict[obj_index]['ATT'][0]
        att = words[att_index]
        if postags[att_index] == 'r':

            uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj
            r = requests.post(uri)

            if r.json()['message'] == 'success':
                ans_dict = dict(r.json()['data']['avp'])
                keys = ans_dict.keys()
                ans = NLPUtil.get_similarity(obj, keys)
                if ans != None:
                    return ans_dict[ans]
            else:
                return "对不起，我不太明白你在说什么\n"
        elif postags[att_index] in NLPUtil.noun:
            uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj
            r = requests.post(uri)

            if r.json()['message'] == 'success':
                ans_list= list(r.json()['data']['avp'])
                values = [x[1] for x in ans_list]
                ans = NLPUtil.get_similarity(att,values)
                if ans != None:
                    return "是的，" + subj + pron + att + obj + "\n"
                else:
                    return "很抱歉，" + subj + "不" + pron + att + obj + "\n"

















    @classmethod
    def answer_SBV_ATT_POB2(cls, words, arcs_dict,postags,hed_index):
        flag = False
        att_index = 0
        subject = ""
        attribution = ""
        for sub_arcs in arcs_dict:
            keys = sub_arcs.keys()
            #print(keys)
            if 'ATT' in keys:
                for att in range(len(sub_arcs['ATT'])):
                    #print("sub_arcs['ATT'][att]",sub_arcs['ATT'][att])
                    if postags[sub_arcs['ATT'][att]] == 'r':
                        flag = True
                        att_index = sub_arcs['ATT'][att]
                        break
                if flag:
                    break
            #print(att_index)
            if flag:
                break
        for sub_arcs in arcs_dict:
            keys = sub_arcs.keys()
            if 'SBV' in keys:
                sbv_list = sub_arcs['SBV']
                #print("sbv_list",sbv_list)
                for sub_sbv in sbv_list:

                    if sub_sbv < att_index:
                        subject = words[sub_sbv]
                        break
            #print("subject",subject)
            if 'POB' in keys:
                pob_list = sub_arcs['POB']
                for sub_pob in pob_list:
                    if sub_pob > att_index:
                        attribution = words[sub_pob]
                        break
            #print(attribution)

        if flag:
            uri = "https://api.ownthink.com/kg/knowledge?entity="+subject
            r = requests.post(uri)
            #print(r.json())
            if r.json()['message'] == 'success':
                ans_dict = dict(r.json()['data']['avp'])
                keys = ans_dict.keys()
                #print(keys)
                ans = NLPUtil.get_similarity(attribution,keys)
                if ans != None:
                    #print(ans,ans_dict[ans])
                    return ans_dict[ans]
            else:
                return "对不起，我不太明白你在说什么\n"











