-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OrConvQA passages datasets and LZ4 store (thanks irds)
- Loading branch information
Showing
6 changed files
with
224 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from collections import namedtuple | ||
from typing import List | ||
from experimaestro import Constant | ||
import attrs | ||
|
||
from datamaestro_text.datasets.irds.data import LZ4DocumentStore | ||
from datamaestro_text.data.ir.formats import OrConvQADocument | ||
|
||
|
||
class OrConvQADocumentStore(LZ4DocumentStore): | ||
NAMED_TUPLE = namedtuple( | ||
"OrConvQADocument", [a.name for a in attrs.fields(OrConvQADocument)] | ||
) | ||
|
||
lookup_field: Constant[str] = "id" | ||
fields: Constant[List[str]] = list(NAMED_TUPLE._fields) | ||
index_fields: Constant[List[str]] = ["id"] | ||
|
||
data_cls = NAMED_TUPLE | ||
|
||
def converter(self, data: NAMED_TUPLE) -> OrConvQADocument: | ||
return OrConvQADocument(**data._asdict()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import logging | ||
from typing import Optional, Type, Callable, Iterator | ||
from ir_datasets.indices import PickleLz4FullStore | ||
from datamaestro.download import Download | ||
from datamaestro.utils import FileChecker | ||
from pathlib import Path | ||
import urllib3 | ||
|
||
|
||
class lz4docstore_downloader(Download): | ||
"""Uses ir_datasets Lz4FullStore to build a document store for a stream of documents""" | ||
|
||
def __init__( | ||
self, | ||
varname: str, | ||
url: str, | ||
iter_factory: Callable[[Path], Iterator], | ||
doc_cls: Type, | ||
lookup_field: str, | ||
*, | ||
count_hint: Optional[int] = None, | ||
size: Optional[int] = None, | ||
checker: FileChecker = None, | ||
): | ||
super().__init__(varname) | ||
self.iter_factory = iter_factory | ||
self.url = url | ||
self.doc_cls = doc_cls | ||
self.size = size | ||
self.lookup_field = lookup_field | ||
self.count_hint = count_hint | ||
self.checker = checker | ||
|
||
p = urllib3.util.parse_url(self.url) | ||
assert p is not None | ||
self.name = Path(p.path).with_suffix("").name | ||
|
||
def prepare(self): | ||
return self.definition.datapath / self.name | ||
|
||
def download(self, force=False): | ||
# Creates directory if needed | ||
destination = self.definition.datapath / self.name | ||
destination.mkdir(exist_ok=True) | ||
|
||
# Early exit | ||
if (destination / "done").is_file() and not force: | ||
return True | ||
|
||
# Download (cache) | ||
logging.info("Building the document index") | ||
with self.context.downloadURL(self.url, size=self.size) as file: | ||
# Checks the file | ||
if self.checker: | ||
self.checker.check(file.path) | ||
|
||
# Builds the LZ4 store | ||
store = PickleLz4FullStore( | ||
destination, | ||
lambda: self.iter_factory(Path(file.path)), | ||
self.doc_cls, | ||
lookup_field=self.lookup_field, | ||
index_fields=[self.lookup_field], | ||
key_field_prefix=None, | ||
size_hint=None, | ||
count_hint=self.count_hint, | ||
) | ||
store.build() | ||
|
||
# All good! | ||
(destination / "done").touch() |