-
Notifications
You must be signed in to change notification settings - Fork 0
/
C0_process_infofile.py
154 lines (112 loc) · 4.06 KB
/
C0_process_infofile.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
import jsonlines
import json
import configparser
from tqdm import tqdm
from pathlib import Path
import hashlib
import datetime
"""
Update dataset_info.ini for corpus-specific information.
"""
config = configparser.ConfigParser()
config.read("dataset_info.ini")
info = dict(config["corpus"])
f_dataset = "EuroParliamentProceedings_1996_2011.jsonl"
f_compressed = "EuroParliamentProceedings_1996_2011.jsonl.zst"
# This is the specific line to show the sample, adjust for a good example
sample_row_n = 20003
def compute_hash(f, blocksize=4096):
print(f"Computing hash for {f}")
sha256_hash = hashlib.sha256()
with open(f, "rb") as FIN:
# Read and update hash string value in blocks of 4K
for byte_block in iter(lambda: FIN.read(blocksize), b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest()
def measure_file(f):
f = Path(f)
assert f.exists()
return {
"filename": str(f),
"size": f.stat().st_size,
"last_modified_UTC": int(f.stat().st_mtime),
"sha256_hash": compute_hash(f),
}
# Create an infofile if it doesn't exist, otherwise read prior values
save_dest = Path("docs")
save_dest.mkdir(exist_ok=True)
f_info = save_dest / "dataset_information.json"
f_markdown = save_dest / (Path(f_dataset).name + ".md")
if Path(f_info).exists():
with open(f_info) as FIN:
info.update(json.load(FIN))
if "file_information" not in info:
info["file_information"] = {
"uncompressed": measure_file(f_dataset),
"compressed": measure_file(f_compressed),
}
if "corpus_statistics" not in info:
print(f"Computing statistics for {f_dataset}")
stats = {
"total_documents": 0,
"total_character_count": 0,
"total_word_count": 0,
"max_character_count": 0,
"min_character_count": 10 ** 20,
"max_word_count": 0,
"min_word_count": 10 ** 20,
}
sample = {}
with jsonlines.open(f_dataset) as FIN:
for n, row in enumerate(tqdm(FIN)):
text = row["text"]
words = text.split()
stats["total_documents"] += 1
stats["total_character_count"] += len(text)
stats["total_word_count"] += len(words)
stats["max_character_count"] = max(len(text), stats["max_character_count"])
stats["min_character_count"] = min(len(text), stats["min_character_count"])
stats["max_word_count"] = max(len(words), stats["max_word_count"])
stats["min_word_count"] = min(len(words), stats["min_word_count"])
if n == sample_row_n:
sample["text"] = row["text"]
sample["meta"] = row["meta"]
sample["sample_row_n"] = sample_row_n
print(text)
info["corpus_statistics"] = stats
info["sample_data"] = sample
js = json.dumps(info, indent=2)
meta_pp = json.dumps(info["sample_data"]["meta"], indent=2)
with open(f_info, "w") as FOUT:
FOUT.write(js)
print(js)
text = info["sample_data"]["text"].strip()
# Now build a nice markdown version of the datafile
MD = f"""
# {info['title']}
| {info["short_title"]} | |
| --- | ---: |
| Reference URL | {info["url"]} |
| Replication URL | {info["replication_url"]} |
| Uncompressed file size | {info["file_information"]["uncompressed"]["size"]:,} |
| Year range | {info["min_year"]} - {info["max_year"]} |
| Primary language | {info["language"]} |
| Document count | {info["corpus_statistics"]["total_documents"]:,} |
| Word count | {info["corpus_statistics"]["total_word_count"]:,} |
| Character count | {info["corpus_statistics"]["total_character_count"]:,} |
### Sample Text (found at row {sample_row_n})
```text
{text}
```
### Sample meta information (found at row {sample_row_n})
```json
{meta_pp}
```
### SHA256 checksums
| Filename | Checksum |
| --- | :--- |
| Compressed | `{info["file_information"]["compressed"]["sha256_hash"]}` |
| Uncompressed | `{info["file_information"]["uncompressed"]["sha256_hash"]}` |
"""
with open(f_markdown, "w") as FOUT:
FOUT.write(MD)