File size: 1,988 Bytes
beaaafa ee0c069 beaaafa |
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 |
import argparse
import csv
from collections import defaultdict
import json
from tqdm import tqdm
import smart_open
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Calculate instructions stats for a large JSONL file."
)
parser.add_argument("input_file", help="path to input JSONL file")
parser.add_argument("output_file", help="path to output CSV file")
args = parser.parse_args()
stats = defaultdict(lambda: defaultdict(int))
with smart_open.open(args.input_file, "rt", encoding="utf-8") as reader:
for item in tqdm(map(json.loads, reader)):
stats[item["language"]][f"{item['payload']['@type']} count"] += 1
stats[item["language"]][f"{item['payload']['@type']} text length"] += item[
"text_length"
]
stats[item["language"]]["items count"] += 1
stats[item["language"]]["text length"] += item["text_length"]
with smart_open.open(args.output_file, "wt", encoding="utf-8") as writer:
w = csv.DictWriter(
writer,
fieldnames=[
"language",
"FAQPage count",
"FAQPage text length",
"HowTo count",
"HowTo text length",
"items count",
"text length",
],
)
w.writeheader()
for language, language_stats in stats.items():
w.writerow(
{
"language": language,
"FAQPage count": language_stats["FAQPage count"],
"FAQPage text length": language_stats["FAQPage text length"],
"HowTo count": language_stats["HowTo count"],
"HowTo text length": language_stats["HowTo text length"],
"items count": language_stats["items count"],
"text length": language_stats["text length"],
}
)
|