在循环遍历wx.TextCtrl.GetValue()以启用进程重复单词时,跟踪单词的位置

问题描述 投票:0回答:1

我曾尝试在wxPython GUI中进行拼写检查,但遇到重复拼写错误的单词时遇到麻烦。该功能需要将单词设置为红色,然后在MessageDialog中建议更正。关于MessageDialog的更正建议,一切都很好,但是我无法完成更改重复拼写错误的单词的颜色的操作。我知道问题是当我获得单词的起始位置时,它会一直在考虑单词的首次出现而忽略其他单词。

for i in range(self.tx_input.GetNumberOfLines()):
            line = self.tx_input.GetLineText(i)
            for word in text:
                if word in line and word not in suggestion:
                    startPos = self.tx_input.GetValue().find(word)
                    endPos = startPos + len(word)
                    self.tx_input.SetStyle(startPos, endPos, wx.TextAttr("red", "white")) 

由于我在行中循环,所以我会理解是否在同一行中出现拼写错误的问题,但是最令人讨厌的是,当重复在另一行中时,它也会失败。

我需要帮助来弄清楚如何跟踪单词的位置,这些位置在最终的新事件中已经被忽略了。


完整代码

class AddQuestion ( wx.Frame ):

    def __init__(self,parent):

        wx.Frame.__init__(self,parent,id=wx.ID_ANY, title='Grammar Checker', pos=wx.DefaultPosition, size=wx.Size(350,350), style=wx.DEFAULT_FRAME_STYLE)
        self.SetBackgroundColour( wx.Colour( 0, 93, 126 ))
        panel = wx.Panel(self)
        mainBox = wx.BoxSizer(wx.VERTICAL)
        panel.SetSizer(mainBox)

        self.tx_input = wx.TextCtrl(panel, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(350,150), wx.TE_MULTILINE|wx.TE_RICH2)
        mainBox.Add( self.tx_input, 1, wx.ALL|wx.EXPAND, 5 )

        btnBox = wx.BoxSizer(wx.VERTICAL)
        self.btn = wx.Button(panel, wx.ID_ANY,u'Grammar Check', wx.DefaultPosition, wx.DefaultSize,0)
        btnBox.Add(self.btn,0,wx.ALL,5)

        mainBox.Add(btnBox,0,wx.ALL|wx.ALIGN_CENTER,5)

        self.btn.Bind(wx.EVT_BUTTON, self.grammarCheck)

    def warn(self, parent, message, caption = 'WARNING!'):
        dlg = wx.MessageDialog(parent, message, caption, wx.OK | wx.ICON_WARNING)
        dlg.ShowModal()
        dlg.Destroy()

    def grammarCheck(self, event):

        from symspellpy import SymSpell

        sym_spell = SymSpell()
        sym_spell.load_dictionary("frequency_dictionary_en_82_765.txt", 0, 1)
        input_term = self.tx_input.GetValue().lower()
        # filter unecessary character
        ignore = r'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~1234567890“”–'
        text = sym_spell.word_segmentation(input_term.translate(str.maketrans("","",ignore)), max_edit_distance=2 ).segmented_string.split()
        suggestion = sym_spell.word_segmentation(input_term.translate(str.maketrans("","",ignore)), max_edit_distance=2 ).corrected_string.split()

        for i in range(self.tx_input.GetNumberOfLines()):
            line = self.tx_input.GetLineText(i)
            for word in text:
                if word in line and word not in suggestion:
                    startPos = self.tx_input.GetValue().find(word)
                    endPos = startPos + len(word)
                    self.tx_input.SetStyle(startPos, endPos, wx.TextAttr("red", "white"))

        for i in range(len(text)):
            if text[i] != suggestion[i]:
                self.warn(self, "`" + text[i] + "`" + " did you mean " + "`" + suggestion[i] + "` ?")


if __name__ == '__main__':

        app = wx.App(False)
        frame = AddQuestion(None)
        frame.Show()

        app.MainLoop() 
python wxpython
1个回答
0
投票

您一直在使用string.find(word)查找第一个匹配项查找类似“ python查找所有出现的子字符串”的内容,我发现:

# using list comprehension + startswith() 
# All occurrences of substring in string  
res = [i for i in range(len(test_str)) if test_str.startswith(test_sub, i)] 

import re

# using re.finditer() 
# All occurrences of substring in string  
res = [i.start() for i in re.finditer(test_sub, test_str)] 

都返回测试字符串中的起始位置列表,并且可以适应您的代码

© www.soinside.com 2019 - 2024. All rights reserved.