-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassify.py
247 lines (201 loc) · 9.49 KB
/
classify.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
#!/usr/bin/env python
"""
Tool to help a human being to classify the scraped VPM pages
into several categories
It creates a GUI browser that shows sequentially one of the detected pages.
You can interact with the browser with the mouse and you can also use the
numerical pad of the keyboard to select one of the categories. Once you
have chosen the right category for a page the software moves to the next.
Author: Carlo Bottai
Copyright (c) 2020 - TU/e and EPFL
License: See the LICENSE file.
Date: 2020-10-16
"""
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
import qtawesome as qta
import sys
import webbrowser
from flata import Flata, Query, JSONStorage
import requests
from iris_utils.parse_args import parse_io
USER_AGENT = ('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) '
'Gecko/2009021910 Firefox/3.0.7')
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
args = parse_io()
self.f_in = args.input
self.read_data()
self.view = QWebEngineView()
self.view.settings() \
.setAttribute(QWebEngineSettings.PluginsEnabled, True)
self.setCentralWidget(self.view)
self.status = QStatusBar()
self.setStatusBar(self.status)
navtb = QToolBar('Navigation')
self.addToolBar(navtb)
back_btn = QAction(qta.icon('fa5s.arrow-left'), 'Back', self)
back_btn.triggered.connect(lambda: self.view.back())
navtb.addAction(back_btn)
next_btn = QAction(qta.icon('fa5s.arrow-right'), 'Forward', self)
next_btn.triggered.connect(lambda: self.view.forward())
navtb.addAction(next_btn)
navtb.addSeparator()
self.urlbar = QLineEdit()
self.urlbar.returnPressed.connect(self.go_to_url)
navtb.addWidget(self.urlbar)
navtb.addSeparator()
reload_btn = QAction(qta.icon('fa5s.redo'), 'Reload', self)
reload_btn.triggered.connect(lambda: self.view.reload())
navtb.addAction(reload_btn)
stop_btn = QAction(qta.icon('fa5s.stop'), 'Stop', self)
stop_btn.triggered.connect(lambda: self.view.stop())
navtb.addAction(stop_btn)
open_btn = QAction(
qta.icon('fa5s.external-link-square-alt'), 'Open', self)
open_btn.triggered.connect(lambda: \
webbrowser.open_new_tab(self.urlbar.text()))
navtb.addAction(open_btn)
labtb = QToolBar('Labeling')
self.addToolBar(Qt.RightToolBarArea, labtb)
for name, idx in [
('VPM page | True patent-product link', 1),
('Brochure or description of the product | True patent-product link', 2),
('Hybrid document | True patent-product link', 3),
('List of patents or metadata of a patent | False patent-product link', 4),
('A scientific publication | False patent-product link', 5),
('News about the patent | False patent-product link', 6),
('CV/resume | False patent-product link', 7),
('Something else in a website to keep | False patent-product link', 8),
('Something else in a website to exclude | False patent-product link', 9),
('The document is unreachable | False patent-product link', 0)]:
label = QAction(f'{name} ({idx})', self)
label.setShortcut(str(idx))
label.triggered.connect(lambda checked, lbl=name: self.label_page(lbl))
labtb.addAction(label)
#labtb.addSeparator()
urls_len_lbl = f'{self.data_to_classify_len} URLs left to classify'
self.status.showMessage(urls_len_lbl)
self.open_next_page()
self.show()
self.setWindowTitle('VPM pages handmade classifier')
def read_data(self):
DB = Flata(self.f_in, storage=JSONStorage)
self.database = DB.table('iris_vpm_pages_classifier')
to_classify = \
(Query().vpm_page_classification==None) & \
(Query().vpm_page!=None)
self.data_to_classify = iter(self.database.search(to_classify))
self.data_to_classify_len = self.database.count(to_classify)
def go_to_url(self, url=None):
if url is None:
url = self.urlbar.text()
else:
self.urlbar.setText(url)
self.urlbar.setCursorPosition(0)
try:
response = requests.head(
url,
headers={'User-Agent': USER_AGENT},
verify=False,
allow_redirects=True,
timeout=10)
headers = response.headers
content_type = headers['Content-Type']
if 'Content-Disposition' in headers:
content_disposition = headers['Content-Disposition']
else:
content_disposition = ''
if not (content_type.startswith('text/html') or \
content_type.startswith('application/pdf') or \
content_type.startswith('text/plain')) or \
content_disposition.startswith('attachment'):
self.msgBox = QMessageBox.about(
self,
'Additional information (DOWNLOAD)',
('It is possible that it is needed to download the next '
'document.\nIf you do not see the page changing, try to '
'open the page in a browser by clicking on '
'the appropriate button'))
except:
pass
url = QUrl(url)
if url.scheme() == '':
url.setScheme('https')
self.view.setUrl(url)
def open_next_page(self):
try:
self.current_data = next(self.data_to_classify)
while self.current_data['vpm_page_classification']:
self.current_data = next(self.data_to_classify)
INFO_MSG = {
'COPYRIGHT':
('The information about the patent(s) has been '
'detected close to the copyright information '
'at the bottom of the document.\n'
'Please, confirm whether or not there is a link '
'between a patent and a product in this document'),
'NOCORPUS':
('No information about any of the patents has been '
'detected in the document.\nPlease, confirm whether '
'or not there is a link between a patent and a '
'product in this document'),
'NOCORPUS+IMG':
('The only information about the patent(s) '
'has been detected in one of the pictures '
'of the document.\nPlease, confirm whether '
'or not there is a link between a patent '
'and a product in this document'),
'NOCORPUS+PATNUMINURL':
('The only information about the patent(s) '
'has been detected in the URL '
'of the document.\nPlease, confirm whether '
'or not there is a link between a patent '
'and a product in this document')}
vpm_page_automatic_classification = self.current_data[
'vpm_page_automatic_classification']
vpm_page_automatic_classification_info = \
vpm_page_automatic_classification \
.split(' | ')[1]
if vpm_page_automatic_classification_info in INFO_MSG.keys():
vpm_page_automatic_classification_msg = INFO_MSG[
vpm_page_automatic_classification_info]
self.msgBox = QMessageBox.about(
self,
f'Additional information ({vpm_page_automatic_classification_info})',
vpm_page_automatic_classification_msg)
print('\n+++++++++++++++++++++++++++')
print(f"Patent assignee: {self.current_data['patent_assignee']}")
try:
print(f"Award recipient: {self.current_data['award_recipient']}")
except Exception:
pass
print(f"Patents: {self.current_data['patent_id']}")
print('+++++++++++++++++++++++++++\n')
url = self.current_data['vpm_page']
self.go_to_url(url)
except:
print('\n+++++++++++++++++++++++++++')
print('No other pages left. Well done!')
print('+++++++++++++++++++++++++++\n')
self.close()
def label_page(self, label):
updated_info = self.database.update(
{'vpm_page_classification': label},
Query().vpm_page==self.current_data['vpm_page'])
updated_ids = updated_info[0]
# Reduce the number of pages left by one
# and show this information in the status bar
self.data_to_classify_len -= len(updated_ids)
urls_len_lbl = f'{self.data_to_classify_len} URLs left to classify'
self.status.showMessage(urls_len_lbl)
self.open_next_page()
self.update()
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setApplicationName('VPM pages handmade classifier')
window = MainWindow()
app.exec_()