# 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/collection/dict.py
# class CoreDict:
#     def __init__(self):
#         self.trie = DoubleArrayTrie.load(config.CORE_DICT_NAME)
# 
# Path: yaya/const.py
# DOUBLE_MAX = 1.7976931348623157e+308
# TAG_PLACE = u"未##地"
# TAG_BIGIN = u"始##始"
# TAG_OTHER = u"未##它"
# TAG_GROUP = u"未##团"
# TAG_NUMBER = u"未##数"
# TAG_QUANTIFIER = u"未##量"
# TAG_PROPER = u"未##专"
# TAG_TIME = u"未##时"
# TAG_CLUSTER = u"未##串"
# TAG_END = u"末##末"
# TAG_PEOPLE = u"未##人"
# MAX_FREQUENCY = 25146057
# SMOOTHING_FACTOR = 1.0 / MAX_FREQUENCY + 0.00001
# SMOOTHING_PARAM = 0.1
# 
# Path: yaya/utility/singleton.py
# def singleton(class_):
#     instances = {}
# 
#     def get_instance(*args, **kwargs):
#         if class_ not in instances:
#             instances[class_] = class_(*args, **kwargs)
#         return instances[class_]
# 
#     return get_instance


import time
        import os
            import cPickle as Pickle
        import cPickle as Pickle
        import codecs
from yaya import config
from yaya.collection.dict import CoreDict
from yaya.const import logger
from yaya.utility.singleton import singleton
    def __init__(self):
        self.start = []
        self.pair = []

    def get_bifreq(self, pre_word, next_word):
        pre_word_id = pre_word if type(pre_word) is int else CoreDict().trie.exact_match_search(pre_word)
        if pre_word_id == -1:
            return 0
        next_word_id = next_word if type(next_word) is int else CoreDict().trie.exact_match_search(next_word)
        if next_word_id == -1:
            return 0
        index = binary_search(self.pair, self.start[pre_word_id],
                              self.start[pre_word_id + 1] - self.start[pre_word_id],
                              next_word_id)
        if index < 0:
            return 0
        index <<= 1
        return self.pair[index + 1]

    @staticmethod
    def load(filename=config.CORE_BIGRAM_NAME):
        start = time.time()
        logger.info(u"开始加载核心二元语法词表")
        if os.path.exists(filename + config.DICT_BIN_EXT):
            return BiGramTable.load_bin(filename + config.DICT_BIN_EXT)
        else:
            table = BiGramTable.build(filename)
            with open(filename + config.DICT_BIN_EXT, 'w') as f:
                Pickle.dump(table, f)
            return table

# 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/collection/dict.py
# class CoreDict:
#     def __init__(self):
#         self.trie = DoubleArrayTrie.load(config.CORE_DICT_NAME)
# 
# Path: yaya/const.py
# DOUBLE_MAX = 1.7976931348623157e+308
# TAG_PLACE = u"未##地"
# TAG_BIGIN = u"始##始"
# TAG_OTHER = u"未##它"
# TAG_GROUP = u"未##团"
# TAG_NUMBER = u"未##数"
# TAG_QUANTIFIER = u"未##量"
# TAG_PROPER = u"未##专"
# TAG_TIME = u"未##时"
# TAG_CLUSTER = u"未##串"
# TAG_END = u"末##末"
# TAG_PEOPLE = u"未##人"
# MAX_FREQUENCY = 25146057
# SMOOTHING_FACTOR = 1.0 / MAX_FREQUENCY + 0.00001
# SMOOTHING_PARAM = 0.1
# 
# Path: yaya/utility/singleton.py
# def singleton(class_):
#     instances = {}
# 
#     def get_instance(*args, **kwargs):
#         if class_ not in instances:
#             instances[class_] = class_(*args, **kwargs)
#         return instances[class_]
# 
#     return get_instance


# Path: yaya/collection/bigram.py
import time
        import os
            import cPickle as Pickle
        import cPickle as Pickle
        import codecs
from yaya import config
from yaya.collection.dict import CoreDict
from yaya.const import logger
from yaya.utility.singleton import singleton

    def __init__(self):
        self.start = []
        self.pair = []

    def get_bifreq(self, pre_word, next_word):
        pre_word_id = pre_word if type(pre_word) is int else CoreDict().trie.exact_match_search(pre_word)
        if pre_word_id == -1:
            return 0
        next_word_id = next_word if type(next_word) is int else CoreDict().trie.exact_match_search(next_word)
        if next_word_id == -1:
            return 0
        index = binary_search(self.pair, self.start[pre_word_id],
                              self.start[pre_word_id + 1] - self.start[pre_word_id],
                              next_word_id)
        if index < 0:
            return 0
        index <<= 1
        return self.pair[index + 1]

    @staticmethod
    def load(filename=config.CORE_BIGRAM_NAME):
        start = time.time()
        logger.info(u"开始加载核心二元语法词表")
        if os.path.exists(filename + config.DICT_BIN_EXT):
            return BiGramTable.load_bin(filename + config.DICT_BIN_EXT)
        else:
            table = BiGramTable.build(filename)
            with open(filename + config.DICT_BIN_EXT, 'w') as f:
                Pickle.dump(table, f)
            return table

        logger.info(u"加载核心二元语法词表完毕，耗时%s", time.time() - start)