-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
34 lines (29 loc) · 1.24 KB
/
Copy pathmain.py
File metadata and controls
34 lines (29 loc) · 1.24 KB
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
import re
import random
library = open(r"./articles.txt","r",encoding='utf-8').read()
lines = open(r"./words.txt","r",encoding='utf-8').readlines()
questionfile = open(r"./questions.txt","r+",encoding='utf-8')
answerlist = []
for eachline in lines:
eachline = "".join(eachline.splitlines(False))
selflist = []
while True:
if eachline.find(',') != -1:
selflist.append(eachline[:eachline.find(',')])
eachline =eachline[eachline.find(',') + 1:]
else:
selflist.append(eachline)
break
sentences = re.findall("[A-Z]{1}[^.]*.",library) #分析所有语句
for i in selflist:
for sentence in sentences:
while True:
qword = "".join(random.sample(selflist,1))
question = re.sub("\w*"+i+"\w*","_____("+qword+")",sentence)
answer = "".join(re.findall("\w*"+i+"\w*",sentence))
if qword != answer:
break
if question != sentence and answer not in answerlist:
print(question,"\n答案:"+answer)
questionfile.write(question+"\n答案:"+answer+"\n")
answerlist.append(answer) #避免出答案相同的题目