# Path: yaya/const.py
# DOUBLE_MAX = 1.7976931348623157e+308
# 
# Path: yaya/config.py
# DICT_BIN_EXT = '.ya'
# DICT_BIN_REVERSE_EXT = '.reverse.ya'
# DATA_ROOT = "/home/tony/MyProject/YaYaNLP/data"
# CUSTOM_DICT_NAME = [os.path.join(DATA_ROOT, "dictionary", "custom", f) for f in [
#     u"CustomDictionary.txt",
#     u"上海地名.txt",
#     u"人名词典.txt",
#     u"全国地名大全.txt",
#     u"机构名词典.txt",
#     u"现代汉语补充词库.txt"]]
# CORE_DICT_NAME = os.path.join(DATA_ROOT, "dictionary", "CoreNatureDictionary.txt")
# CORE_BIGRAM_NAME = os.path.join(DATA_ROOT, "dictionary", "CoreNatureDictionary.ngram.txt")
# CORE_TR_PATH = os.path.join(DATA_ROOT, "dictionary", "person", "CoreNatureDictionary.tr.txt")
# CHAR_TYPE_PATH = os.path.join(DATA_ROOT, "dictionary", "other", "CharType.dat.yes")
# PERSON_TR_PATH = os.path.join(DATA_ROOT, "dictionary", "person", "nr.tr.txt")
# PERSON_DICT_NAME = os.path.join(DATA_ROOT, "dictionary", "person", "nr.txt")
# ORG_TR_PATH = os.path.join(DATA_ROOT, "dictionary", "organization", "nt.tr.txt")
# ORG_DICT_NAME = os.path.join(DATA_ROOT, "dictionary", "organization", "nt.txt")
# PLACE_TR_PATH = os.path.join(DATA_ROOT, "dictionary", "place", "ns.tr.txt")
# PLACE_DICT_NAME = os.path.join(DATA_ROOT, "dictionary", "place", "ns.txt")
# TRADITIONAL_CHINESE_DICT_NAME = os.path.join(DATA_ROOT, "dictionary", "tc", "TraditionalChinese.txt")
# class _Config:


import math
from yaya.const import DOUBLE_MAX
from yaya.config import Config
            if node.vertex_from is None:
                continue
            for node_to in vertexs[i + len(node.real_word)]:
                node_to.update_from(node)
    vertex_from = vertexs[-1][0]
    vertex_list = []
    while vertex_from is not None:
        vertex_list.insert(0, vertex_from)
        vertex_from = vertex_from.vertex_from
    return vertex_list

def viterbi_roletag(roletaglist, hmm):
    _length = len(roletaglist)
    taglist = []
    # 得到第一个元素的第一个标签的词性
    _pre_nature = roletaglist[0].nature
    _perfect_nature = _pre_nature
    taglist.append(_pre_nature)
    for i in xrange(1, _length):
        perfect_cost = DOUBLE_MAX
        item = roletaglist[i]
        for i, nature, freq in item.natures:
            _now = hmm.trans_prob[_pre_nature.index][nature.index] - math.log((item.get_nature_frequency(nature)+1e-8) / hmm.get_total_freq(nature))
            if perfect_cost > _now:
                perfect_cost = _now
                _perfect_nature = nature
        _pre_nature = _perfect_nature
        taglist.append(_pre_nature)
    return taglist


# Path: yaya/const.py
# DOUBLE_MAX = 1.7976931348623157e+308
# 
# Path: yaya/config.py
# DICT_BIN_EXT = '.ya'
# DICT_BIN_REVERSE_EXT = '.reverse.ya'
# DATA_ROOT = "/home/tony/MyProject/YaYaNLP/data"
# CUSTOM_DICT_NAME = [os.path.join(DATA_ROOT, "dictionary", "custom", f) for f in [
#     u"CustomDictionary.txt",
#     u"上海地名.txt",
#     u"人名词典.txt",
#     u"全国地名大全.txt",
#     u"机构名词典.txt",
#     u"现代汉语补充词库.txt"]]
# CORE_DICT_NAME = os.path.join(DATA_ROOT, "dictionary", "CoreNatureDictionary.txt")
# CORE_BIGRAM_NAME = os.path.join(DATA_ROOT, "dictionary", "CoreNatureDictionary.ngram.txt")
# CORE_TR_PATH = os.path.join(DATA_ROOT, "dictionary", "person", "CoreNatureDictionary.tr.txt")
# CHAR_TYPE_PATH = os.path.join(DATA_ROOT, "dictionary", "other", "CharType.dat.yes")
# PERSON_TR_PATH = os.path.join(DATA_ROOT, "dictionary", "person", "nr.tr.txt")
# PERSON_DICT_NAME = os.path.join(DATA_ROOT, "dictionary", "person", "nr.txt")
# ORG_TR_PATH = os.path.join(DATA_ROOT, "dictionary", "organization", "nt.tr.txt")
# ORG_DICT_NAME = os.path.join(DATA_ROOT, "dictionary", "organization", "nt.txt")
# PLACE_TR_PATH = os.path.join(DATA_ROOT, "dictionary", "place", "ns.tr.txt")
# PLACE_DICT_NAME = os.path.join(DATA_ROOT, "dictionary", "place", "ns.txt")
# TRADITIONAL_CHINESE_DICT_NAME = os.path.join(DATA_ROOT, "dictionary", "tc", "TraditionalChinese.txt")
# class _Config:


# Path: yaya/seg/viterbi.py
import math
from yaya.const import DOUBLE_MAX
from yaya.config import Config

            if node.vertex_from is None:
                continue
            for node_to in vertexs[i + len(node.real_word)]:
                node_to.update_from(node)
    vertex_from = vertexs[-1][0]
    vertex_list = []
    while vertex_from is not None:
        vertex_list.insert(0, vertex_from)
        vertex_from = vertex_from.vertex_from
    return vertex_list

def viterbi_roletag(roletaglist, hmm):
    _length = len(roletaglist)
    taglist = []
    # 得到第一个元素的第一个标签的词性
    _pre_nature = roletaglist[0].nature
    _perfect_nature = _pre_nature
    taglist.append(_pre_nature)
    for i in xrange(1, _length):
        perfect_cost = DOUBLE_MAX
        item = roletaglist[i]
        for i, nature, freq in item.natures:
            _now = hmm.trans_prob[_pre_nature.index][nature.index] - math.log((item.get_nature_frequency(nature)+1e-8) / hmm.get_total_freq(nature))
            if perfect_cost > _now:
                perfect_cost = _now
                _perfect_nature = nature
        _pre_nature = _perfect_nature
        taglist.append(_pre_nature)
    return taglist


def viterbi_template(node_list, hmm, init_cost=DOUBLE_MAX):