-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.py
252 lines (221 loc) · 9.62 KB
/
GUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import sys
# from PyQt6.QtWidgets import QMainWindow, QApplication, QPushButton, QToolTip, QMessageBox
from PyQt6.QtWidgets import *
from PyQt6.QtGui import QFont, QPalette, QColor, QIcon
from timeit import default_timer as timer
class MyWidget(QWidget):
def __init__(self, search_engine):
super().__init__()
self.search_engine = search_engine
self.doc_list = []
self.simple_search = False
self.initUI()
self.center() # 调用center方法
self.show()
def center(self):
# 获取窗口的矩形
qtRectangle = self.frameGeometry()
# 获取屏幕的中心点
centerPoint = self.screen().availableGeometry().center()
# 移动窗口的中心点到屏幕的中心点
qtRectangle.moveCenter(centerPoint)
self.move(qtRectangle.topLeft())
def initUI(self):
# QToolTip.setFont(QFont('SansSerif', 10))
# self.setToolTip('This is a <b>QWidget</b> widget')
############
## 搜索设置
self.layout1 = QVBoxLayout()
# 创建下拉窗口
self.combo = QComboBox(self)
self.combo.addItem('Search Method: Simple')
self.combo.addItem('Search Method: Complex')
self.combo.currentIndexChanged.connect(self.selectionchange)
# 显示文本
self.label1 = QLabel(self)
self.label1.setText("Time Limit:")
self.label1.setFont(QFont('Time', 10, QFont.Weight.Bold)) # 设置字体字号
# 创建单行输入框并设置回车键响应, 接收时间限制
self.input_box_time = QLineEdit(self)
self.input_box_time.editingFinished.connect(self.get_time_limit)
self.input_box_time.setFont(QFont('Time', 20, QFont.Weight.Bold)) # 设置字体字号
self.layout1.addWidget(self.label1)
self.layout1.addWidget(self.input_box_time)
self.layout2 = QVBoxLayout()
# 显示文本
self.label = QLabel(self)
self.label.setText("Query:")
self.label.setFont(QFont('Time', 10, QFont.Weight.Bold)) # 设置字体字号
# 创建单行输入框并设置回车键响应,接收查询语句
self.input_box_query = QLineEdit(self)
self.input_box_query.returnPressed.connect(self.process_input)
self.input_box_query.setFont(QFont('Time', 20, QFont.Weight.Bold)) # 设置字体字号
self.layout2.addWidget(self.label)
self.layout2.addWidget(self.input_box_query)
# 创建水平布局,将两个垂直布局放入其中
self.layout_H = QHBoxLayout()
self.layout_H.addLayout(self.layout1, 2)
self.layout_H.addLayout(self.layout2, 8)
############
## 搜索结果
self.layout = QVBoxLayout()
self.layout.addWidget(self.combo,)
self.layout.addLayout(self.layout_H)
# 显示文本
self.label3 = QLabel(self)
self.label3.setText("Results:")
self.label3.setFont(QFont('Time', 10, QFont.Weight.Bold)) # 设置字体字号
# 创建列表部件,用以显示搜索结果
self.list_widget = QListWidget(self)
self.list_widget.setObjectName("listWidget_docs")
self.list_widget.setFont(QFont('Time', 20, QFont.Weight.Bold)) # 设置字体字号
self.list_widget.itemDoubleClicked.connect(self.on_item_double_clicked) # 设置双击项目时触发的槽函数
self.list_widget.itemClicked.connect(self.on_item_clicked) # 设置鼠标进入项目时触发的槽函数
self.layout.addWidget(self.label3)
self.layout.addWidget(self.list_widget)
# 显示文本
self.label4 = QLabel(self)
self.label4.setText("Content:")
self.label4.setFont(QFont('Time', 10, QFont.Weight.Bold)) # 设置字体字号
# 创建文本框,显示更详细内容
self.text_box = QTextEdit(self)
self.text_box.setObjectName("textEdit")
self.text_box.setFont(QFont('Time', 20, QFont.Weight.Bold)) # 设置字体字号
self.text_box.setReadOnly(True) # 设置为只读
self.layout.addWidget(self.label4)
self.layout.addWidget(self.text_box)
# 状态栏
self.label5 = QLabel(self)
self.label5.setText("Ready") # 当状态栏用(左下角)
self.label5.setFont(QFont("KaiTi", 10, QFont.Weight.Normal)) # 设置字体字号
self.layout.addWidget(self.label5)
self.layout.addWidget(self.label5)
# 设置中央窗口部件的布局管理器
self.setLayout(self.layout)
self.setGeometry(300, 600, 600, 700) # x, y, width, height
self.setWindowTitle('Search Engine')
self.setWindowIcon(QIcon('icon.png'))
# def closeEvent(self, event):
# reply = QMessageBox.question(self, 'Message', 'Are you sure to quit?', QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, QMessageBox.StandardButton.No)
# if reply == QMessageBox.StandardButton.Yes:
# event.accept()
# else:
# event.ignore()
def reset(self):
self.list_widget.clear()
self.text_box.clear()
# self.statusBar().setStyleSheet("QStatusBar {color: black;}")
self.label5.setStyleSheet("color: black")
def get_time_limit(self):
"""获取用户输入的时间限制 """
self.reset()
input_text = self.input_box_time.text()
try:
integer_value = float(input_text)
if integer_value > 0:
# integer_value = float(text)
print(f"用户输入了: {integer_value}")
if integer_value > 5: # 设置最大时间限制为5min
integer_value = 5
self.search_engine.set_time_limit(integer_value)
self.label5.setText(f"set time limit:{integer_value} min") # 状态栏(左下角)
return integer_value
else:
self.search_engine.set_time_limit(2)
self.label5.setText('Error! Please input valid number!')
self.label5.setStyleSheet("color: red")
except ValueError:
self.label5.setText('Error! Please input valid number!') # 状态栏(左下角)
# self.statusBar().setStyleSheet("QStatusBar {color: red;}")
# 设置self.label5的字体颜色为红色
self.label5.setStyleSheet("color: red")
def get_input(self):
"""
获取用户输入的文本
"""
input_text = self.input_box_query.text()
# print(f"用户输入了: {input_text}")
if self.simple_search:
words = input_text.split()
query = {
"bool": {
"should": [{"match": {"transcript": f"{word}"}} for word in words],
}
}
else:
query = {
"bool": {
"must": {
"match": {
"transcript": {
"query": input_text,
"minimum_should_match": "50%"
}
}
},
"should": {
"match_phrase": {
"transcript": {
"query": input_text,
"slop": 20
}
}
}
}
}
return query, input_text
def show_result(self):
"""
搜索结果显示到list_view上
:param doc_list: 搜索结果列表
"""
self.list_widget.clear()
for i, clip in enumerate(self.doc_list):
str = f"{i+1}. " + clip.show_info.get('show_filename_prefix')
str += " (" + " ".join(clip.text.split()[:3]) + "...)"
str += f": {clip.score:.5f}"
self.list_widget.addItem(str)
def process_input(self):
"""
处理用户输入
"""
self.get_time_limit()
"""获取输入文本"""
query, input_text = self.get_input()
"""进行搜索"""
self.label5.setText('Searching...') # 状态栏(左下角)
t0 = timer()
self.doc_list = self.search_engine.search(query, input_text)
self.label5.setText('Search Finished') # 状态栏(左下角)
t1 = timer()
"""展示搜索结果"""
self.show_result()
self.label5.setText(f"OK, search use time: {t1-t0} s") # 状态栏(左下角)
# 设置双击项目时触发的槽函数
def on_item_double_clicked(self, item):
"""
双击显示transcript
"""
index = self.list_widget.row(item)
text = self.doc_list[index].text
# 用 QTextEdit 显示富文本
text_box = QTextEdit()
text_box.setHtml(text)
self.text_box.setText(text)
def on_item_clicked(self, item):
"""
单击小窗口显示episode_name
"""
index = self.list_widget.row(item)
text = self.doc_list[index].show_info.get('episode_name') # 显示 episode_name
item.setToolTip(text)
def selectionchange(self, idx):
if idx == 0:
self.simple_search = True
else:
self.simple_search = False
if __name__ == '__main__':
app = QApplication(sys.argv)
search_engine = None # TODO 这里要创建SeacherEngine对象
w = MyWidget(search_engine)
sys.exit(app.exec())