import string
import re
from pypinyin import pinyin, lazy_pinyin, Style
from nltk.corpus import wordnet as wn
def get_all_char_pinyin():
path = '/www/wwwroot/wonder/wonderful/output2.txt'
pinyin_dict = {}
with open('/www/wwwroot/wonder/wonderful/output2.txt', "r", encoding="utf-8") as f:
for line in f.readlines():
ch = line.strip()
ch_pinyin = pinyin(ch, style=Style.TONE3, heteronym=False)
# heteronym 是否启用多音字模式
for p_li in ch_pinyin:
for p in p_li:
if p not in pinyin_dict:
pinyin_dict[p] = [ch]
else:
pinyin_dict[p].append(ch)
return pinyin_dict
pinyin_dict = get_all_char_pinyin()
def getsamesoundword(s):
# 获取同音汉字
similarity_dict = {}
match_char = s
ch_pinyin = pinyin(match_char, style=Style.TONE3, heteronym=False)
res = []
for p_li in ch_pinyin:
for p in p_li:
#print(p,s,'match_char')
if match_char in pinyin_dict[p]:
pinyin_dict[p].remove(match_char)
res.extend(pinyin_dict[p])
return res
def extract_and_replace(s):
# 随机选择2-6个字符的索引范围
start = random.randint(0, len(s))
end = start + random.randint(0, len(s))
# 提取字符串中的子串
substring = s[start:end]
# 用空括号替换子串
s = s[:start] + '(__)' * len(substring) + s[end:]
return [s, substring]
def replace_random_char(s):
# 排除中英文标点符号
exclude = set(string.punctuation + string.digits)
# 从剩余字符中随机选取一个字符
char = random.choice(list(set(s) - exclude))
# 用括号替换原有位置
s = s.replace(char, '()', 1)
# 多个字符
# chars = [random.choice(list(set(s) - exclude)) for _ in range(2)]
#
# # 用括号替换原有位置
#
# s = ''.join(f'({char})' if chars in chars else char for char in s)
#
return [s, char]
def replace_duplicates(lst): # 去除掉重复选项
seen = set()
result = []
for item in lst:
if item not in seen:
seen.add(item)
result.append(item)
else:
result.append('-')
return result
import random
def random_split_string(s):
# 随机选择一个切割点
split_point = random.randint(1, len(s) - 1)
# 切割字符串
first_part = s[:split_point]
second_part = s[split_point:]
return first_part, second_part
def addlist2(s,questionlist):
data2 = []
for w in questionlist:
a1 = re.split(r'[.,、, !?]+', w) # 两个句子 循环一下
for q in a1:
data = []
answerlist = []
word = extract_and_replace(q) # 从句子中抽取扣空 ['嫦娥应悔偷灵()', '药']
# print(q,word,'抽空的句子')
if word[1] == '': # 如果抽到空的 过滤掉
break
if len(word[1]) > s: # 控制题目
break
data.append(word[0])
for w in range(3):
newword = ''
for x in word[1]: # 生成答案的相思答案
list = getsamesoundword(x)
if list:
# print(list,'这是list')
if len(list) > 0:
choose = random.choice(list)
# print(choose,'choose')
else:
choose = list[0]
else:
choose = '无'
newword += str(choose)
answerlist.append(newword)
answerlist.append(word[1])
# print(answerlist,'answerlist')
random.shuffle(answerlist) # 重新排列ANSWERLIST
answerlist = replace_duplicates(answerlist)
index = answerlist.index(word[1]) # 列表中找到某个元素
for qw in answerlist:
data.append(qw)
# 生成正确选项
if index == 0:
asd = 'a'
elif index == 1:
asd = 'b'
elif index == 2:
asd = 'c'
elif index == 3:
asd = 'd'
else:
asd = '无'
data.append(asd)
data.append(word[1])
# print(data,'组的题')
if data:
# print(data,'data')
data2.append(data)
return data2
# with open("new.csv", "a", encoding='utf-8') as csvfile:
# writer = csv.writer(csvfile, lineterminator='\n')
# writer.writerows([data])
def addlist3(questionlist):
data2 = []
for w in questionlist:
a1 = re.split(r'[.,、, !?]+', w) # 两个句子 循环一下
data1 = []
for q in a1:
while True:
answerlist = []
data = []
s1, s2 = random_split_string(q) # 将一个诗句分成两段
#print(s1, s2, 'newword')
index = random.randint(0, len(s1) - 1) # 获取第一段的一个随机汉字
newword = getsamesoundword(s1[index]) # 创造一个同声字
#print(newword)
if len(newword) > 0:
b1 = s1[:index] + newword[0] + s1[index + 1:] # 制造一个被替换同声字的混淆答案
else:
b1='-'
pass
index2 = random.randint(0, len(s2) - 1)
newword2 = getsamesoundword(s2[index2])
if len(newword2) > 0:
b2 = s2[:index2] + newword2[0] + s2[index2 + 1:]
else:
b2 = '-'
pass
# print(newword, 'newword')
if len(newword) > 0 and s1[index] != newword[0]:
answerlist.append(s1)
answerlist.append(b1)
answerlist.append(s2)
answerlist.append(b2)
random.shuffle(answerlist)
data.append('请选择两个能连成正确的诗句')
for x in answerlist:
data.append(x)
index_s1 = data.index(s1)
index_s2 = data.index(s2)
if index_s1 == 1:
asd = 'a'
elif index_s1 == 2:
asd = 'b'
elif index_s1 == 3:
asd = 'c'
elif index_s1 == 4:
asd = 'd'
else:
asd = '无'
if index_s2 == 1:
asd2 = 'a'
elif index_s2 == 2:
asd2 = 'b'
elif index_s2 == 3:
asd2 = 'c'
elif index_s2 == 4:
asd2 = 'd'
else:
asd2 = '无'
data.append(asd)
data.append(asd2)
data.append(s1 + s2)
data2.append(data)
# print(data2,'打印的')
break
return data2
# print(data, 'datalist')
def remove_random_letter(word):
if not word: # 如果单词为空,则返回一个空字符串和 None
return "", None
letter = random.choice(word) # 从单词中随机选择一个字母
new_word = word.replace(letter, "(__)", 1) # 将该字母替换为 "(_)"
return new_word, letter
def split_word_randomly(word): #单词截成两个
if len(word) == 1:
return word, ""
split_point = random.randint(1, len(word) - 1)
part1 = word[:split_point]
part2 = word[split_point:]
return part1, part2
def creatbreakletter(word):#创建一个
questdata = {
'id': 0,
'type': 'checkbox',
'number': 0,
'title': '听单词选择正确的单词',
'question_option': [
{'id': 1, 'name': 'A', 'content': 0, 'active': 0},
{'id': 2, 'name': 'B', 'content': 0, 'active': 0},
{'id': 3, 'name': 'C', 'content': 0, 'active': 0},
{'id': 4, 'name': 'D', 'content': '-', 'active': 0}
],
'answer1':0,
'answer2':0,
}
wordlist = []
part1,part2 = split_word_randomly(word)
print(part1,part2)
fake1 = replace_random_letter(part1)
fake2 = replace_random_letter(part2)
wordlist = [fake1,fake2,part1,part2]
random.shuffle(wordlist)
index_s1 = wordlist.index(part1)
index_s2 = wordlist.index(part2)
if index_s1 == 0:
asd = 'a'
elif index_s1 == 1:
asd = 'b'
elif index_s1 == 2:
asd = 'c'
elif index_s1 == 3:
asd = 'd'
else:
asd2 = '无'
if index_s2 == 0:
asd2 = 'a'
elif index_s2 == 1:
asd2 = 'b'
elif index_s2 == 2:
asd2 = 'c'
elif index_s2 == 3:
asd2 = 'd'
else:
asd2 = '无'
questdata['title'] = '请选择两个单词元素组成单词'
questdata['question_option'][0]['content'] = wordlist[0].lower()
questdata['question_option'][1]['content'] = wordlist[1].lower()
questdata['question_option'][2]['content'] = wordlist[2].lower()
questdata['question_option'][3]['content'] = wordlist[3].lower()
questdata['answer1'] = asd
questdata['answer2'] = asd2
# print(questdata,'打印的混淆项目')
return questdata
def createmissingletter(word):#创建一个
questdata = {
'id': 0,
'type': 'radio',
'number': 0,
'title': '听单词选择正确的单词',
'question_option': [
{'id': 1, 'name': 'A', 'content': 0, 'active': 0},
{'id': 2, 'name': 'B', 'content': 0, 'active': 0},
{'id': 3, 'name': 'C', 'content': 0, 'active': 0},
{'id': 4, 'name': 'D', 'content': '-', 'active': 0}
],
'answer1':0,
}
wordlist = []
new_word,letter = remove_random_letter(word)
print(new_word,letter)
while True:
# 生成包含所有大小写字母的列表
letters = list(string.ascii_letters)
# 随机选择一个字母
random_letter = random.choice(letters)
# 输出随机选择的字母
print("随机给出的字母是:", random_letter)
if random_letter in wordlist or random_letter==word:
pass
else:
wordlist.append(random_letter)
if len(wordlist) > 2:
break
wordlist.append(letter)
random.shuffle(wordlist)
index_s1 = wordlist.index(letter)
# #print(wordlist,index_s1)
if index_s1 == 0:
asd = 'a'
elif index_s1 == 1:
asd = 'b'
elif index_s1 == 2:
asd = 'c'
elif index_s1 == 3:
asd = 'd'
questdata['title'] = '请选择丢失的字母'+new_word
questdata['question_option'][0]['content'] = wordlist[0].lower()
questdata['question_option'][1]['content'] = wordlist[1].lower()
questdata['question_option'][2]['content'] = wordlist[2].lower()
questdata['question_option'][3]['content'] = wordlist[3].lower()
questdata['answer1'] = asd
# print(questdata,'打印的混淆项目')
return questdata
def createmissingword(word):#创建一个
questdata = {
'id': 0,
'type': 'radio',
'number': 0,
'title': '听单词选择正确的单词',
'question_option': [
{'id': 1, 'name': 'A', 'content': 0, 'active': 0},
{'id': 2, 'name': 'B', 'content': 0, 'active': 0},
{'id': 3, 'name': 'C', 'content': 0, 'active': 0},
{'id': 4, 'name': 'D', 'content': '-', 'active': 0}
],
'answer1':0,
}
wordlist = []
while True:
newword = replace_random_letter(word)
if newword in wordlist:
pass
else:
wordlist.append(newword)
if len(wordlist) > 2:
break
wordlist.append(word)
random.shuffle(wordlist)
index_s1 = wordlist.index(word)
#print(wordlist,index_s1)
if index_s1 == 0:
asd = 'a'
elif index_s1 == 1:
asd = 'b'
elif index_s1 == 2:
asd = 'c'
elif index_s1 == 3:
asd = 'd'
questdata['question_option'][0]['content'] = wordlist[0]
questdata['question_option'][1]['content'] = wordlist[1]
questdata['question_option'][2]['content'] = wordlist[2]
questdata['question_option'][3]['content'] = wordlist[3]
questdata['answer1'] = asd
print(questdata,'打印的混淆项目')
return questdata
def addlist(questionlist):
for w in questionlist:
data = []
answerlist = []
# print(replace_random_char(w))
word = replace_random_char(w)
print(word[1] + '的形近字,答案最多3个汉字,用逗号隔开,必须不是词语,不要陈述问题,不要解析,答案之间不能重复')
answer = sparkWeb.chat(word[1] + '的形近字,答案只给我3个字,用逗号隔开,不可以是词语,不要陈述问题,不要解析,答案之间不能重复')
list = re.split(r'[.,、, !?]+', answer)
print(list, '这是list')
# data.append('选择对应的汉字填入古诗' + word[1])
if answer:
for x in list:
answerlist.append(x)
else:
data.append('无答案')
answerlist.append(word[1])
random.shuffle(answerlist) # 重新排列ANSWERLIST
index = answerlist.index(word[1]) # 列表中找到某个元素
# 生成正确选项
if index == 0:
choose = 'a'
if index == 1:
choose = 'b'
if index == 2:
choose = 'c'
if index == 3:
choose = 'd'
data.append('选择对应的汉字填入古诗中,' + word[0])
for x in answerlist:
data.append(x)
data.append(choose)
data.append(word[1])
# with open("new.csv", "a", encoding='utf-8') as csvfile:
# writer = csv.writer(csvfile, lineterminator='\n')
# writer.writerows([data])
def check_elements(lst, element1, element2, element3):
return all(element in lst for element in [element1, element2, element3])
tempdata = []
def checklist2(word): # 查询是否重复
for x in tempdata:
# print(findaldata, word[0], x[0], '对比')
if word[0] == x[0]:
return 1
def checklist(word, list): # 查询是否重复
for x in list:
print(word,x[1],x,'checklist')
if word == x[1]:
return 1
# {
# id:1, //题目id
# type:'radio',//单选 checkbox - 多选 ; write - 填空
# number:1, //题目序号 - 非必要
# title:'生物灭绝又叫生物绝种。历史上一共有几次大灭绝?', //题目名称
# imageList:['https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1588056060&di=67dc5595a44e90101f524bae2273cc0a&src=http://a3.att.hudong.com/14/75/01300000164186121366756803686.jpg'], //图片地址
# question_option:[
# {
# id:1,//答案id
# name:'A',//答案选项名
# content:'一次',//答案内容
# active:0//选中状态
# },
# {id:2,name:'B',content:'二次',active:0},
# {id:3,name:'C',content:'三次',active:0},
# {id:4,name:'D',content:'四次',active:0},
# ]//选项集
# },
def getenglishwordquestions(word):#英语单词的题目生成
finaldata = []
for i in range(6):
item = createmissingword(word)
finaldata.append(item)
#print(finaldata)
for i in range(10):
item1 = createmissingletter(word)
finaldata.append(item1)
for i in range(10):
item2 = creatbreakletter(word)
finaldata.append(item2)
return finaldata
def replace_random_letter(word): #随机替换一个字母
# 随机选择一个字母的索引
index = random.randint(0, len(word) - 1)
# 生成一个包含26个英文字母的列表
letters = list(string.ascii_lowercase)
# 从列表中随机选择一个字母
random_letter = random.choice(letters)
# 替换选定的字母
new_word = word[:index] + random_letter + word[index+1:]
return new_word
def getquestions(qlist,all_added_questions):
questionlist = qlist
findaldata = []
tempquestions = []
id = 0
print( len(all_added_questions),'诗句匹配题目')
for x in all_added_questions:#增加了选择诗句题目
id += 1
x['id']=id
findaldata.append(x)
#print(findaldata,'!!!finaldata')
for i in range(8):
item = addlist3(questionlist)
for x in item:
tempquestions.append(x)
random_elements = random.sample(tempquestions, 10)
for x in random_elements:
print(x)
id += 1
questdata = {
'id': id,
'type': 'checkbox',
'number': id,
'title': x[0],
'question_option': [
{'id': 1, 'name': 'A', 'content': x[1], 'active': 0},
{'id': 2, 'name': 'B', 'content': x[2], 'active': 0},
{'id': 3, 'name': 'C', 'content': x[3], 'active': 0},
{'id': 4, 'name': 'D', 'content': x[4], 'active': 0}
],
'answer1': x[5],
'answer2': x[6],
}
findaldata.append(questdata)
# print(findaldata)
tempdata3 = []
for i in range(30):
new = addlist2(1,questionlist)
if new:
#print(new)
identify = 0
if tempdata3:
for x in tempdata3:
checkitem = check_elements(new[0], '无', '-', '-')
if checkitem:
#print(new[0],'发现空答案')
identify = 1
#print(new[0][0], x['title'], '发现答案出现重复')
if new[0][0] == x['title']:
identify = 1
#print(new[0][0], x['title'], '发现答案出现重复')
if identify == 0:
#print(new[0])
id += 1
questdata2 = {
'id': id,
'type': 'radio',
'number': id,
'title': new[0][0],
'question_option': [
{'id': 1, 'name': 'A', 'content': new[0][1], 'active': 0},
{'id': 2, 'name': 'B', 'content': new[0][2], 'active': 0},
{'id': 3, 'name': 'C', 'content': new[0][3], 'active': 0},
{'id': 4, 'name': 'D', 'content': new[0][4], 'active': 0}
],
'answer1': new[0][5],
'disp': new[0][6],
}
#print(questdata2,'添加的questdata2')
tempdata3.append(questdata2)
random.shuffle(tempdata3)
#print(tempdata3,'tempdata3')
if len(tempdata3) < 10:
random_elements2 = random.sample(tempdata3, len(tempdata3))
for x in random_elements2:
findaldata.append(x)
else:
random_elements2 = random.sample(tempdata3, 10)
for x in random_elements2:
findaldata.append(x)
#print(findaldata)
tempdata2 = []
for i in range(30):
new = addlist2(3,questionlist)
if new:
identify = 0
for x in tempdata2:
if new[0][0] == x['title']:
identify = 1
# print(new[0][0], x[0], '发现重复')
break
if identify == 0:
id += 1
questdata2 = {
'id': id,
'type': 'radio',
'number': id,
'title': new[0][0],
'question_option': [
{'id': 1, 'name': 'A', 'content': new[0][1], 'active': 0},
{'id': 2, 'name': 'B', 'content': new[0][2], 'active': 0},
{'id': 3, 'name': 'C', 'content': new[0][3], 'active': 0},
{'id': 4, 'name': 'D', 'content': new[0][4], 'active': 0}
],
'answer1': new[0][5],
'disp': new[0][6],
}
tempdata2.append(questdata2)
#print(len(tempdata2),'第二梯队题目数量')
if len(tempdata2) < 10: # 有时候抽不到那么多题会报错
random.shuffle(tempdata2)
# print(tempdata2)
random_elements3 = random.sample(tempdata2, len(tempdata2))
for x in random_elements2:
findaldata.append(x)
else:
random.shuffle(tempdata2)
# print(tempdata2)
random_elements3 = random.sample(tempdata2, 10)
for x in random_elements3:
findaldata.append(x)
#print(findaldata)
return findaldata
标签:index,题目,random,print,word,英语,id,append,备注
From: https://blog.51cto.com/u_15813778/8980006