#!/usr/bin/python3 # -*- coding: utf-8 -*- import argparse from collections import Counter from datasets import load_dataset, DownloadMode import matplotlib.pyplot as plt from tqdm import tqdm from project_settings import project_path def get_args(): parser = argparse.ArgumentParser() parser.add_argument("--dataset_name", default="amazon_reviews_multi", type=str) parser.add_argument( "--dataset_cache_dir", default=(project_path / "hub_datasets").as_posix(), type=str ) args = parser.parse_args() return args def main(): args = get_args() dataset = load_dataset( "../language_identification.py", name="amazon_reviews_multi", split="train", cache_dir=args.dataset_cache_dir, # download_mode=DownloadMode.FORCE_REDOWNLOAD ) counter1 = Counter() counter2 = Counter() for sample in tqdm(dataset): text = sample["text"] language = sample["language"] text_length = len(text) text_length_round = int(text_length / 10) * 10 text_length_round = 200 if text_length_round > 200 else text_length_round counter1.update([language]) counter2.update([text_length_round]) print("语种数量:") for k, v in counter1.most_common(): print("{}: {}".format(k, v)) print("文本长度:") counter2 = list(sorted(counter2.items(), key=lambda x: x[0])) x = [item[0] for item in counter2] y = [item[1] for item in counter2] for k, v in counter2: text_length_range = "{}-{}".format(k, k+10) print("{}: {}".format(text_length_range, v)) plt.plot(x, y) plt.savefig("{}_text_length.jpg".format(args.dataset_name)) plt.show() return if __name__ == "__main__": main()