資料集文件

載入文字資料

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」載入器,這等同於使用設定了 sample_by=“document” 的「text」載入器

>>> 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.