Beispiel #1
0
 def load_sheet(self, sheet_name):
     if not sheet_name in self.content:
         self.content[sheet_name] = {}
     if not sheet_name in self.excel.sheetnames:
         print_in_red("创建新的sheet[%s]" % sheet_name)
         self.excel.create_sheet(sheet_name)
         table = self.excel[sheet_name]
         table.cell(row=1, column=1).value = "id"
         table.cell(row=1, column=2).value = "word"
         table.cell(row=1, column=3).value = "status"
         table.cell(row=1, column=4).value = "score"
         table.cell(row=1, column=5).value = "latest_update_time"
         self.excel.save(FREQUENCY_PATH)
     else:
         table = self.excel[sheet_name]
         rows = table.max_row
         for row in range(2, rows + 1):
             id = table.cell(row=row, column=1).value
             if not id:
                 continue
             word = table.cell(row=row, column=2).value
             status = table.cell(row=row, column=3).value
             score = table.cell(row=row, column=4).value
             time = str_to_time(table.cell(row=row, column=5).value)
             frequency_word = FrequencyWord(sheet_name, id, word, status, score, time)
             self.add(frequency_word)
Beispiel #2
0
def load() -> WordCollection:
    print_in_red("开始加载数据库!")
    # 解析xlsx
    excel = load_workbook(XL_PATH)
    context = WordCollection(excel)
    for sheet_name in excel.sheetnames:
        # 加载频率统计
        get_frequency_center().load_sheet(sheet_name)
        # 加载信息到内存
        table = excel[sheet_name]
        rows = table.max_row
        for row in range(2, rows + 1):
            id = table.cell(row=row, column=1).value
            if not id:
                # 防止出现空行
                continue
            word = table.cell(row=row, column=2).value
            mean = table.cell(row=row, column=3).value
            nums = float(table.cell(row=row, column=4).value)
            hit_nums = float(table.cell(row=row, column=5).value)
            word = Word(sheet_name, id, word, mean, nums, hit_nums, 0)
            # 计算每个单词的权重
            word.weight = get_weight(word)
            # 加入到上下文
            context.add(word)
    print_in_red("数据库加载完毕!")
    return context
Beispiel #3
0
def get_meaning(word: str) -> str:
    if word:
        url = URL % (word)
        content = get_url_content(url,
                                  constants.get_conf_center().get_proxies())
        if content:
            return __analysis_content(str(content, encoding="utf-8"))
        else:
            utils.print_in_red("网络问题!")
def play(type, word):
    center = get_conf_center()
    if not center.PRONOUNCE_SWITCH == "on":
        return
    if type == MP3_TYPE_EN:
        file_path = os.path.join(EN_PATH, "%s.mp3" % word)
    else:
        file_path = os.path.join(AM_PATH, "%s.mp3" % word)

    # 先判断文件是否存在
    if not os.path.exists(file_path):
        if type == MP3_TYPE_EN:
            download_mp3(EN_URL % word, EN_PATH, "%s.mp3" % word)
        else:
            download_mp3(AM_URL % word, AM_PATH, "%s.mp3" % word)
    try:
        mixer.music.load(file_path)
        mixer.music.play()
    except Exception as a:
        print_in_red(a)
Beispiel #5
0
def get_weight(word: Word) -> float:
    """"
    权重计算公式
    当hit_hums = 0 时
    weight = FORMAL_WEIGHT * word.nums

    当 max_hit_num > hit_hums > 0
    weight = (FORMAL_WEIGHT * word.nums + abs(ORIGIN_WEIGHT * word.hit_nums) * POSITIVE_WEIGHT

    当 min_hit_num < hit_hums < 0 时
    weight = (FORMAL_WEIGHT * word.nums + abs(ORIGIN_WEIGHT * word.hit_nums) * MINUS_WEIGHT

    当 hit_hums = max_hit_num 时
    weight = (FORMAL_WEIGHT * word.nums + abs(ORIGIN_WEIGHT * word.hit_nums) * MAX_POSITIVE_WEIGHT + MAX_POSITIVE_CONSTANT_WEIGHT

    当 hit_hums = min_hit_num 时
    weight = (FORMAL_WEIGHT * word.nums + abs(ORIGIN_WEIGHT * word.hit_nums) * MIN_MINUS_WEIGHT + MIN_MINUS_CONSTANT_WEIGHT
    """
    center = get_conf_center()
    if center.FREQUENCY_STATISTICS and not get_frequency_center().can_use(word.sheet_name, word.id):
        return 0

    # 获取权重值
    if word.hit_nums == center.NO_SHOW_HIT_NUM:
        # 出现概率为0
        return 0
    elif word.hit_nums == 0:
        return center.FORMAL_WEIGHT * word.nums
    elif center.MAX_HIT_NUM > word.hit_nums > 0:
        return (center.FORMAL_WEIGHT * word.nums + abs(center.ORIGIN_WEIGHT * word.hit_nums)) * center.POSITIVE_WEIGHT
    elif center.MIN_HIT_NUM < word.hit_nums < 0:
        return (center.FORMAL_WEIGHT * word.nums + abs(center.ORIGIN_WEIGHT * word.hit_nums)) * center.MINUS_WEIGHT
    elif center.MAX_HIT_NUM <= word.hit_nums:
        return (center.FORMAL_WEIGHT * word.nums + abs(
            center.ORIGIN_WEIGHT * word.hit_nums)) * center.MAX_POSITIVE_WEIGHT + center.MAX_POSITIVE_CONSTANT_WEIGHT
    elif center.MIN_HIT_NUM >= word.hit_nums:
        return (center.FORMAL_WEIGHT * word.nums + abs(
            center.ORIGIN_WEIGHT * word.hit_nums)) * center.MIN_MINUS_WEIGHT + center.MIN_MINUS_CONSTANT_WEIGHT
    else:
        print_in_red("计算权重出现错误!")
        return 0
Beispiel #6
0
 def excel_flush(self):
     print_in_red("缓存开始刷新到磁盘!")
     try:
         # 缓存刷新到硬盘
         for k in self.flush_list:
             table = self.excel[self.sheet_name]
             table.cell(row=k + 1, column=5).value = self.flush_list[k]
         self.excel.save(XL_PATH)
         print_in_red("刷新完毕!")
         self.flush_list = {}
     except Exception as e:
         print_in_red("写入失败,可能是excel被占用!")
Beispiel #7
0
 def excel_flush(self):
     center = get_conf_center()
     if not center.FREQUENCY_STATISTICS:
         return
     try:
         print_in_red("开始刷入频率统计缓存!")
         for k in self.flush_list:
             fword = self.flush_list[k]
             table = self.excel[fword.sheet_name]
             table.cell(row=k + 1, column=1).value = fword.id
             table.cell(row=k + 1, column=2).value = fword.word
             table.cell(row=k + 1, column=3).value = fword.status
             table.cell(row=k + 1, column=4).value = fword.score
             table.cell(row=k + 1, column=5).value = time_to_str(fword.latest_time)
         self.excel.save(FREQUENCY_PATH)
         self.flush_list = {}
         print_in_red("刷新完毕!")
     except Exception as e:
         print_in_red(e)
         print_in_red("写入失败,可能是excel被占用!")
Beispiel #8
0
def load_init():
    print_in_red("加载配置文件!")
    load()
    print_in_red("配置文件加载完毕!")
Beispiel #9
0
def reload():
    global CONF_CENTER
    print_in_red("重新加载配置文件!")
    load()
    print_in_red("配置文件加载完毕!")
Beispiel #10
0
def load():
    global CONF_CENTER
    # 先读取config中的文件
    # 再读取config中的其他文件
    with open(BDC_CONF_PATH, encoding="utf-8") as file:
        print_in_red("加载配置文件%s" % BDC_CONF_PATH)
        lines = file.readlines()
        CONF_CENTER.EX_NAMES[BDC_CONF_PATH] = get_file_md5(BDC_CONF_PATH)
        for line in lines:
            if line:
                line = line.lstrip(" ").rstrip("\n")
            if line and not line.startswith("#"):
                index = line.index("=")
                key = line[0:index].lstrip(" ").rstrip(" ")
                value = line[index +
                             1:].lstrip(" ").rstrip(" ") if line[index +
                                                                 1:] else None
                if hasattr(CONF_CENTER, key):
                    try:
                        if value:
                            if key in ("EX_NAMES", ):
                                for ex in value.split(","):
                                    CONF_CENTER.EX_NAMES[BDC_TEMP_CONF_PATH %
                                                         ex] = None
                            elif key in (
                                    "PROXY_HTTP",
                                    "PROXY_HTTPS",
                                    "PRONOUNCE_SWITCH",
                            ):
                                setattr(CONF_CENTER, key, str(value))
                            elif key in ("FREQUENCY_STATISTICS", ):
                                setattr(CONF_CENTER, key, bool(value))
                            else:
                                setattr(CONF_CENTER, key, float(value))

                    except Exception as e:
                        print_in_red("加载数据失败")
                        print_in_red(e)

    for path in CONF_CENTER.EX_NAMES:
        if path == BDC_CONF_PATH:
            continue
        CONF_CENTER.EX_NAMES[path] = get_file_md5(path)
        print_in_red("加载配置文件%s" % path)
        with open(path, encoding="utf-8") as file:
            lines = file.readlines()
            for line in lines:
                if line:
                    line = line.lstrip(" ").rstrip("\n")
                if line and not line.startswith("#"):
                    index = line.index("=")
                    key = line[0:index].lstrip(" ").rstrip(" ")
                    value = line[index + 1:].lstrip(" ").rstrip(
                        " ") if line[index + 1:] else None
                    if hasattr(CONF_CENTER, key):
                        try:
                            if value:
                                if key in (
                                        "PROXY_HTTP",
                                        "PROXY_HTTPS",
                                        "PRONOUNCE_SWITCH",
                                ):
                                    setattr(CONF_CENTER, key, str(value))
                                elif key in ("FREQUENCY_STATISTICS", ):
                                    setattr(CONF_CENTER, key, bool(value))
                                else:
                                    setattr(CONF_CENTER, key, float(value))

                        except Exception as e:
                            print_in_red("加载数据失败")
                            print_in_red(e)
Beispiel #11
0
def init():
    context = load()
    message = "请输入数字,选择模块\n"
    for i in range(len(context.sheet_names)):
        message += "'%s => %s'" % (str(i), context.sheet_names[i])
    print_in_red(message)
    in_ = input().lower().rstrip(" ").lstrip(" ")
    if not is_number(in_) or int(in_) > len(context.sheet_names) or int(in_) < 0:
        print_in_red("输入有误,程序终止!")
        return
    context.chose_sheet(context.sheet_names[int(in_)])
    # 上一个单词
    pre_word = None
    while True:
        one = context.pick_one()
        question = get_question_and_answer(context, one)
        print_in_wihte(question.message)
        have_tip = False
        while True:
            center = get_conf_center()
            #  判断是否发音,填空题和选择题才发音
            sound_play_before_input(question.type, have_tip, one.word)
            if center.HAD_UPDATE:
                # 重新计算权值
                print_in_red("配置有变动,重新修改权值")
                context.reload_words()
                center.HAD_UPDATE = False
                print_in_red("修改权值完毕!")
            in_ = input().lower().rstrip(" ").lstrip(" ")
            print_in_green("input:[%s]" % in_)
            if in_ == "@":
                print_in_tip(question.get_answer())
                have_tip = True
                pass
            elif in_ == "#":
                sound_play_after_unknow(question.type, have_tip, one.word)
                print_in_red(one.word)
                context.hint_one(one, center.WRONG_SCORE)
                print_in_red("不认识,正确答案[%s],得分[%s]" % (question.get_answer(), center.WRONG_SCORE))
                break
            elif in_ == "$":
                # 后续不再显示此单词
                context.hint_one(one, center.NO_SHOW_HIT_NUM)
                print_in_red("后续不再显示此单词")
                break
            elif in_ == "%":
                # 后续不再显示上一个单词
                if pre_word:
                    context.hint_one(pre_word, center.NO_SHOW_HIT_NUM)
                print_in_red("后续不再显示上一个单词")
                continue
            elif in_ == "&":
                context.excel_flush()
                get_frequency_center().excel_flush()
                return
            elif in_ == "^":
                print_in_red("请输入要翻译的单词!")
                in_ = input().lower().rstrip(" ").lstrip(" ")
                print_in_green("input:[%s]" % in_)
                from src.youdao_dict import get_meaning
                print_in_red(get_meaning(in_))
                continue
            elif question.right(in_):
                if have_tip:
                    score = center.HALF_RIGHT_SCORE
                else:
                    score = center.RIGHT_SCORE
                context.hint_one(one, score)
                print_in_red("输入正确,得分[%s]" % str(score))
                break
            else:
                print_in_red("输入答案[%s]错误,请重新输入!" % in_)
                pass
        pre_word = one
Beispiel #12
0
import os

from src.automatic_update import auto_update
from src.bdc import init
from src.constants import Monitor, load_init
from src.utils import print_in_red

if __name__ == '__main__':
    os.system("")
    print_in_red("START!!")
    # 查看是否有更新
    auto_update()
    # 加载全局配置
    load_init()
    # 初始化监控线程
    monitor = Monitor()
    # 设置为守护线程
    monitor.setDaemon(True)
    monitor.start()
    os.system("")
    # 程序启动
    init()
    print_in_red("END!!")
Beispiel #13
0
def auto_update():
    print_in_red("测试网络是否联通[%s]!" % (CHECK_FILE_REMOTE_PATH,))
    try:
        content = get_url_content(CHECK_FILE_REMOTE_PATH)
    except Exception as e:
        print_in_red("网络测试失败,项目文件不进行更新使用!")
        print_in_red(e)
        return
    print_in_red("网络测试联通!")
    print_in_red("开始检查远程是否有文件更新!")
    content = get_url_content(CHECK_FILE_REMOTE_PATH)
    file_names = content.split("\n")
    for file_name in file_names:
        if file_name:
            split = file_name.split("||")
            name = split[0]
            md5 = split[1]
            local_path = os.path.join(SRC_PATH, name)
            remote_path = REMOTE_PATH + REMOTE_DIR + name
            if os.path.exists(local_path):
                if get_file_md5(local_path) == md5:
                    pass
                else:
                    # md5值与远程不一致则看作有更新
                    print_in_red("[%s]文件有更新,更新本地文件!" % (local_path,))
                    # 获取文件内容
                    url_content = get_url_content(remote_path)
                    # 删除本地文件
                    os.remove(local_path)
                    # 创建新的文件 并更新内容
                    t = open(local_path, "a", encoding="utf-8")
                    t.write(url_content)
                    t.close()
                    print_in_red("[%s]文件更新完毕!" % (local_path,))
            else:
                print_in_red("新增文件[%s],更新本地文件!" % (local_path,))
                # 更新文件
                url_content = get_url_content(remote_path)
                # 创建新的文件 并更新内容
                t = open(local_path, "a", encoding="utf-8")
                t.write(url_content)
                t.close()
                print_in_red("[%s]文件更新完毕!" % (local_path,))
    print_in_red("更新检查完毕!")