我正在python 3.4中使用PyQt4 GUI-tool&创建一个GUI应用程序。 NLTK。我在下面解释我必须在我的应用程序中执行的任务以及我已完成的部分:
目标:
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)
这一贡献对我有很大帮助。
答案 0 :(得分:0)
我通过使用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)