# Path: deepnlp/ner_tagger.py
# TAG_NONE_ENTITY = "nt"
# ENTITY_TAG_DICT = "entity_tags.dic"
# ENTITY_TAG_DICT_PICKLE = "entity_tags.dic.pkl"
# DEFAULT_DICT_ZH_NAME = "zh"
# def udf_default(word, tags, *args):
# def udf_disambiguation_cooccur(word, tags, context, tag_feat_dict, *args):
# def ensemble_udf(udfs, word, tags, *args):
#     def __init__(self, name, data_path, ckpt_path, conf_path):
#     def predict(self, words, tagset = []):
#     def _init_ner_model(self, session):
#     def _predict_ner_tags_model(self, session, model, words, data_path):
#     def set_tag_feat_dict(self, tag_feat_dict):
#     def _predict_ner_tags_dict(self, words, merge = False, tagset = [],udfs = [udf_default]):
#     def _get_context_words(self, words, i, window = 4):
#     def _preprocess_segment(self, words):
#     def _merge_tagging(self, model_tagging, dict_tagging):
#     def _load_default_dict(self, name = 'zh'):
#     def _load_dict(self, dict_name):
#     def load_dict(self, dict_name):
#     def load_user_dict(self, path):
# def load_model(name = 'zh'):
# class ModelLoader(object):
# 
# Path: deepnlp/ner_tagger.py
# def udf_disambiguation_cooccur(word, tags, context, tag_feat_dict, *args):
#     """ Disambiguation based on cooccurence of context and tag_feat_dict
#         Args: word: input word
#             tags: multiple tags on word
#             context: list of words surrounding current word of a window
#     """
#     if (len(tag_feat_dict) == 0) or (len(tags) == 0):
#         return None, 0.0
# 
#     num = len(tags)
#     coocur_dict = {}
#     coocur_count = []
#     for tag in tags:
#         feat_words = tag_feat_dict[tag] if tag in tag_feat_dict else []
#         common = []
#         for feat in feat_words:
#             if feat in context:
#                 common.append(feat)
#         coocur_dict[tag] = len(common)  # How many occurence under current tags
#         coocur_count.append(len(common))
#     vec = np.array(coocur_count)
#     total = np.sum(vec)
#     prob_vec = []
#     if total > 0.0:
#         prob_vec = vec/total
#     else:
#         prob_vec = 0.0 * vec
#     max_index = np.argmax(prob_vec)
#     return tags[max_index], prob_vec[max_index]
# 
# Path: deepnlp/ner_tagger.py
# def udf_default(word, tags, *args):
#     """ Default get the first tag
#         Return: tag, confidence
#     """
#     if (len(tags) > 0):
#         return tags[0], 1.0
#     else:
#         return TAG_NONE_ENTITY
#     return tags[0], 1.0


import deepnlp
from deepnlp import ner_tagger
from deepnlp.ner_tagger import udf_disambiguation_cooccur
from deepnlp.ner_tagger import udf_default

#北京/city
#望京/area
#最好吃/nt
#的/nt
#黑椒牛排/dish
#在哪里/nt


# Word Sense Disambuguition
# Sometime each word have multiple tags, use can define customized UDF(user_difine_function)
tagger.load_dict("zh_entertainment")                     # Change to other dict
text = "今天 我 看 了 琅 琊 榜"
words = text.split(" ")
tagging = tagger._predict_ner_tags_dict(words, merge = True)
print ("DEBUG: NER tagger zh_entertainment dictionary with 'merge = True'")
for (w,t) in tagging:
    pair = w + "/" + t
    print (pair)

# '琅琊榜' have two category: 'list_name' and 'teleplay'
# Disambiguation

word = "琅琊榜"
tags = ['list_name', 'teleplay']
context = ["今天", "我", "看", "了"]
tag_feat_dict = {}
# Most Freq Word Feature of two tags
tag_feat_dict['list_name'] = ['听', '专辑', '音乐']
tag_feat_dict['teleplay'] = ['看', '电视', '影视']
# Disambuguiation Prob

# Path: deepnlp/ner_tagger.py
# TAG_NONE_ENTITY = "nt"
# ENTITY_TAG_DICT = "entity_tags.dic"
# ENTITY_TAG_DICT_PICKLE = "entity_tags.dic.pkl"
# DEFAULT_DICT_ZH_NAME = "zh"
# def udf_default(word, tags, *args):
# def udf_disambiguation_cooccur(word, tags, context, tag_feat_dict, *args):
# def ensemble_udf(udfs, word, tags, *args):
#     def __init__(self, name, data_path, ckpt_path, conf_path):
#     def predict(self, words, tagset = []):
#     def _init_ner_model(self, session):
#     def _predict_ner_tags_model(self, session, model, words, data_path):
#     def set_tag_feat_dict(self, tag_feat_dict):
#     def _predict_ner_tags_dict(self, words, merge = False, tagset = [],udfs = [udf_default]):
#     def _get_context_words(self, words, i, window = 4):
#     def _preprocess_segment(self, words):
#     def _merge_tagging(self, model_tagging, dict_tagging):
#     def _load_default_dict(self, name = 'zh'):
#     def _load_dict(self, dict_name):
#     def load_dict(self, dict_name):
#     def load_user_dict(self, path):
# def load_model(name = 'zh'):
# class ModelLoader(object):
# 
# Path: deepnlp/ner_tagger.py
# def udf_disambiguation_cooccur(word, tags, context, tag_feat_dict, *args):
#     """ Disambiguation based on cooccurence of context and tag_feat_dict
#         Args: word: input word
#             tags: multiple tags on word
#             context: list of words surrounding current word of a window
#     """
#     if (len(tag_feat_dict) == 0) or (len(tags) == 0):
#         return None, 0.0
# 
#     num = len(tags)
#     coocur_dict = {}
#     coocur_count = []
#     for tag in tags:
#         feat_words = tag_feat_dict[tag] if tag in tag_feat_dict else []
#         common = []
#         for feat in feat_words:
#             if feat in context:
#                 common.append(feat)
#         coocur_dict[tag] = len(common)  # How many occurence under current tags
#         coocur_count.append(len(common))
#     vec = np.array(coocur_count)
#     total = np.sum(vec)
#     prob_vec = []
#     if total > 0.0:
#         prob_vec = vec/total
#     else:
#         prob_vec = 0.0 * vec
#     max_index = np.argmax(prob_vec)
#     return tags[max_index], prob_vec[max_index]
# 
# Path: deepnlp/ner_tagger.py
# def udf_default(word, tags, *args):
#     """ Default get the first tag
#         Return: tag, confidence
#     """
#     if (len(tags) > 0):
#         return tags[0], 1.0
#     else:
#         return TAG_NONE_ENTITY
#     return tags[0], 1.0


# Path: test/test_ner_dict_udf.py
import deepnlp
from deepnlp import ner_tagger
from deepnlp.ner_tagger import udf_disambiguation_cooccur
from deepnlp.ner_tagger import udf_default

#北京/city
#望京/area
#最好吃/nt
#的/nt
#黑椒牛排/dish
#在哪里/nt


# Word Sense Disambuguition
# Sometime each word have multiple tags, use can define customized UDF(user_difine_function)
tagger.load_dict("zh_entertainment")                     # Change to other dict
text = "今天 我 看 了 琅 琊 榜"
words = text.split(" ")
tagging = tagger._predict_ner_tags_dict(words, merge = True)
print ("DEBUG: NER tagger zh_entertainment dictionary with 'merge = True'")
for (w,t) in tagging:
    pair = w + "/" + t
    print (pair)

# '琅琊榜' have two category: 'list_name' and 'teleplay'
# Disambiguation

word = "琅琊榜"
tags = ['list_name', 'teleplay']
context = ["今天", "我", "看", "了"]
tag_feat_dict = {}
# Most Freq Word Feature of two tags
tag_feat_dict['list_name'] = ['听', '专辑', '音乐']
tag_feat_dict['teleplay'] = ['看', '电视', '影视']
# Disambuguiation Prob

tag, prob = udf_disambiguation_cooccur(word, tags, context, tag_feat_dict)
