# -*- coding: utf-8 -*-
from tools.new_mysql import MySQLUploader
import sys
import os

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

m = MySQLUploader()
s = "select Word,InflectedWordSpelling,Properties from dictionary_exchange"
r = m.query_data(s)


all_exchange_words = set()
all_exchange_words_dict = {}
all_prototype_deformation_dict = {}
prototype_deformation_dict2 = {}

for i in r:
   
    prototype,deformation,properties= [i[0],i[1],i[2]]
   
    all_exchange_words.update({prototype,deformation})

    if properties == "原型":
        prototype_deformation_dict2[prototype] = deformation

   
    if deformation not in all_prototype_deformation_dict:
        all_prototype_deformation_dict[deformation] = prototype

    if prototype not in all_exchange_words_dict:
        all_exchange_words_dict[prototype] = [deformation]
    if deformation not in all_exchange_words_dict[prototype]:
        all_exchange_words_dict[prototype].append(deformation)



def word_to_prototype(word:str) -> str:
    """依次按顺序查询。1.先查原型 2.最后小写再查变形对应的原型 3.再查变形对应的原型。这样才能保证，不过滤有特殊意义的大写"""
    if word in all_exchange_words_dict:
        return word
    elif word.lower() in all_exchange_words_dict:
        return word.lower()
   
    elif word in all_prototype_deformation_dict:
        w = all_prototype_deformation_dict[word]
        if w in prototype_deformation_dict2:
            w = prototype_deformation_dict2[w]
        return w

    else: 
        return word


def get_word_exchange_list(word) -> list:
    prototype_word = word_to_prototype(word)
    all_exchange_words_list = all_exchange_words_dict.get(prototype_word,[])
    return all_exchange_words_list


if __name__ == '__main__':
    print(word_to_prototype("was"))
    print(word_to_prototype("made"))