Using regular expressions to process question banks and the use of Flask+sqllite open source examination system

Source code download: https://download.csdn.net/download/wjcroom/86308452
Recently, it took two or three days to deploy an exam practice system. The reason is that there is a WORD document question bank with more than 1,000 questions and answers. Randomly take out a part of it for practice.
I think the general steps are

  • Convert WORD into a standardized table format for import
  • Find an exam system that can import question banks
  • Randomly select a part of it to form a test paper for practice
  • Judge right or wrong, prompt the correct answer, it is best to form a wrong question book for the next review

1. Standardize the word to form a question bank

The form of word is

12. Topic content (AB),
A. Option 1 B. Option 2
C. Option 3 D. Option 4

Copy the block content of word to editplus. This is the tool I am used to. Any tool that supports regular expressions is fine. Word itself is also fine, but I am not used to it. Line up the options ABC and separate them with TAB, which can be copied to excel.

  1. Copy it to editplus, cancel all \t because there are many redundant tab characters, please clear it first.
  2. Look for AD plus . There is a newline before the beginning, and remove the newline. And add TAB in front, that is, \t
    search:
[\n]*([A-E]\.)
//\n换行*多次 \. 是,的转义符

result:

12. Topic content (AB), A. Option 1 B. Option 2 C. Option 3 D. Department 4

Replace with: \t\1
3. Take out the answer, display it as empty brackets, and put the answer in a new column.
Find:

(\n[0-9]+.*?)([A-E]+)


Explanation: Start with a newline and several numbers, followed by any character, and end with multiple or one AE letter. Here, because (there are uncertain spaces behind, so it is not used as a feature extraction. This results in no uppercase letters in the title. OK, the two modes of 1 and 2 replace its complete set with: \2\t\1 and the answer
comes
out Yes, the single brackets remain.

   改进1
   (\n[0-9]+.*?[ ]*   )([A-E]+)
   替换为 \n\2\t\1
   将换行为开始,一个单括号(这里是中文的英文的需要\转义)加若干空格做为第一个模式匹配。将后续的字母组合取出来。颠倒位置为新的顺序

However, it ignores the first line, and the space is allowed by editplus, it is better to use \s under the regular regularization,
so

改进2
^([0-9]+.*?(\s*)([A-E]+)
首尾合适中间任意.*?,分为两组颠倒顺序,这样题干里的答案就被提取出来了。

result:

AB 12. Topic content (), A. Option 1 B. Option 2 C. Option 3 D. Department 4

  1. Convert the formatted question bank into excel to eliminate errors and adjust it as a trial version, and save it as a txt file separated by tabs and newlines. sigle.txt,muti.txt

2. Build an examination system

download source code

https://github.com/hsian/flask-exampy
Deploy according to the instructions
Tip, this initialization code is not perfect, you need to generate the administrator, permission table, role, user, and briefly build the user.

python  manager.py shell

Using the above command, you can call the User class method in main model.py, User.insert_admin() and Role class method, Role.insert_role() to confirm the code of the IP field and the corresponding IP part in main view.py.

However, it is relatively simple and easy to understand. The disadvantage is that the old version is relatively low, but the degree of completion is very good.

First build the platform and test run

Install a sqllite editing tool to understand a whole code. Create corresponding users. Otherwise, you will not be able to see the menu. The role table needs to be added, and the user needs to be added. The user needs to be assigned the admin permission role. To understand a little bit.

to customize

  • Read out 50 questions from the txt text to form a new test paper, and the main process of generating a new subject from the question bank file
    maxperiod= db.session.query(func.max(Select.period)).scalar() or 0
    with open('./sigle.txt','r',encoding='utf-8') as sigles:
      lns=sigles.readlines()
      for ln in random.sample(lns[1:],50):
        
        #ln=lns[rd].strip().split('\t')这里lns[rd]是资源下载中的纰漏,应该只是ln
        ln=ln.strip().split('\t')
        s=Select()
        s.title=re.sub('[\d+、..•]', '', ln[0])
        s.option="###".join(ln[1:5]+[''])
        s.answer=ln[5]
        s.period=maxperiod+1
        
        print(s.option)
        db.session.add(s)
  • Return wrong title from submit page processing
@login_required
def figure_score(id,username):
    data =  json.loads(request.form.get('list'))

    if username == current_user.username:
        exist = Score.query.filter_by(period=id,u_id=current_user.id).first()
        if exist is None:
            score = 0
            wrong={
    
    }
            selects = Select.query.filter_by(period=id)

            for n in selects:
                print(data[str(n.id)]+n.answer)
                if data[str(n.id)] == n.answer:
                    score +=[1,2] [len(n.answer)>=1] #多选2,单选1
                else:
                   wrong[str(n.id)]=n.answer

            o_score = Score(u_id=current_user.id,
                username=current_user.username,
                score=score,
                period=id)
           # db.session.add(o_score)
            db.session.commit()
           
            return  json.dumps({
    
    'score':score,'wrong':wrong})  
        else:
            return "不能重复提交"
    else:
        return "用户信息错误"
  • Display wrong answers in template files
			$.ajax({
    
    
				url : "/figure/{
    
    {period}}/{
    
    {current_user.username}}",
				type : "POST",
				data : data,
				success : function(res){
    
    
					if(res != "不能重复提交"){
    
    
						res=JSON.parse(res)
						score = "你答了" + res.score + "分";
					}
                    document.rsd=res
					$("#release").attr("disabled", true)
					$("#result").html(score);
                    $.each($(".answer span"),function(i,e){
    
    
			            right=res.wrong[$(e).attr("data-id")]
						if (right)
			            {
    
    
			            
						 $(e).html( $(e).html()+"<font color='red'>答案:"+ right+"</font>");
						}
						else 
						  1
						 // {
    
    $(e).parent().parent().css("display","none")}

			        })
					//alert(res.wrong);
				}
			})

The above customization is over.
insert image description here
All the above codes are placed in my download, please download and try it if you need it. There are many instructions in it.

Guess you like

Origin blog.csdn.net/wjcroom/article/details/126148144