-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
603 lines (530 loc) Β· 16.1 KB
/
__init__.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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
from dataclasses import dataclass, field
from datetime import datetime, timezone
from functools import cached_property
from hashlib import md5
from pathlib import Path
from typing import Sequence, Any
from urllib.parse import SplitResult, urlsplit
from uuid import UUID, uuid5, NAMESPACE_URL
from dataclasses_json import DataClassJsonMixin, config
from marshmallow.fields import List, Nested, String, Field
from archive_query_log.model.highlight import HighlightedText
from archive_query_log.util.serialization import HighlightedTextField
@dataclass(frozen=True, slots=True)
class ArchivedUrl(DataClassJsonMixin):
"""
A URL that is archived in the Wayback Machine (https://web.archive.org/).
The archived snapshot can be retrieved using the ``archive_url``
and ``raw_archive_url`` properties.
Output of: 2-archived-urls
Input of: 3-archived-query-urls
"""
url: str
"""
Original URL that was archived.
"""
timestamp: int
"""
Timestamp of the archived snapshot in the Wayback Machine.
"""
@cached_property
def id(self) -> UUID:
"""
Unique ID for this archived URL.
"""
return uuid5(NAMESPACE_URL, f"{self.timestamp}:{self.url}")
@cached_property
def split_url(self) -> SplitResult:
"""
Original URL split into its components.
"""
return urlsplit(self.url)
@cached_property
def url_domain(self) -> str:
"""
Domain of the original URL.
"""
return self.split_url.netloc
@cached_property
def url_md5(self) -> str:
"""
MD5 hash of the original URL.
"""
return md5(self.url.encode()).hexdigest()
@cached_property
def datetime(self) -> datetime:
"""
Snapshot timestamp as a ``datetime`` object.
"""
return datetime.fromtimestamp(self.timestamp, timezone.utc)
@cached_property
def archive_timestamp(self) -> str:
"""
Snapshot timestamp as a string in the format used
by the Wayback Machine (``YYYYmmddHHMMSS``).
"""
return f"{self.datetime.year:04d}{self.datetime.month:02d}" \
f"{self.datetime.day:02d}{self.datetime.hour:02d}" \
f"{self.datetime.minute:02d}{self.datetime.second:02d}"
@property
def archive_url(self) -> str:
"""
URL of the archived snapshot in the Wayback Machine.
"""
return f"https://web.archive.org/web/" \
f"{self.archive_timestamp}/{self.url}"
@property
def raw_archive_url(self) -> str:
"""
URL of the archived snapshot's raw contents in the Wayback Machine.
"""
return f"https://web.archive.org/web/" \
f"{self.archive_timestamp}id_/{self.url}"
@dataclass(frozen=True, slots=True)
class ArchivedQueryUrl(ArchivedUrl, DataClassJsonMixin):
"""
Archived URL of a search engine result page (SERP) for a query.
Output of: 3-archived-query-urls
Input of: 4-archived-raw-serps, 8-ir-corpus
"""
query: str
"""
Query that was used to retrieve the SERP.
"""
page: int | None
"""
Page number of the SERP, e.g., 1, 2, 3.
Note: the page number should be zero-indexed, i.e.,
the first result page has the page number 0.
See also: ``results_page_offset``.
"""
offset: int | None
"""
Offset (start position) of the first result in the SERP, e.g., 10, 20.
Note: the offset should be zero-indexed, i.e.,
the first result page has the offset 0.
See also ``results_page``.
"""
@dataclass(frozen=True, slots=True)
class ArchivedRawSerp(ArchivedQueryUrl, DataClassJsonMixin):
"""
Raw snapshot content of an archived SERP.
Output of: 4-archived-raw-serps
Input of: 5-archived-parsed-serps
"""
content: bytes
"""
Raw byte content of the archived SERP's snapshot.
"""
encoding: str
"""
Encoding of the archived SERP's snapshot.
"""
@dataclass(frozen=True, slots=True)
class ArchivedSearchResultSnippet(ArchivedUrl, DataClassJsonMixin):
"""
Single retrieved result from a query's archived SERP.
"""
rank: int
"""
Rank of the result in the SERP.
"""
title: HighlightedText | str = field(metadata=config(
encoder=str,
decoder=HighlightedText,
mm_field=HighlightedTextField(),
))
"""
Title of the result with optional highlighting.
"""
snippet: HighlightedText | str | None = field(metadata=config(
encoder=str,
decoder=HighlightedText,
mm_field=HighlightedTextField(allow_none=True),
))
"""
Snippet of the result.
Highlighting should be normalized to ``<em>`` tags.
Other HTML tags are removed.
"""
@cached_property
def id(self) -> UUID:
"""
Unique ID for this archived URL.
"""
return uuid5(NAMESPACE_URL, f"{self.rank}:{self.timestamp}:{self.url}")
@dataclass(frozen=True, slots=True)
class ArchivedParsedSerp(ArchivedQueryUrl, DataClassJsonMixin):
"""
Archived search engine result page (SERP) corresponding to a query.
Output of: 5-archived-parsed-serps
Input of: 6-archived-raw-search-results, 8-ir-corpus
"""
results: Sequence[ArchivedSearchResultSnippet] = field(
metadata=config(
encoder=list,
decoder=tuple,
mm_field=List(Nested(ArchivedSearchResultSnippet.schema()))
)
)
"""
Retrieved results from the SERP in the same order as they appear.
"""
interpreted_query: str | None
"""
Interpreted query that is displayed or otherwise included in the SERP.
Note: the interpreted result query can be different from the original query
due to spelling correction etc.
"""
@dataclass(frozen=True, slots=True)
class ArchivedRawSearchResult(ArchivedSearchResultSnippet, DataClassJsonMixin):
"""
Raw content of an archived search result.
Output of: 6-archived-raw-search-results
Input of: 7-archived-parsed-search-results
"""
content: bytes
"""
Raw byte content of the archived search result's snapshot.
"""
encoding: str
"""
Encoding of the archived search result's snapshot.
"""
@dataclass(frozen=True, slots=True)
class ArchivedParsedSearchResult(ArchivedSearchResultSnippet,
DataClassJsonMixin):
"""
Parsed representation of an archived search result.
Output of: 7-archived-parsed-search-results
Input of: 8-ir-corpus
"""
content_title: str | None
"""
Title of the archived SERP's snapshot content.
Note: the content title can be different from the snippet title due to ellipses etc.
"""
content_plaintext: str | None
"""
Plaintext of the archived SERP's snapshot content.
"""
# TODO
pass
# flake8: noqa: E402
from archive_query_log.model.parse import QueryParser, \
PageParser, OffsetParser, QueryParserField, PageOffsetParserField, \
ResultsParserField, InterpretedQueryParserField, InterpretedQueryParser, \
ResultsParser
@dataclass(frozen=True, slots=True)
class Service(DataClassJsonMixin):
"""
An online service that has a search interface.
Output of: service collection, service domains
Input of: service URLs, query extraction
"""
name: str
"""
Service name (corresponds to ``alexa_domain`` without
the ``alexa_public_suffix``).
"""
public_suffix: str
"""
Public suffix (https://publicsuffix.org/) of ``alexa_domain``.
"""
alexa_domain: str
"""
Domain as it appears in Alexa top-1M ranks.
"""
alexa_rank: int | None
"""
Rank from fused Alexa top-1M rankings.
"""
category: str | None
"""
Category of the service (manual annotation).
"""
notes: str | None
"""
Notes about the service (manual annotation).
"""
input_field: bool | None
"""
Whether the service has an input field.
"""
search_form: bool | None
"""
Whether the service has a search form element.
"""
search_div: bool | None
"""
Whether the service has a search div element.
"""
domains: Sequence[str] = field(
metadata=config(
decoder=tuple,
mm_field=List(String())
)
)
"""
Known domains of the service, including the main domain.
"""
query_parsers: Sequence[QueryParser] = field(
metadata=config(
decoder=tuple,
mm_field=List(QueryParserField())
)
)
"""
Query parsers in order of precedence.
"""
page_parsers: Sequence[PageParser] = field(
metadata=config(
decoder=tuple,
mm_field=List(PageOffsetParserField())
)
)
"""
Page number parsers in order of precedence.
"""
offset_parsers: Sequence[OffsetParser] = field(
metadata=config(
decoder=tuple,
mm_field=List(PageOffsetParserField())
)
)
"""
Page number parsers in order of precedence.
"""
interpreted_query_parsers: Sequence[InterpretedQueryParser] = field(
metadata=config(
decoder=tuple,
mm_field=List(InterpretedQueryParserField())
)
)
"""
Interpreted query parsers in order of precedence.
The interpreted query is the query that is displayed
or otherwise included in the SERP.
Note: the interpreted result query can be different from the original query
due to spelling correction etc.
"""
results_parsers: Sequence[ResultsParser] = field(
metadata=config(
decoder=tuple,
mm_field=List(ResultsParserField())
)
)
"""
SERP parsers in order of precedence.
"""
focused_url_prefixes: Sequence[str] = field(
metadata=config(
decoder=tuple,
mm_field=List(String())
)
)
"""
URL prefixes for a more focused pipeline which might miss some queries
but executes faster.
"""
class PathField(Field):
def _serialize(
self, value: Any, attr: str | None, obj: Any, **kwargs
) -> str:
return str(value)
def _deserialize(
self, value: str, attr: str, data: Any, **kwargs: Any
) -> Path:
return Path(value)
@dataclass(frozen=True, slots=True)
class CorpusJsonlLocation(DataClassJsonMixin):
relative_path: Path = field(
metadata=config(
encoder=str,
decoder=Path,
mm_field=PathField(),
)
)
"""
Path of the JSONL file relative to the corpus root path.
"""
byte_offset: int
"""
Position of the JSONL line's first byte in the decompressed JSONL file.
"""
@dataclass(frozen=True, slots=True)
class CorpusJsonlSnippetLocation(CorpusJsonlLocation, DataClassJsonMixin):
index: int
"""
Index of the snippet in the JSONL file entry's results list.
"""
@dataclass(frozen=True, slots=True)
class CorpusWarcLocation(DataClassJsonMixin):
relative_path: Path = field(
metadata=config(
encoder=str,
decoder=Path,
mm_field=PathField(),
)
)
"""
Path of the WARC file relative to the corpus root path.
"""
byte_offset: int
"""
Position of the WARC record's first byte in the compressed WARC file.
"""
@dataclass(frozen=True, slots=True)
class CorpusQueryUrl(DataClassJsonMixin):
id: UUID
"""
Unique ID based on the URL and timestamp.
"""
url: str
"""
Original URL that was archived.
"""
timestamp: int
"""
POSIX timestamp of the URL's archived snapshot in the Wayback Machine.
"""
wayback_url: str
"""
URL to access the archived snapshot in the Wayback Machine.
"""
wayback_raw_url: str
"""
URL to access the archived snapshot's raw contents in the Wayback Machine.
"""
url_query: str | None
"""
Query that was parsed from the URL.
"""
url_page: int | None
"""
SERP page number that was parsed from the URL, e.g., 1, 2, 3.
Note: the page number should be zero-indexed, i.e.,
the first result page has the page number 0.
"""
url_offset: int | None
"""
SERP results offset (start position) that was parsed from the URL,
e.g., 10, 20.
Note: the offset should be zero-indexed, i.e.,
the first result page has the offset 0.
"""
serp_query: str | None
"""
Interpreted query as displayed on (or otherwise included in) the SERP.
Note: the interpreted result query can be different from the original query
due to spelling correction etc.
"""
archived_url_location: CorpusJsonlLocation
"""
Location of the corresponding URL entry and JSONL file.
"""
archived_query_url_location: CorpusJsonlLocation | None
"""
Location of the corresponding query URL entry and JSONL file.
"""
archived_raw_serp_location: CorpusWarcLocation | None
"""
Location of the corresponding raw SERP entry and WARC file.
"""
archived_parsed_serp_location: CorpusJsonlLocation | None
"""
Location of the corresponding parsed SERP entry in the JSONL file.
"""
@dataclass(frozen=True, slots=True)
class CorpusSearchResult(DataClassJsonMixin):
id: UUID
"""
Unique ID for this archived URL.
"""
url: str
"""
Original URL that was archived.
"""
timestamp: int
"""
POSIX timestamp of the archived snapshot in the Wayback Machine.
Note that there might not be a snapshot for the exact timestamp,
but the Wayback Machine will instead redirect
to the nearest available snapshot.
"""
wayback_url: str
"""
URL of the archived snapshot in the Wayback Machine.
Note that there might not be a snapshot for the exact timestamp,
but the Wayback Machine will instead redirect
to the nearest available snapshot.
"""
wayback_raw_url: str
"""
URL of the archived snapshot's raw contents in the Wayback Machine.
Note that there might not be a snapshot for the exact timestamp,
but the Wayback Machine will instead redirect
to the nearest available snapshot.
"""
snippet_rank: int
"""
Rank of the result in the SERP.
"""
snippet_title: HighlightedText | str = field(metadata=config(
encoder=str,
decoder=HighlightedText,
mm_field=HighlightedTextField(),
))
"""
Snippet title of the result with optional highlighting.
Highlighting should be normalized to ``<em>`` tags.
Other HTML tags are removed.
"""
snippet_text: HighlightedText | str | None = field(metadata=config(
encoder=str,
decoder=HighlightedText,
mm_field=HighlightedTextField(allow_none=True),
))
"""
Snippet text of the result with optional highlighting.
Highlighting should be normalized to ``<em>`` tags.
Other HTML tags are removed.
"""
archived_snippet_location: CorpusJsonlSnippetLocation
"""
Location of the corresponding snippet entry in the JSONL file.
"""
archived_raw_search_result_location: CorpusWarcLocation | None
"""
Location of the corresponding raw search result entry and WARC file.
"""
archived_parsed_search_result_location: CorpusJsonlLocation | None
"""
Location of the corresponding parsed search result entry in the JSONL file.
"""
@dataclass(frozen=True, slots=True)
class CorpusQuery(CorpusQueryUrl, DataClassJsonMixin):
service: str
"""
Name of the search engine service from which the query was fetched.
"""
results: Sequence[CorpusSearchResult] | None = field(
metadata=config(
encoder=list,
decoder=tuple,
mm_field=List(Nested(CorpusSearchResult.schema()))
)
)
"""
Retrieved results from the SERP in the same order as they appear.
"""
@dataclass(frozen=True, slots=True)
class CorpusDocument(CorpusSearchResult, DataClassJsonMixin):
service: str
"""
Name of the search engine service from which the snippet was fetched.
"""
query: CorpusQueryUrl
"""
Query and SERP that was used to retrieve this result.
"""