微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 tkinter 创建 python 拼写检查器

如何解决使用 tkinter 创建 python 拼写检查器

对于学校,我需要使用 python 创建一个拼写检查器。我决定使用 tkinter 创建的 GUI 来完成它。我需要能够输入一个将被检查的文本 (.txt) 文件一个字典文件,也是一个文本文件。程序需要打开这两个文件,对照字典文件检查检查文件,然后显示任何拼写错误的单词。

这是我的代码

import tkinter as tk
from tkinter.filedialog import askopenfilename

def checkFile():
    # get the sequence of words from a file
    text = open(file_ent.get())
    dictDoc = open(dict_ent.get())

    for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~':
        text = text.replace(ch,' ')
    words = text.split()

    # make a dictionary of the word counts
    wordDict = {}
    for w in words:
        wordDict[w] = wordDict.get(w,0) + 1

    for k in dictDict:
        dictDoc.pop(k,None)
    misspell_lbl["text"] = dictDoc

# Set-up the window
window = tk.Tk()
window.title("Temperature Converter")
window.resizable(width=False,height=False)

# Setup Layout
frame_a = tk.Frame(master=window)
file_lbl = tk.Label(master=frame_a,text="File Name")
space_lbl = tk.Label(master=frame_a,width = 6)
dict_lbl =tk.Label(master=frame_a,text="Dictionary File")
file_lbl.pack(side=tk.LEFT)
space_lbl.pack(side=tk.LEFT)
dict_lbl.pack(side=tk.LEFT)

frame_b = tk.Frame(master=window)
file_ent = tk.Entry(master=frame_b,width=20)
dict_ent = tk.Entry(master=frame_b,width=20)
file_ent.pack(side=tk.LEFT)
dict_ent.pack(side=tk.LEFT)

check_btn = tk.Button(master=window,text="Spellcheck",command=checkFile)

frame_c = tk.Frame(master=window)
message_lbl = tk.Label(master=frame_c,text="Misspelled Words:")
misspell_lbl = tk.Label(master=frame_c,text="")
message_lbl.pack()
misspell_lbl.pack()

frame_a.pack()
frame_b.pack()
check_btn.pack()
frame_c.pack()

# Run the application
window.mainloop()

我希望文件对照字典进行检查并在 mispell_lbl 中显示拼写错误的单词。

我用来使其工作并与作业一起提交的测试文件在这里

check file dictionary file

我已将文件预加载到我提交此文件站点,因此只需输入文件名和扩展名,而不是整个路径。

我很确定问题出在我读取和检查文件功能上,我一直在努力解决这个问题,但我被卡住了。任何帮助将不胜感激。

谢谢。

解决方法

第一个问题是您尝试读取文件的方式。 open(...) 将返回一个 _io.TextIOWrapper 对象,而不是字符串,这就是导致您出错的原因。要从文件中获取文本,您需要使用 .read(),如下所示:

def checkFile():
    # get the sequence of words from a file
    with open(file_ent.get()) as f:
      text = f.read()
    with open(dict_ent.get()) as f:
      dictDoc = f.read().splitlines()

with open(...) as f 部分为您提供了一个名为 f 的文件对象,并在完成后自动关闭该文件。这是更简洁的版本

f = open(...)
text = f.read()
f.close()

f.read() 将从文件中获取文本。对于字典,我还添加了 .splitlines() 以将换行符分隔的文本转换为列表。

我真的看不出你在哪里尝试检查拼写错误的单词,但你可以通过列表理解来完成。

misspelled = [x for x in words if x not in dictDoc]

这会获取字典文件中没有的每个单词,并将其添加到名为 misspelled 的列表中。总的来说,checkFile 函数现在看起来像这样,并且按预期工作:

def checkFile():
    # get the sequence of words from a file
    with open(file_ent.get()) as f:
      text = f.read()
    with open(dict_ent.get()) as f:
      dictDoc = f.read().splitlines()

    for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~':
        text = text.replace(ch,' ')
    words = text.split()

    # make a dictionary of the word counts
    wordDict = {}
    for w in words:
        wordDict[w] = wordDict.get(w,0) + 1
    
    misspelled = [x for x in words if x not in dictDoc]

    misspell_lbl["text"] = misspelled

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。