-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsql.py
More file actions
executable file
·466 lines (390 loc) · 15.3 KB
/
Copy pathsql.py
File metadata and controls
executable file
·466 lines (390 loc) · 15.3 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
"""
Greynir: Natural language processing for Icelandic
Scraper database queries
Copyright (C) 2023 Miðeind ehf.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
This module wraps a number of raw SQL queries using SQLAlchemy.
"""
from typing import Any, Iterable, Optional, Tuple, Union, cast
from datetime import datetime
from . import SessionContext, Session
ArticleListItem = Tuple[str, str, datetime, str, str]
ChartQueryItem = Tuple[str, int, int, int]
RelatedWordsItem = Tuple[str, str, int]
BestAuthorsItem = Tuple[str, int, int, int, float]
QueryCountItem = Tuple[int, int]
class _BaseQuery:
_Q = ""
def __init__(self) -> None:
pass
def execute_q(self, session: Session, q: str, **kwargs: Any) -> Iterable[Any]:
"""Execute the given query and return the result from fetchall()"""
return cast(Iterable[Any], cast(Any, session).execute(q, kwargs).fetchall())
def execute(self, session: Session, **kwargs: Any) -> Iterable[Any]:
"""Execute the default query and return the result from fetchall()"""
return cast(
Iterable[Any], cast(Any, session).execute(self._Q, kwargs).fetchall()
)
def scalar(self, session: Session, **kwargs: Any) -> Union[int, float]:
"""Execute the query and return the result from scalar()"""
return cast(Union[int, float], cast(Any, session).scalar(self._Q, kwargs))
class GenderQuery(_BaseQuery):
"""A query for gender representation in the persons table."""
_Q = """
select domain,
sum(case when gender = 'kk' then cnt else 0 end) as kk,
sum(case when gender = 'kvk' then cnt else 0 end) as kvk,
sum(case when gender = 'hk' then cnt else 0 end) as hk,
sum(cnt) as total
from (
select r.domain, p.gender, sum(1) as cnt
from persons as p, articles as a, roots as r
where p.article_url = a.url and a.root_id = r.id and r.visible
group by r.domain, p.gender
) as q
group by domain
order by domain;
"""
class StatsQuery(_BaseQuery):
"""A query for statistics on articles."""
_Q = """
select r.domain,
r.scrape as enabled,
sum(1) as art,
coalesce(sum(a.num_sentences),0) as sent,
coalesce(sum(a.num_parsed),0) as parsed
from articles as a, roots as r
where a.root_id = r.id and r.visible
group by r.domain, r.scrape
order by r.domain;
"""
class ChartsQuery(_BaseQuery):
"""Statistics on article, sentence and parse count
for all sources for a given time period."""
_Q = """
select r.description AS name,
count(a.id) AS cnt,
coalesce(sum(a.num_sentences),0) as sent,
coalesce(sum(a.num_parsed),0) as parsed
from roots as r
left join articles as a on r.id = a.root_id
and a.timestamp >= :start and a.timestamp < :end
where r.visible and r.scrape
group by name
order by name
"""
@classmethod
def period(
cls, start: datetime, end: datetime, enclosing_session: Optional[Session] = None
) -> Iterable[ChartQueryItem]:
r: Iterable[ChartQueryItem] = []
with SessionContext(session=enclosing_session, read_only=True) as session:
r = cast(
Iterable[ChartQueryItem], cls().execute(session, start=start, end=end)
)
return r
class QueryCountQuery(_BaseQuery):
"""Statistics on the number of queries received over a given time period."""
_Q = """
select count(queries.id), count(distinct queries.client_id) from queries
where timestamp >= :start and timestamp < :end
"""
@classmethod
def period(
cls, start: datetime, end: datetime, enclosing_session: Optional[Session] = None
) -> Iterable[QueryCountItem]:
r = cast(Iterable[QueryCountItem], [])
with SessionContext(session=enclosing_session, read_only=True) as session:
r = cast(
Iterable[QueryCountItem], cls().execute(session, start=start, end=end)
)
return r
class QueryTypesQuery(_BaseQuery):
"""Stats on the most frequent query types over a given time period."""
_Q = """
select count(queries.id), queries.qtype from queries
where queries.qtype is not NULL and
timestamp >= :start and timestamp < :end
group by queries.qtype
order by queries.count desc
"""
@classmethod
def period(
cls, start: datetime, end: datetime, enclosing_session: Optional[Session] = None
) -> Iterable[Any]:
g: Iterable[Any] = []
with SessionContext(session=enclosing_session, read_only=True) as session:
g = cls().execute(session, start=start, end=end)
return g
class QueryClientTypeQuery(_BaseQuery):
"""Stats on query client type and version (e.g. ios 1.3.0,
android 1.2.1, etc.) over a given time period."""
_Q = """
select client_type, client_version, count(client_type) as freq
from queries
where client_type is not NULL and client_type != '' and
timestamp >= :start and timestamp < :end
group by client_type, client_version
order by client_type
"""
@classmethod
def period(
cls, start: datetime, end: datetime, enclosing_session: Optional[Session] = None
) -> Iterable[Any]:
g: Iterable[Any] = []
with SessionContext(session=enclosing_session, read_only=True) as session:
g = cls().execute(session, start=start, end=end)
return g
class TopUnansweredQueriesQuery(_BaseQuery):
"""Return list of the most frequent *unanswered* queries
over a given time period."""
_Q = """
select question, count(question) as qoccurrence from queries
where answer is NULL and
timestamp >= :start and timestamp < :end
group by question
order by qoccurrence desc
limit :count
"""
_DEFAULT_COUNT = 20
@classmethod
def period(
cls,
start: datetime,
end: datetime,
count: int = _DEFAULT_COUNT,
enclosing_session: Optional[Session] = None,
) -> Iterable[Any]:
g: Iterable[Any] = []
with SessionContext(session=enclosing_session, read_only=True) as session:
g = cls().execute(session, start=start, end=end, count=count)
return g
class TopAnsweredQueriesQuery(_BaseQuery):
"""Return list of the most frequent *answered* queries
over a given time period."""
_Q = """
select question, count(question) as qoccurrence from queries
where answer is not NULL and
timestamp >= :start and timestamp < :end
group by question
order by qoccurrence desc
limit :count
"""
_DEFAULT_COUNT = 20
@classmethod
def period(
cls,
start: datetime,
end: datetime,
count: int = _DEFAULT_COUNT,
enclosing_session: Optional[Session] = None,
) -> Iterable[Any]:
g: Iterable[Any] = []
with SessionContext(session=enclosing_session, read_only=True) as session:
g = cls().execute(session, start=start, end=end, count=count)
return g
class BestAuthorsQuery(_BaseQuery):
"""A query for statistics on authors with the best parse ratios.
The query only includes authors with at least 10 articles."""
_MIN_ARTICLE_COUNT = 10
_Q = """
select * from (
select trim(author) as auth,
sum(1) as cnt,
sum(num_parsed) as sum_parsed,
sum(num_sentences) as sum_sent,
(sum(100.0 * num_parsed) / sum(1.0 * num_sentences)) as ratio
from articles
where num_sentences > 0
and articles.timestamp >= :start and articles.timestamp < :end
group by auth
) as q
where cnt >= :min_articles
order by ratio desc;
"""
@classmethod
def period(
cls,
start: datetime,
end: datetime,
min_articles: int = _MIN_ARTICLE_COUNT,
enclosing_session: Optional[Session] = None,
) -> Iterable[BestAuthorsItem]:
r = cast(Iterable[BestAuthorsItem], [])
with SessionContext(session=enclosing_session, read_only=True) as session:
r = cast(
Iterable[BestAuthorsItem],
cls().execute(session, start=start, end=end, min_articles=min_articles),
)
return r
class RelatedWordsQuery(_BaseQuery):
"""A query for word stems commonly occurring in the same articles
as the given word stem."""
_Q = """
select stem, cat, sum(cnt) as c
from (
select article_id as aid
from words
where stem=:root
) as src
join words on src.aid = words.article_id
group by stem, cat
order by c desc
limit :limit;
"""
@classmethod
def rel(
cls, stem: str, limit: int = 21, enclosing_session: Optional[Session] = None
) -> Iterable[RelatedWordsItem]:
"""Return a list of (stem, category, count) tuples describing
word stems that are related to the given stem, in descending
order of number of appearances."""
# The default limit is 21 instead of 20 because the original stem
# is usually included in the result list
r: Iterable[RelatedWordsItem] = []
with SessionContext(session=enclosing_session, read_only=True) as session:
r = cls().execute(session, root=stem, limit=limit)
return r
class TermTopicsQuery(_BaseQuery):
"""A query for topic vectors of documents where a given (stem, cat)
tuple appears. We return the newest articles first, in case the
query result is limited by a specified limit."""
_Q = """
select topic_vector, q.cnt
from (
select a.id as id, sum(w.cnt) as cnt
from articles a, words w
where a.id = w.article_id and w.stem = :stem and w.cat = :cat
group by a.id
) as q
join articles on q.id = articles.id
order by articles.timestamp desc
limit :limit;
"""
class ArticleCountQuery(_BaseQuery):
"""A query yielding the number of articles containing any of the given word stems."""
_Q = """
select count(*)
from (
select distinct article_id
from words
where stem in :stems
) as q;
"""
@classmethod
def count(
cls,
stems: Union[str, Iterable[str]],
enclosing_session: Optional[Session] = None,
) -> int:
"""Return a count of articles containing any of the given word
stems. stems may be a single string or an iterable."""
cnt = 0
with SessionContext(session=enclosing_session, read_only=True) as session:
cnt = int(
cls().scalar(
session,
stems=tuple((stems,)) if isinstance(stems, str) else tuple(stems),
)
)
return cnt
class ArticleListQuery(_BaseQuery):
"""A query returning a list of the newest articles that contain
a particular word stem."""
_Q_lower = """
select distinct a.id, a.heading, a.timestamp, r.domain, a.url
from words w, articles a, roots r
where w.stem = :stem and w.article_id = a.id and a.root_id = r.id and r.visible
order by a.timestamp desc
limit :limit;
"""
_Q_upper = """
select distinct a.id, a.heading, a.timestamp, r.domain, a.url
from words w, articles a, roots r
where (w.stem = :stem or w.stem = :lstem) and w.article_id = a.id
and a.root_id = r.id and r.visible
order by a.timestamp desc
limit :limit;
"""
@classmethod
def articles(
cls, stem: str, limit: int = 20, enclosing_session: Optional[Session] = None
) -> Iterable[ArticleListItem]:
"""Return a list of the newest articles containing the given stem."""
r: Iterable[ArticleListItem] = []
with SessionContext(session=enclosing_session, read_only=True) as session:
if stem == stem.lower():
# Lower case stem
r = cast(
Iterable[ArticleListItem],
cls().execute_q(session, cls._Q_lower, stem=stem, limit=limit),
)
# Upper case stem: include the lower case as well
r = cast(
Iterable[ArticleListItem],
cls().execute_q(
session, cls._Q_upper, stem=stem, lstem=stem.lower(), limit=limit
),
)
return r
class WordFrequencyQuery(_BaseQuery):
"""A query yielding the number of times a given word occurs in
articles over a given period of time, broken down by either
day or week."""
_Q = """
with days as (
select to_char(d, :datefmt) date
from generate_series(
:start,
:end,
:timeunit
) d
),
appearances as (
select to_char(a.timestamp, :datefmt) date, sum(w.cnt) cnt
from words w, articles a
where w.stem = :stem
and w.cat = :cat
and w.article_id = a.id
and a.timestamp >= :start
and a.timestamp <= :end
group by date
order by date
)
select days.date, coalesce(appearances.cnt,0) from days
left outer join appearances on days.date = appearances.date;
"""
@classmethod
def frequency(
cls,
stem: str,
cat: str,
start: datetime,
end: datetime,
timeunit: str = "day",
enclosing_session: Optional[Session] = None,
) -> Iterable[Any]:
result: Iterable[Any] = []
with SessionContext(session=enclosing_session, read_only=True) as session:
assert timeunit in ["week", "day"]
datefmt = "IYYY-IW" if timeunit == "week" else "YYYY-MM-DD"
tu = f"1 {timeunit}"
result = cls().execute(
session,
stem=stem,
cat=cat,
start=start,
end=end,
timeunit=tu,
datefmt=datefmt,
)
return result