def create_new_vocab_group(group_path, group_number): pyvoc.stop_loading_animation() cprint("creating ", color="cyan", attrs=["bold"], end="") cprint("vocabulary group number {}...".format(group_number), color="green") with open(group_path, "w") as f: json.dump({}, f)
def list_all_groups(): # reading user groups name and size usergroups_path = os.path.join(config_dir_path(), "usergroups.json") with open(usergroups_path, "r") as f: user_group_numbers = json.load(f) default_group_numbers = {101: 800, 102: 800, 103: 800} pyvoc.stop_loading_animation() # print user groups cprint("\nUSER GROUPS", color="cyan", on_color="on_grey") cprint("Group no.", color="green", end=" " * (14 - len("Group no"))) cprint("No. of words") for group in user_group_numbers: cprint(group, color="green", end=" " * (15 - len(str(group)))) cprint(str(user_group_numbers[group])) # print default groups cprint("\nDEFAULT GROUP", color="cyan", on_color="on_grey") cprint("Group no.", color="green", end=" " * (14 - len("Group no"))) cprint("No. of words") for group in default_group_numbers: cprint(group, color="green", end=" " * (15 - len(str(group)))) cprint(str(default_group_numbers[group])) exit()
def check_group_path(group_path, group_number): if not os.path.isfile(group_path): pyvoc.stop_loading_animation() cprint( "group number {} does not exist".format(group_number), color="red", attrs=["bold"], ) exit()
def start_quiz(group_number, no_of_questions): print("") if no_of_questions is None: cprint( "Number of questions not specified. Assuming 5 questions.", color="yellow", attrs=["bold"], ) no_of_questions = 5 else: cprint(f"Initializing quiz with {no_of_questions} questions.") path = validate_group_number(group_number) group_path = os.path.join(config_dir_path(), "group" + str(group_number) + ".json") options_path = os.path.join(config_dir_path(), "options.json") check_group_path(group_path, group_number) if path == "custom group": count_words_in_custom_group(group_path, no_of_questions, group_number) else: count_words_in_group(path, group_number, no_of_questions) result = {} word_definition = {} with open(group_path, "r") as f: group_content = json.load(f) word_list = random.sample(list(group_content), no_of_questions) for word in word_list: _ = group_content[word] refined_def = _[random.sample(list(_), 1)[0]] word_definition[word] = refined_def with open(options_path, "r") as f: options = json.load(f) pyvoc.stop_loading_animation() cprint( "1 point for every correct answer. q<enter> to exit", color="yellow", attrs=["bold"], ) print("\n") score = 0 for i in range(no_of_questions): cprint(word_list[i], color="white", attrs=["bold", "reverse"]) correct_option_number = print_options(options, word_definition, word_list[i]) prompt_input(correct_option_number, word_list[i], score, result, i + 1) for word in result: if result[word] is True: score += 1 print("") cprint("Score: {}/{}".format(score, no_of_questions), color="green", attrs=["bold"]) if score == no_of_questions: cprint("Perfect Score ヽ(´▽`)/", color="yellow", attrs=["bold", "blink"]) exit()
def revise_vocab(group_number): print("") group_path = os.path.join(config_dir_path(), "group" + str(group_number) + ".json") try: with open(os.path.join(group_path), "r") as f: group = json.load(f) except FileNotFoundError: pyvoc.stop_loading_animation() cprint( "group number {} does not exists".format(group_number), color="red", attrs=["bold"], ) exit() words = list(group) random.shuffle(words) pyvoc.stop_loading_animation() print("") cprint(" Press <enter> for next. q<enter> to exit ", "yellow", attrs=["bold"]) print("") for i, word in enumerate(words, 1): cprint( "{}".format(word), color="green", attrs=["reverse", "bold"], end=" " * (15 - len(word)), ) width_left = terminal_width - 24 sentences = textwrap.wrap(list(group[word].values())[0], width=width_left) s_count = 1 for sentence in sentences: if s_count == 1: print(sentence) else: print(" " * (15) + sentence) s_count += 1 print("{}. ".format(i), end="") prompt = input("> ") if prompt.lower() == "q": cprint("END\n", color="yellow", attrs=["bold", "reverse"]) break print(" ") cprint( "Revised all the words in vocabulary group {}!\n".format(group_number), color="yellow", attrs=["bold", "reverse"], ) exit()
def count_words_in_group(path, group_number, no_of_questions): with open(path, "r") as f: num_of_words_in_group = json.load(f)[str(group_number)] if num_of_words_in_group < no_of_questions: pyvoc.stop_loading_animation() cprint( "group number {} does not have enough words".format( group_number), color="red", attrs=["bold"], ) exit()
def count_words_in_custom_group(group_path, no_of_questions, group_number): with open(group_path, "r") as f: content = json.load(f) no_of_words_in_group = len(list(content.keys())) if no_of_questions > no_of_words_in_group: pyvoc.stop_loading_animation() cprint( "group number {} does not have enough words".format(group_number), color="red", attrs=["bold"], ) exit()
def validate_group_number(group_number): if group_number in [101, 102, 103]: path = os.path.join(config_dir_path(), "group101.json") if not os.path.isfile(path): pyvoc.stop_loading_animation() cprint("group{} does not exist".format(101), color="red", attrs=["bold"]) exit() return "custom group" if group_number < 1 or group_number > 100: pyvoc.stop_loading_animation() cprint("Invalid group number. choose from 1-100", color="red", attrs=["bold"]) exit() if group_number < 51: path = os.path.join(config_dir_path(), "usergroups.json") else: path = os.path.join(config_dir_path(), "defaultgroups.json") return path
def add_word_to_vocab(word, parsed_response, group_number=None): check_config_dir() config_path = config_dir_path() definition = {word: parsed_response} if not group_number: group_number = select_user_group() group_path = os.path.join(config_dir_path(), "group" + str(group_number) + ".json") if not os.path.isfile(group_path): create_new_vocab_group(group_path, group_number) check_if_group_full(group_number) pyvoc.stop_loading_animation() cprint("\nwriting to vocabulary group...", color="yellow") with open(group_path, "r") as f: content = json.load(f) content.update(definition) with open(group_path, "w") as f: json.dump(content, f, ensure_ascii=False) # increase the count of word entries in the group by 1 counter_increment(group_number) # add word (not definition) to all_words.json with open(os.path.join(config_path, "all_words.json"), "r") as f: all_words = json.load(f) all_words.update({word: True}) with open(os.path.join(config_path, "all_words.json"), "w") as f: json.dump(all_words, f) cprint("word added to ", color="green", end="") cprint("group number {}".format(group_number), color="cyan", attrs=["bold"])