|
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"], |
|
} |
|
) |
|
|