def hint_one(self, word: Word, score: int): center = get_conf_center() # 更新频率设置 if score > 0: if score == center.HALF_RIGHT_SCORE: f_score = center.FREQUENCY_HALF_SCORE else: f_score = center.FREQUENCY_SCORE get_frequency_center().update_status(word.sheet_name, word.id, word.word, f_score) words = self.name_content[word.sheet_name] old_hit_nums = word.hit_nums # 重设命中值 word.hit_nums = center.NO_SHOW_HIT_NUM if score == center.NO_SHOW_HIT_NUM else word.hit_nums + score if word.hit_nums == center.NO_SHOW_HIT_NUM: pass elif word.hit_nums > center.MAX_HIT_NUM: word.hit_nums = center.MAX_HIT_NUM elif word.hit_nums < center.MIN_HIT_NUM: word.hit_nums = center.MIN_HIT_NUM if old_hit_nums == word.hit_nums: return new_weight = get_weight(word) if word.weight == new_weight: pass else: words.sum = words.sum - word.weight + new_weight word.weight = new_weight # 刷入缓存重 self.flush_list[word.id] = word.hit_nums if len(self.flush_list) >= center.FLUSH_CACHE_SIZE: self.excel_flush()
def sound_play_before_input(question_type, have_tip, word): center = get_conf_center() if question_type in (Question.TYPE_MEANING, Question.TYPE_OPTION): media_play.play(center.PRONOUNCE_TYPE, word) elif question_type in (Question.TYPE_FILL,) and have_tip: # 默写还有提示才有发音 media_play.play(center.PRONOUNCE_TYPE, word)
def get_question_and_answer(context: WordCollection, word: Word) -> Question: global QUESTION_INDEX QUESTION_INDEX += 1 center = get_conf_center() randomnum = random.randint(0, center.FILL_QUESTION + center.OPTION_QUESTION + center.MEANING_WRITE_QUESTION) message = "" if randomnum <= center.FILL_QUESTION: # 填空题 type = Question.TYPE_FILL message = "请输入(),该单词含义为%s[%s]" % (word.mean, TIP_MESSAGE,) right_answer = str(word.word).lower() elif randomnum <= center.FILL_QUESTION + center.OPTION_QUESTION: # 选择题 type = Question.TYPE_OPTION no_random = context.pick_three_no_weight() no_random.append(None) index = random.randint(0, 3) no_random.insert(index, word) message += "单词[%s]请选择正确的含义[%s]" % (word.word, TIP_MESSAGE) for i in range(4): message += "\n %s:%s" % (OPTION_EMUS[i], no_random[i].mean) right_answer = OPTION_EMUS[index].lower() else: # 默写题目 type = Question.TYPE_MEANING message = "请输入写出[%s]的中文意思[%s]" % (word.word, TIP_MESSAGE,) right_answer = [] for a in word.mean.split(","): right_answer.append(a.rstrip(" ").lstrip(" ")) message = "[NO:%s][分值:%s]%s" % (QUESTION_INDEX, word.hit_nums, message) return Question(type, message, right_answer)
def update_status(self, sheet_name, id, word, score): if not sheet_name in self.content: self.load_sheet(sheet_name) center = get_conf_center() if not center.FREQUENCY_STATISTICS: return if score <= 0: return if not id in self.content[sheet_name]: if score >= center.MAX_FREQUENCY_SCORE: fword = FrequencyWord(sheet_name, id, word, 2, 0, datetime.datetime.now()) else: fword = FrequencyWord(sheet_name, id, word, 1, score, datetime.datetime.now()) self.content[sheet_name][id] = fword else: fword = self.content[sheet_name][id] fword.score += score if fword.status >= MAX_STATUS: pass else: if fword.score >= center.MAX_FREQUENCY_SCORE: fword.score = 0 fword.status += 1 fword.latest_time = datetime.datetime.now() self.flush_list[id] = fword if len(self.flush_list) >= center.MAX_FREQUENCY_FLUSH_SIZE: self.excel_flush()
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 get_url_content(file_url): proxies = {} center = get_conf_center() if center.PROXY_HTTP: proxies["http"] = center.PROXY_HTTP if center.PROXY_HTTPS: proxies["https"] = center.PROXY_HTTPS # 输出内容:user=admin&password=admin header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'} req = requests.get(url=file_url, headers=header_dict, proxies=proxies, verify=False) content = req.content return str(content, encoding='utf-8')
def can_use(self, sheet_name, id) -> bool: center = get_conf_center() if not sheet_name in self.content: self.load_sheet(sheet_name) if not center.FREQUENCY_STATISTICS: return True name_ = self.content[sheet_name] if not id in name_: return True fword = name_[id] if fword == 0: # 为0 一个阶段才开始 return (datetime.datetime.now() - fword.latest_time).days >= STATUS_TO_PERIOD[fword.status] else: return True
def download_mp3(file_url, save_url, file_name): res = utils.get_url_content(file_url, constants.get_conf_center().get_proxies()) if res: # 解码 file_path = os.path.join(save_url, file_name) if os.path.exists(file_path): print("[%s]已经存在" % file_path) return try: with open(file_path, 'wb') as fd: fd.write(res) except Exception as e: print(e) print("[%s]程序错误" % file_path) os.remove(file_path)
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)
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被占用!")
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
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
def sound_play_after_unknow(question_type, have_tip, word): center = get_conf_center() if question_type in (Question.TYPE_FILL,) and not have_tip: # 默写还有提示才有发音 media_play.play(center.PRONOUNCE_TYPE, word) time.sleep(1.5)