Datasets 文件

載入文字資料

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

載入文字資料

本指南向您展示如何載入文字資料集。要了解如何載入任何型別的資料集,請參閱通用載入指南

文字檔案是儲存資料集最常見的檔案型別之一。預設情況下,🤗 Datasets 會逐行對文字檔案進行取樣來構建資料集。

>>> 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 引數

# 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 模式來載入特定檔案

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

要透過 HTTP 載入遠端文字檔案,請改為傳遞 URL

>>> dataset = load_dataset("text", data_files="https://huggingface.co/datasets/hf-internal-testing/dataset_with_data_files/resolve/main/data/train.txt")

要載入 XML 資料,您可以使用“xml”載入器,它等同於“text”並設定 sample_by=“document”

>>> from datasets import load_dataset
>>> dataset = load_dataset("xml", data_files={"train": ["my_xml_1.xml", "my_xml_2.xml"], "test": "my_xml_file.xml"})

# Load from a directory
>>> dataset = load_dataset("xml", data_dir="path/to/xml/dataset")
< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.