-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #283 from centre-for-humanities-computing/lexdk
Add lexdk dataset
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import datetime | ||
import gzip | ||
import io | ||
import json | ||
import tarfile | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
|
||
def main(): | ||
input_path = Path("/work/raw_datasets/lexdk-raw.tar.gz") | ||
output_path = Path("/work/pre-training/lexdk/documents/lexdk_articles.jsonl.gz") | ||
|
||
with tarfile.open(input_path, "r:gz") as inputfile, gzip.open( | ||
output_path, | ||
"wt", | ||
) as outputfile: | ||
extracted = inputfile.extractfile("lexdk_data/articles.jsonl") | ||
assert extracted is not None | ||
count = 0 | ||
for idx, line in enumerate(io.TextIOWrapper(extracted, encoding="UTF-8")): | ||
obj = json.loads(line) | ||
|
||
metadata = { | ||
"url": obj["url"], | ||
"title": obj["title"], | ||
"authors": obj["authors"], | ||
"clarification": obj["clarification"], | ||
} | ||
|
||
created = datetime.datetime.strptime(obj["date"], "%Y-%m-%dT%H:%M:%SZ").strftime("%Y-%m-%d") | ||
|
||
new_obj: dict[str, Union[dict[str, str], str]] = { | ||
"id": str(idx), | ||
"text": obj["text"], | ||
"source": "lexdk", | ||
"added": datetime.datetime.now().strftime("%Y-%m-%d"), | ||
"created": created + ", " + created, | ||
"metadata": metadata, | ||
} | ||
json.dump(new_obj, outputfile) | ||
outputfile.write("\n") | ||
count += 1 | ||
print("Wrote %d documents to %s" % (count, output_path)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |