猫史档案馆


【Python作品分享】【46】我的英语词典(下)-成果包【教程贴】

用户:梧桐的灯梧桐的灯查看:0 回复:0 评论:0 创建时间:2022-07-15T19:32:30


【作品展示】

center_image

 

【作品介绍】

可以自行查询单词以及添加联想词的喵程序,欢迎体验

 

【作品源代码】

'''我的英语词典下-工程包'''
# import tool

def load_dict():
    '''加载词典'''
    '''
    输入:无
    输出:包含所有单词的字典,格式如{'apple':{'NO.':'1', 'mean': 'n.苹果', 'link': ''}, ......}
    '''
    #建立空字典用来存储加载出来的单词信息
    wordsDict = {}
    #打开词典文件
    with open('wordsSpace.txt', 'r', encoding='utf-8') as w:
        #读取全部数据
        word_list = w.readlines()
    for word in word_list:
        #读取到的信息处理
        wordList = word.split('#')
        wordDict = {
            'NO.': wordList[0], 'mean': wordList[2],"link":wordList[3][:-1] } # 向子典中添加联想词数据
        wordsDict[wordList[1]] = wordDict
    #将存有全部单词信息的wordsDict返回
    return wordsDict


def save_dict():
    '''保存词典'''
    with open("wordsSpace.txt",'w',encoding="utf-8")as w:
        for k, v in wordsDict.items():
            line=V['NO.'] + '#' + k + '#' + v["mean"] + "#" + v["link"] + "\n"
            w.write(line)
    


def search_by_ENG(word):
    '''英译中查询'''
    '''
    输入:想要查询的单词word
    输出:单词的对应格式字符串或者没有单词提示字符串res
    '''
    try:
        res = word+'  ' + wordsDict[word]['mean']
        # 判断有无联想词,有则将联想词一块输出
        if wordsDict[word]['link']:
            res+="联想词:"+wordsDict[word]["link"]
    except KeyError:
        res = 'Not found'
    return res


def add_linked_words(word, addWord):
    '''添加联想词'''
    '''
    输入:想要增加的联想词add,被添加的单词word
    输出:更新后的wordsDict
    '''
    wordsDict[word]["link"]+=addWord
    wordsDict[word]["link"]+=","
    return wordsDict


def menu():
    '''显示功能菜单'''
    print('-------------------------------')
    print('英译中查找单词功能请按:1')
    #添加联想词功能显示菜单
    print('添加联想词请按:2')
    print('退出功能请按:0')
    print('-------------------------------')


#主程序
#加载词典
wordsDict = load_dict()
#调用菜单函数
menu()
while True:
    #功能判断
    #选择为0时,退出
    message=input("请用户输入数字0,1,2,#")
    if message == '0':
        #退出时保存词典
        save_dict()
        break
    #选择为#时,呼出菜单
    elif message == '#':

        menu()
    #选择为1时,单词查询
    elif message == '1':
        while True:
            word = input('查找单词(按0返回):')
            if word == '0':
                break
            res = search_by_ENG(word)
            print(res)
    #选择为2时,添加联想词
    elif message=="2":
        word=input("请输入目标单词:")
        addWord=input("请输入添加的联想词")
        wordsDict=add_linked_words(word,addWord)
        print("添加成功")

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页