File size: 2,537 Bytes
691ae91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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