import random
import string
from sparkdesk_web.core import SparkWeb
from pyhandytools.file import FileUtils
import csv
import random
import time
from nltk.corpus import wordnet as wn
def generate_synonyms(word):
synsets = wn.synsets(word)
synonyms = [synset.name() for synset in synsets]
return synonyms
if __name__ == '__main__':
conf_data = FileUtils.load_json('./conf/keys.json')
sparkWeb = SparkWeb(
cookie=conf_data['Cookie'],
fd=conf_data['fd'],
GtToken=conf_data['GtToken']
)
#single chat
wordlist = [ ]
quote = '用'
questionlist =[
'白日依山尽,黄河入海流',
'飞流直下三千尺,疑是银河落九天'
]
#'给我三个嫦的形近字,答案给我三个字用逗号隔开,不要陈述问题,不要解析,答案之间不能重复'
# '云母屏风烛影深,长河渐落晓星沉',
# '嫦娥应悔偷灵药,碧海青天夜夜心'
import random
import string
import re
from pypinyin import pinyin, lazy_pinyin, Style
import os
import pickle
def get_all_char_pinyin():
path = "output2.txt"
pinyin_dict = {}
with open(path, "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)
if match_char in pinyin_dict[p]:
pinyin_dict[p].remove(match_char)
res.extend(pinyin_dict[p])
#print(res)
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):
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(s):
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])#创造一个同声字
if len(newword) >0:
b1 = s1[:index] + newword[0] + s1[index + 1:]#制造一个被替换同声字的混淆答案
else:
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:
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:
asd2 = '无'
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 addlist():
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])
tempdata=[]
data=[]
findaldata = []
def checklist2(word): #查询是否重复
for x in tempdata:
#print(findaldata, word[0], x[0], '对比')
if word[0] == x[0]:
return 1
def checklist(word): #查询是否重复
for x in findaldata:
if word == x[1]:
return 1
def getquestions():
for i in range(2):
item = addlist3(1)
for x in item:
if checklist(x) !=1:
findaldata.append(x)
# print(findaldata)
while True:
new = addlist2(1)
if new:
if checklist2(new[0]) == 1:
#print('pass addlist(1)')
pass
else:
#print(new,'new',len(tempdata))
#print(new[0], 'new')
tempdata.append(new[0])
#print(tempdata,'tempdata')
if len(tempdata) > 11:
random_elements = random.sample(tempdata, 8)
#print(random_elements,'random_elements')
break
for x in random_elements:
findaldata.append(x)
while True:
new = addlist2(3)
#print(new)
if new:
if checklist2(new[0]) != 1:
pass
else:
findaldata.append(new)
if len(findaldata) > 28:
break
print(findaldata,len(findaldata))
python 题目处理题目处理
精选 原创
©著作权归作者所有:来自51CTO博客作者小胖子就是我的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:正则表达式 2学习记录与代码
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章