当前位置: 代码迷 >> python >> 短语突出显示QEditText PyQt4 Python3
  详细解决方案

短语突出显示QEditText PyQt4 Python3

热度:128   发布时间:2023-07-16 10:40:56.0

我正在使用PyQt4 GUI工具和NLTK在python 3.4中创建GUI应用程序。 在下面,我在解释我在应用程序中必须执行的任务以及已完成的部分:

目标:

1)用户将创建类别并提供非常大的文本以保存到数据库中

2)根据上传的文本,用户将搜索词组(词组)。 短语可以出现在多行中。

3)匹配的短语列表将根据在文档中找到的行进行维护。

4)当用户选择匹配的短语行时,光标应移至匹配的短语行并突出显示该短语。

已完成:

1)我可以根据匹配的行上传文档和搜索短语。

2)并在QEditText文本框中突出显示

问题:1)我无法突出显示短语。 它仅突出显示短语中的单个单词

下面我所执行的代码突出显示了短语:

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

import string
# list view class
from view_matched_phrases_ui import Ui_ViewList
from PyDB import DatabaseHandle



class ViewList(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(ViewList, self).__init__(parent)
        self.list = Ui_ViewList()
        self.list.setupUi(self)

    def show_list(self, phrases):
        # self.list.phrase_text_view.setText("<div>Hello</div> Sahadev")

        # cursor = self.list.phrase_line_view.setCursor()
        # cursor.movePosition(QtGui.QTextCursor.Start)
        # self.list.phrase_text_view.setTextCursor(curson)

        ids = phrases[1]['pid']

        with DatabaseHandle() as db:
            for id in ids:
                sql = "SELECT document FROM contracts WHERE id="+str(id)
                data = db.get_single_data(sql)

                phrase_data = str.maketrans({key: None for key in string.punctuation})
                new_s = data[0].translate(phrase_data).lower()

                for line in new_s.splitlines():
                    if phrases[0].replace('_', ' ') in line:
                        self.list.phrase_line_view.addItem(line)

        # set selected contract in QTextEdit
        self.list.phrase_text_view.setPlainText(new_s)

        # self.list.phrase_text_view

        cursor = self.list.phrase_text_view.textCursor()

        # setup match
        format = QtGui.QTextCharFormat()
        format.setBackground(QtGui.QBrush(QtGui.QColor("yellow")))
        # Setup the regex engine
        pattern = phrases[0].replace("_", " ")

        regex = QtCore.QRegExp(pattern)
        # Process the displayed document
        pos = 0

        index = regex.indexIn(self.list.phrase_text_view.toPlainText(), pos)

        while index != -1:
            # Select the matched text and apply the desired format

            cursor.setPosition(index)
            cursor.movePosition(QtGui.QTextCursor.EndOfWord, 1)

            cursor.mergeCharFormat(format)
            # Move to the next match
            pos = index + regex.matchedLength()
            index = regex.indexIn(self.list.phrase_text_view.toPlainText(), pos)

        # print(self.list.phrase_text_view)
        # print(phrases)

这将对我有很大的帮助。

我通过计算短语长度和Qt Text Object的nextWord方法解决了这个问题。

其余代码将相同,仅更改我在此处提供的部分:

while index != -1:
        # Select the matched text and apply the desired format
        cursor.setPosition(index)
        # go to next word
        for i in range(len(self.matched_phrase.split(" "))):
            cursor.movePosition(QtGui.QTextCursor.NextWord, 1)
            cursor.mergeCharFormat(format)

        # Move to the next match
        pos = index + regex.matchedLength()
        index = regex.indexIn(self.list.phrase_text_view.toPlainText(), pos)
  相关解决方案