Datasets documentation

Tải dữ liệu văn bản

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v4.8.4).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Tải dữ liệu văn bản

Hướng dẫn này cho bạn cách tải các bộ dữ liệu văn bản. Để tìm hiểu cách tải bất kỳ loại bộ dữ liệu nào, hãy xem hướng dẫn tải chung.

File văn bản là một trong những định dạng file phổ biến nhất để lưu trữ một tập dữ liệu. Mặc định, 🤗 Datasets lấy mẫu file văn bản theo từng dòng để xây dựng tập dữ liệu.

>>> 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")

Để lấy mẫu một tệp văn bản theo đoạn hoặc thậm chí toàn bộ tài liệu, hãy sử dụng tham số 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")

Bạn cũng có thể sử dụng mẫu grep để tải các tệp cụ thể:

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

Để tải các tệp văn bản từ xa qua HTTP, hãy truyền các URL thay vào đó:

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

Để nạp dữ liệu XML, bạn có thể sử dụng trình nạp “xml”, tương đương với “text” có 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")
Update on GitHub