前言

这份指南向您展示了如何加载文本数据集。要想了解如何加载任何类型的数据集,请查阅通用加载指南

文本文件是存储数据集最常见的文件类型之一。默认情况下,🤗 Datasets 逐行采样文本文件以构建数据集。

1
2
3
4
5
>>> from datasets import load_dataset
>>> dataset = load_dataset("text", data_files={"train": ["my_text_1.txt", "my_text_2.txt"], "test": "my_test_file.txt"})

# Load from a directory
>>> dataset = load_dataset("text", data_dir="path/to/text/dataset")

要按段落甚至整个文档采样文本文件,请使用 sample_by 参数:

1
2
3
4
5
# Sample by paragraph
>>> dataset = load_dataset("text", data_files={"train": "my_train_file.txt", "test": "my_test_file.txt"}, sample_by="paragraph")

# Sample by document
>>> dataset = load_dataset("text", data_files={"train": "my_train_file.txt", "test": "my_test_file.txt"}, sample_by="document")

你也可以使用grep模式来加载特定的文件:

1
2
>>> from datasets import load_dataset
>>> c4_subset = load_dataset("allenai/c4", data_files="en/c4-train.0000*-of-01024.json.gz")

要通过HTTP加载远程文本文件,请传递URL:

1
>>> dataset = load_dataset("text", data_files="https://huggingface.co/datasets/lhoestq/test/resolve/main/some_text.txt")

Operating System: Ubuntu 22.04.4 LTS

参考文档

  1. Load text data

File-based builders

结语

第一百九十六篇博文写完,开心!!!!

今天,也是充满希望的一天。