-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlossaryKeywordSearchReport.py
344 lines (285 loc) · 11.6 KB
/
GlossaryKeywordSearchReport.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env python
"""Find phrases in Glossary documents.
"""
from functools import cached_property
from cdrcgi import Controller
from cdrapi.docs import Doc
from lxml import etree
from re import compile, IGNORECASE, UNICODE
class Control(Controller):
"""Logic manager for the report."""
SUBTITLE = "Glossary Keyword Search Report"
LOGNAME = "GlossaryKeywordSearchReport"
REGEX_FLAGS = UNICODE | IGNORECASE
CONCEPT_PATH = "/GlossaryTermName/GlossaryTermConcept/@cdr:ref"
def build_tables(self):
"""Assemble the report."""
if not self.terms:
message = "At least one search term is required."
self.alerts.append(dict(message=message, type="warning"))
return self.show_form()
return self.Reporter.Table(self.rows, columns=self.columns)
def populate_form(self, page):
"""Put the fields on the form.
Pass:
page - `cdrcgi.HTMLPage` object
"""
# Put up the field sets
fieldset = page.fieldset("Report Filters")
languages = "Any", "English", "Spanish"
opts = dict(options=languages, default="Spanish")
fieldset.append(page.select("language", **opts))
audiences = "Any", "Patient", "Health professional"
opts = dict(options=audiences, default="Patient")
fieldset.append(page.select("audience", **opts))
page.form.append(fieldset)
fieldset = page.fieldset("Enter Search Terms", id="search-terms")
legend = fieldset.find("legend")
add_button = page.B.SPAN(
page.B.IMG(
page.B.CLASS("clickable"),
src="/images/add.gif",
onclick="add_term_field();",
title="Add another term"
),
page.B.CLASS("term-button")
)
legend.append(add_button)
fieldset.append(page.text_field("term", classes="term"))
page.form.append(fieldset)
page.add_output_options(default="html")
# Button/script for adding new search term fields.
page.add_css(".term-button { padding-left: 10px; }")
page.add_script("""\
function add_term_field() {
var id = "term-" + (jQuery(".term").length + 1);
var field = jQuery("<div>", {class: "labeled-field"});
var cls = "term usa-input usa-input--xl";
field.append(jQuery("<label>", {for: id, text: "Term", class: "usa-label"}));
field.append(jQuery("<input>", {class: cls, name: "term", id: id}));
jQuery("#search-terms").append(field);
}
""")
@cached_property
def audience(self):
"""Optional audience for narrowing the report."""
return self.fields.getvalue("audience")
@cached_property
def columns(self):
"""Headers for the report's table."""
return (
self.Reporter.Column("GTN ID", width="75px"),
self.Reporter.Column("GTC ID", width="75px"),
self.Reporter.Column("Term Names", width="400px"),
self.Reporter.Column("Definitions", width="600px"),
)
@cached_property
def docs(self):
"""Sequence of GlossaryDoc objects containing the user's term(s)."""
docs = []
query = self.Query("pub_proc_cg p", "p.id", "c.int_val", "p.xml")
query.join("query_term_pub c", "c.doc_id = p.id")
query.where(query.Condition("c.path", self.CONCEPT_PATH))
row = query.execute(self.cursor).fetchone()
while row:
doc = GlossaryDoc(self, row.id, row.int_val, row.xml)
if doc.row:
docs.append(doc)
row = self.cursor.fetchone()
return docs
@cached_property
def language(self):
"""Optional language for narrowing the report."""
language = self.fields.getvalue("language")
return language if language in ("English", "Spanish") else None
@cached_property
def regex(self):
"""
Create a compiled regular expression for finding the caller's phrases.
The ugly wrapper surrounding the phrases ensures that we
match on word boundaries, so that (for example) "breast"
isn't matched in the phrase "they were walking abreast."
Escape characters which have special meaning in a regular expression.
We also make sure that Microsoft doesn't mess up the matching
when it replaces apostrophes with "smart quotes" (as it frequently
does).
"""
phrases = [GlossaryDoc.normalize(phrase) for phrase in self.terms]
expressions = []
for phrase in sorted(phrases, key=len, reverse=True):
expressions.append(phrase
.replace("\\", r"\\")
.replace("+", r"\+")
.replace(" ", r"\s+")
.replace(".", r"\.")
.replace("^", r"\^")
.replace("$", r"\$")
.replace("*", r"\*")
.replace("?", r"\?")
.replace("{", r"\{")
.replace("}", r"\}")
.replace("[", r"\[")
.replace("]", r"\]")
.replace("|", r"\|")
.replace("(", r"\(")
.replace(")", r"\)")
.replace("'", "['\u2019]"))
expressions = "|".join(expressions)
expression = f"(?<!\\w)({expressions})(?!\\w)"
return compile(expression, self.REGEX_FLAGS)
@cached_property
def rows(self):
"""Rows for the report's table."""
return [doc.row for doc in sorted(self.docs)]
@property
def terms(self):
"""Phrases to look for."""
if not hasattr(self, "_terms"):
self._terms = []
for term in self.fields.getlist("term"):
term = term.strip()
if term:
self._terms.append(term)
return self._terms
@cached_property
def wide_css(self):
"""Use the entire browser page width for the report table."""
return self.Reporter.Table.WIDE_CSS
class GlossaryDoc:
"""CDR Glossary document which knows how to find specific phrases."""
WHITESPACE = compile(r"\s+")
AUDIENCES = {"Patient", "Health professional"}
def __init__(self, control, gtn_id, gtc_id, xml):
"""Look for matches with the user's term phrases.
Sets `self.row` if any matches found. Otherwise `self.row` is `None`.
The constructor assembles the calculated values because we don't
want the original document retained in memory, as it would be if
we were using `@property` to calculate those values as needed.
Pass:
control - access to the database and the user's report criteria
gtn_id - integer for the CDR GlossaryTermName document's unique ID
gtc_id - integer for the CDR GlossaryTermConcept document's ID
xml - filtered GlossaryTerm document exported to cancer.gov
"""
self.control = control
self.row = None
self.matched = False
try:
root = etree.fromstring(xml.encode("utf-8"))
except Exception:
control.logger.exception("parsing CDR%d", gtn_id)
root = None
if root is not None:
self.sort_key = self.__get_sort_key(root)
names = self.__get_names(root)
definitions = self.__get_definitions(root)
if self.matched:
self.row = gtn_id, gtc_id, names, definitions
def __lt__(self, other):
"""Support sorting the documents by term name."""
return self.sort_key < other.sort_key
def __get_sort_key(self, root):
"""Find the name in the appropriate language for sorting.
Pass:
root - parsed export document
Return:
English or Spanish term name used for sorting
"""
path = "TermName"
if self.control.language == "Spanish":
path = "SpanishTermName"
return Doc.get_text(root.find(path), "").strip().lower()
def __get_names(self, root):
"""Assemble the term's names.
Only get the ones matching the user's language. If any match any
of the user's terms, highlight the match and set `self.matched`
to `True`.
Pass:
root - parsed export document
Return:
sequence of term name strings
"""
if self.control.language == "English":
paths = ["TermName"]
elif self.control.language == "Spanish":
paths = ["SpanishTermName"]
else:
paths = ["TermName", "SpanishTermName"]
names = []
for path in paths:
for node in root.findall(path):
text = Doc.get_text(node, "").strip()
if text:
text = self.normalize(text)
name = self.__add_highlights(text)
if name != text:
self.matched = True
names.append(name)
return names
def __get_definitions(self, root):
"""Assemble the term's definitions.
Only get the ones matching the user's language and audience.
If any contain matches for any of the user's terms, highlight
the match and set `self.matched` to `True`.
Pass:
root - parsed export document
Return:
sequence of term definition strings
"""
if self.control.language == "English":
paths = ["TermDefinition"]
elif self.control.language == "Spanish":
paths = ["SpanishTermDefinition"]
else:
paths = ["TermDefinition", "SpanishTermDefinition"]
definitions = []
for path in paths:
for node in root.findall(path):
if self.control.audience in self.AUDIENCES:
audience = Doc.get_text(node.find("Audience"), "").strip()
if audience != self.control.audience:
continue
text = Doc.get_text(node.find("DefinitionText"), "").strip()
if text:
text = self.normalize(text)
definition = self.__add_highlights(text)
if definition != text:
self.matched = True
definitions.append(definition)
return definitions
def __add_highlights(self, text):
"""Highlight matches with the user's target strings.
Pass:
text - original string for term name or definition
Return:
possibly altered string to highlight matched term phrases
"""
matches = [m for m in self.control.regex.finditer(text)]
if not matches:
return text
position = 0
segments = []
for match in matches:
start, end = match.span()
if start > position:
segments.append(text[position:start])
segments.append("\u25b6")
segments.append(text[start:end])
segments.append("\u25c0")
position = end
if position < len(text):
segments.append(text[position:])
return "".join(segments)
@staticmethod
def normalize(me):
"""
Reduce contiguous sequences of whitespace to single spaces
Pass:
me - string to be normalized
Return:
processed string
"""
return GlossaryDoc.WHITESPACE.sub(" ", me)
if __name__ == "__main__":
"""Don't execute if loaded as a module."""
Control().run()