#!/usr/bin/python3 # -*- coding: utf-8 -*- import argparse from collections import Counter, defaultdict 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="tatoeba", 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=args.dataset_name, split="train", cache_dir=args.dataset_cache_dir, # download_mode=DownloadMode.FORCE_REDOWNLOAD ) counter1 = Counter() counter2 = Counter() examples = defaultdict(list) examples_counter = defaultdict(int) 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]) if examples_counter[language] < 3: examples[language].append(text) examples_counter[language] += 1 print("\n") print("语种数量:") for k, v in counter1.most_common(): print("{}: {}".format(k, v)) print("\n") print("样本示例:") print("\n") print("| 数据 | 语种 | 样本 |") print("| :---: | :---: | :---: |") for language, text_list in examples.items(): for text in text_list: row = "| {} | {} | {} |".format(args.dataset_name, language, text) print(row) print("\n") 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) image_file = project_path / "docs/picture/{}_text_length.jpg".format(args.dataset_name) plt.savefig(image_file.as_posix()) plt.show() return if __name__ == "__main__": main()