Spaces:
Runtime error
Runtime error
import math | |
import pathlib | |
import datetime | |
from pathlib import Path | |
from config import * | |
from question import * | |
def txt_to_list(txt): | |
while txt[0] == '\n': | |
txt = txt[1:] | |
while txt[-1] == '\n': | |
txt = txt[:-1] | |
txt = txt.split('\n') | |
txt = [i for i in txt if i is not None] | |
return txt | |
def generate_scope(progress: str): | |
""" | |
:param progress: | |
:return: | |
""" | |
if progress == '期中': | |
progress = ['unit1', 'unit2', 'unit3', 'unit4', 'unit5', 'unit6', 'unit7'] | |
elif progress == '期末': | |
progress = ['unit1', 'unit2', 'unit3', 'unit4', 'unit5', 'unit6', 'unit7', 'unit8', 'unit9', | |
'unit10', 'unit11', 'unit12', 'unit13', 'unit14'] | |
else: | |
progress = [progress] | |
scope = {'word': [], 'phrase': [], 'sentence': [], 'grammar': []} | |
root_path = Path(__file__).parent | |
for i in progress: | |
path = root_path.joinpath('material', i + '.txt') | |
with open(path, 'r', encoding='utf-8') as file: | |
content = file.read() | |
# scope = re.split(r'<word>|<phrase>|<sentence>|<grammar>', content) | |
_, word, phrase, sentence, grammar = re.split(r'<word>|<phrase>|<sentence>|<grammar>', content) | |
scope['word'].extend(txt_to_list(word)) | |
scope['phrase'].extend(txt_to_list(phrase)) | |
scope['sentence'].extend(txt_to_list(sentence)) | |
scope['grammar'].extend(txt_to_list(grammar)) | |
return scope | |
def generate(progress: str, question_config: dict): | |
full_scope = generate_scope(progress) | |
# doc = docx.Document() | |
answer = '' | |
for key, value in question_config.items(): | |
if (value is not None) & (value != 0): | |
scope = full_scope[generate_command_to_material_type[key]] | |
if len(scope) < value: | |
add_scope = scope * math.ceil(value / len(scope) - 1) | |
random.shuffle(add_scope) | |
scope = scope.extend(add_scope[:(value-len(scope))]) | |
else: | |
random.shuffle(scope) | |
scope = scope[: value] | |
rst = eval(generate_command_to_function_type[key])(scope) | |
# print(rst) | |
answer = answer + generate_command_to_question_type[key] + ':\n\n' | |
for i in range(len(rst)): | |
if rst[i]['question'] != 'fail!': | |
answer = answer + str(i+1) + '.' + rst[i]['question'] + '\n' + '正确答案:' + rst[i]['answer'] + '\n\n' | |
answer = answer + '\n\n' | |
return answer | |