这里我有一段代码执行以下操作:
import nltk
from nltk.corpus import words
from nltk.corpus import wordnet
import re
import tkinter as tk
from tkinter.scrolledtext import ScrolledText
import string
from textblob import Word
from gramformer import Gramformer
gf = Gramformer(models = 1, use_gpu=False) # 1=corrector, 2=detector
#nltk.download("words")
#nltk.download("wordnet")
w= words.words()+list(wordnet.words())
w.append("typing")
word_set= set(w) #set of correctly spelled words from NLTK corpus
root= tk.Tk()
root.geometry("600x600")
text= ScrolledText(root, font = ("Arial", 18))
correction_text = tk.Text(root)
correction_text.pack(side="bottom", fill="y")
text.pack(fill="both", expand=True)
wrong_words= []
def check(self):
global wrong_words
#get mark
mark = text.mark_previous('lpos')
#get characters from mark to just before last space or return
word = text.get("lpos", f'{tk.INSERT}-1c')
#if that word is not in the word list, tag it
if not (re.sub("[^\w]", "", word.lower()) in word_set):
text.tag_add('misspelled', "lpos", f'{tk.INSERT}-1c')
if word!= '' and word.endswith('.') or word.endswith('?') or word.endswith(';') or word.endswith(',') or word.endswith(':') or word.endswith('-') :
word = word[0:len(word)-1]
if word!= "":
wrong_words.append(word)
print(wrong_words)
#move mark to caret position
text.mark_set('lpos', tk.INSERT)
#check on return and space
for key in ('Return', 'space'):
text.bind(f"<KeyRelease-{key}>", check)
def hover(event):
text = event.widget
keyword_start = text.index(f"@{event.x},{event.y} wordstart")
keyword_end = text.index(f"@{event.x},{event.y} wordend")
word = text.get(keyword_start, keyword_end)
text.tag_remove("keyword", "1.0", "end")
if word in wrong_words:
text.mark_set("keyword_start", keyword_start)
text.mark_set("keyword_end", keyword_end)
text.tag_add("keyword", keyword_start, keyword_end)
def keyword_click(event):
text = event.widget
word = text.get("keyword_start", "keyword_end")
blob_word= Word(word)
result = blob_word.spellcheck()
label_word= ""
for el in result:
label_word +=el[0]+"\n"
correction_text.delete('1.0', tk.END)
correction_text.insert('1.0', "Did you mean:\n"+label_word)
text.bind("<Motion>", hover)
text.tag_bind("keyword", "<1>", keyword_click)
#
#init mark at first position with gravity set to left so it wont move as you type
text.mark_set('lpos', "1.0")
text.mark_gravity('lpos', tk.LEFT)
#make 1 tag and reuse it
text.tag_configure('misspelled', foreground= "red")
root.mainloop()
我想做以下事情: 我希望能够单击 Corrected_text 中的更正单词建议之一,并将相应的拼写错误单词替换为所选的更正。
我该怎么做?
要启用用 Correction_text 框中的更正建议中的单词替换拼写错误的单词的功能,您可以修改 keywords_click 函数并在其中集成替换逻辑。所以请找到我的keyword_click函数的代码来实现这个
def keyword_click(event):
text_widget = event.widget
word = text_widget.get("keyword_start", "keyword_end")
blob_word = Word(word)
result = blob_word.spellcheck()
label_word = ""
for el in result:
label_word += el[0] + "\n"
correction_text.delete('1.0', tk.END)
correction_text.insert('1.0', "Did you mean:\n" + label_word)
def replace_word(replacement):
start = text_widget.search(word, "1.0", tk.END)
while start:
end = text_widget.index(f"{start}+{len(word)}c")
text_widget.delete(start, end)
text_widget.insert(start, replacement)
start = text_widget.search(word, end, tk.END)
def handle_correction_click(event):
index = correction_text.index("@%s,%s" % (event.x, event.y))
line, char = index.split('.')
line = int(line)
char = int(char)
if line == 1:
selected_word = correction_text.get(f"{line}.{char}", f"{line}.{char + 1} lineend")
replace_word(selected_word.strip())
correction_text.bind("<Button-1>", handle_correction_click)
text.bind("<Motion>", hover)
text.tag_bind("keyword", "<1>", keyword_click)