[Python] 纯文本查看 复制代码
""""
@说明:首发52pojie
"""
import random as r
words = dict()
a, b, c = "", "", ""
# 新建函数 get_inputs(),获取输入数据
def get_inputs():
global a, b, c
print("========================请选择功能========================")
a = input("请选择功能编号:[0.查询,1.默写]:")
if a not in ("0", "1"):
a = "0"
if a == "0":
b = input("请选择查询结果保存模式:[0.数据模式,1.背诵模式]")
if b not in ("0", "1"):
b = "0"
else:
pass
else:
b = input("请输入默写单词的个数:")
if not b.isnumeric():
b = "0"
c = input("请输入默写时,单词中已知字符的个数:")
if not c.isnumeric():
c = "0"
# 新建函数 txt_to_dic(),将txt文件转换为字典
def txt_to_dic(filename):
dic = dict()
with open(filename, "rt", encoding="utf-8-sig") as f:
lines = f.readlines()
for line in lines:
ls = line.strip("\n").split("\t")
dic[ls[0]] = ls[1:]
return dic
# 新建函数 words_get(),批量获取单词
def words_get(filename):
global words
words = txt_to_dic(filename)
words_get_dic = dict()
print("============批量获取单词(用于:输出查询结果/从中抽样默写)============")
# 输入查询条件
print("提示:按Enter可以略过条件。")
word = input("(1)、请输入单个单词文本:")
ch_count = input("(2)、请输入单个单词最低长度:")
grade = input("(3)、请输入学习阶段编号[0.小学,1.初中,2.高中]:")
publish = input("(4)、请输入教材版本编号[0.人教版,1.译林版]:")
error_count = input("(5)、请输入单词最低出错次数:")
# 对查询条件进行加工赋值
if word.strip() == "":
word = "NULL"
if not ch_count.strip().isnumeric():
ch_count = "0"
if grade.strip() in ("0", "1", "2"):
grade = ("小学", "初中", "高中")[int(grade)]
else:
grade = "NULL"
if publish.strip() in ("0", "1"):
publish = ("人教版", "译林版")[int(publish)]
else:
publish = "NULL"
if not error_count.strip().isnumeric():
error_count = "0"
# 多条件组合
if word == "NULL" and grade != "NULL" and publish != "NULL": # 100
for key in words.keys():
if len(key) >= int(ch_count) and words[key][0] == grade and publish in words[key][1].split("|") and int(words[key][4]) >= int(error_count):
words_get_dic[key] = words[key]
if word == "NULL" and grade == "NULL" and publish != "NULL": # 110
for key in words.keys():
if len(key) >= int(ch_count) and publish in words[key][1].split("|") and int(words[key][4]) >= int(error_count):
words_get_dic[key] = words[key]
if word == "NULL" and grade != "NULL" and publish == "NULL": # 101
for key in words.keys():
if len(key) >= int(ch_count) and words[key][0] == grade and int(words[key][4]) >= int(error_count):
words_get_dic[key] = words[key]
if word == "NULL" and grade == "NULL" and publish == "NULL": # 111
for key in words.keys():
if len(key) >= int(ch_count) and int(words[key][4]) >= int(error_count):
words_get_dic[key] = words[key]
if word != "NULL": # 010,001,011,000
for key in words.keys():
if key == word:
words_get_dic[key] = words[key]
return words_get_dic
# 新建函数dic_to_txt(),将字典转为txt文件
def dic_to_txt(filename, dic, mode):
with open(filename, "w+", encoding="utf-8") as f:
for key in dic.keys():
if mode == 0: # 数据模式
f.write(key + "\t" + dic[key][0] + "\t" + dic[key][1] + "\t" + dic[key][2] + "\t" + dic[key][3] + "\t" + dic[key][4] + "\n")
if mode == 1: # 背诵模式
f.write("单词:" + key + "\n")
f.write("音标:" + dic[key][2] + "\n")
f.write("释义:\n")
ls = dic[key][3].split("|")
for i in ls:
f.write(" " + i + "\n")
f.write("\n")
# 新建函数write_from_mem(),根据中文意思默写单词
def write_from_mem(dic, n, rm=0):
print("========================批量默写单词========================")
if len(dic) == 0 or n == 0:
print("无可默写的单词!")
else:
if n >= len(dic):
n = len(dic)
sample_keys = r.sample(dic.keys(), n)
n = 0
e = 0
for key in sample_keys:
print("默写序号:", n+1)
print("单词释义:")
for i in dic[key][3].split("|"):
if key in i:
i = i.replace(key, "*"*len(key))
if key.lower() in i:
i = i.replace(key.lower(), "*" * len(key))
if key.upper() in i:
i = i.replace(key.upper(), "*" * len(key))
if key.capitalize() in i:
i = i.replace(key.capitalize(), "*" * len(key))
print(" " + i)
# 限制默写提示的字符个数
if rm not in [i for i in range(len(key) + 1)]:
rm = 0
ch_dic = dict()
for i in range(len(key)):
ch_dic[i] = key[i]
for j in r.sample(ch_dic.keys(), len(key) - rm):
ch_dic[j] = "_"
rm_txt = "".join(ch_dic.values())
print("单词提示:", rm_txt)
word_write = input("请输入默写的单词(不区分大小写):").strip()
if word_write.lower() == key.lower():
print("默写结果:正确!。")
else:
print("默写错误:错误!正确答案为:" + key, end=",")
e = e + 1
words[key][4] = str(int(words[key][4]) + 1)
print("累计默写错误次数:", words[key][4])
print("\n")
n = n + 1
print("默写完毕!共默写{0:}个单词,错误{1:}个单词,正确率{2:.2%}!".format(n, e, ((n-e)/n)))
# 定义主函数main(data_filename, output_filename)
def main(data_filename, output_filename):
get_inputs()
if a == "0": # 查询功能
dic_to_txt(output_filename, words_get(data_filename), int(b))
print("查询完毕!查询结果保存在当前目录下{}中。".format(output_filename))
if a == "1": # 默写功能
write_from_mem(words_get(data_filename), int(b), int(c))
dic_to_txt(data_filename, words, 0) # 更新单词默写错误的次数
if __name__ == '__main__':
main("英语词汇.txt", "查询结果.txt")