Skip to main content

Data set loading and annotation facilities for the Simple Annotation Framework

Project description

SAF-Datasets

Dataset loading and annotation facilities for the Simple Annotation Framework

The saf-datasets library provides easy access to Natural Language Processing (NLP) datasets, and tools to facilitate annotation at document, sentence and token levels.

It is being developed to address a need for flexibility in manipulating NLP annotations that is not entirely covered by popular dataset libraries, such as HuggingFace Datasets and torch Datasets, Namely:

  • Including and modifying annotations on existing datasets.
  • Standardized API.
  • Support for complex and multi-level annotations.

saf-datasets is built upon the Simple Annotation Framework (SAF) library, which provides its data model and API.

It also provides annotator classes to automatically label existing and new datasets.

Installation

To install, you can use pip:

pip install saf-datasets

Usage

Loading datasets

from saf_datasets import STSBDataSet

dataset = STSBDataSet()
print(len(dataset))  # Size of the dataset
# 17256
print(dataset[0].surface)  # First sentence in the dataset
# A plane is taking off
print([token.surface for token in dataset[0].tokens])  # Tokens (SpaCy) of the first sentence.
# ['A', 'plane', 'is', 'taking', 'off', '.']
print(dataset[0].annotations)  # Annotations for the first sentence
# {'split': 'train', 'genre': 'main-captions', 'dataset': 'MSRvid', 'year': '2012test', 'sid': '0001', 'score': '5.000', 'id': 0}

# There are no token annotations in this dataset
print([(tok.surface, tok.annotations) for tok in dataset[0].tokens])
# [('A', {}), ('plane', {}), ('is', {}), ('taking', {}), ('off', {}), ('.', {})]

Available datasets: AllNLI, CODWOE, CPAE, EntailmentBank, STSB, Wiktionary, WordNet (Filtered).

Annotating datasets

from saf_datasets import STSBDataSet
from saf_datasets.annotators import SpacyAnnotator

dataset = STSBDataSet()
annotator = SpacyAnnotator()  # Needs spacy and en_core_web_sm to be installed.
annotator.annotate(dataset)

# Now tokens are annotated
for tok in dataset[0].tokens:
    print(tok.surface, tok.annotations)

# A {'pos': 'DET', 'lemma': 'a', 'dep': 'det', 'ctag': 'DT'}
# plane {'pos': 'NOUN', 'lemma': 'plane', 'dep': 'nsubj', 'ctag': 'NN'}
# is {'pos': 'AUX', 'lemma': 'be', 'dep': 'aux', 'ctag': 'VBZ'}
# taking {'pos': 'VERB', 'lemma': 'take', 'dep': 'ROOT', 'ctag': 'VBG'}
# off {'pos': 'ADP', 'lemma': 'off', 'dep': 'prt', 'ctag': 'RP'}
# . {'pos': 'PUNCT', 'lemma': '.', 'dep': 'punct', 'ctag': '.'}

Using with other libraries

saf-datasets provides wrappers for using the datasets with libraries expecting HF or torch datasets:

from saf_datasets import CPAEDataSet
from saf_datasets.wrappers.torch import TokenizedDataSet
from transformers import AutoTokenizer

dataset = CPAEDataSet()
tokenizer = AutoTokenizer.from_pretrained("gpt2", padding_side="left", add_prefix_space=True)
tok_ds = TokenizedDataSet(dataset, tokenizer, max_len=128, one_hot=False)
print(tok_ds[:10])
# tensor([[50256, 50256, 50256,  ...,  2263,   572,    13],
#         [50256, 50256, 50256,  ...,  2263,   572,    13],
#         [50256, 50256, 50256,  ...,   781,  1133,    13],
#         ...,
#         [50256, 50256, 50256,  ...,  2712, 19780,    13],
#         [50256, 50256, 50256,  ...,  2685,    78,    13],
#         [50256, 50256, 50256,  ...,  2685,    78,    13]])

print(tok_ds[:10].shape)
# torch.Size([10, 128])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

saf_datasets-0.6.8.tar.gz (30.4 kB view details)

Uploaded Source

Built Distribution

saf_datasets-0.6.8-py3-none-any.whl (37.8 kB view details)

Uploaded Python 3

File details

Details for the file saf_datasets-0.6.8.tar.gz.

File metadata

  • Download URL: saf_datasets-0.6.8.tar.gz
  • Upload date:
  • Size: 30.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for saf_datasets-0.6.8.tar.gz
Algorithm Hash digest
SHA256 b97a04829d17b974a2c61b1b83f0884ae9742353a8b62b16ed58a74fe6e2d559
MD5 62d8ee179a32d73ab298b616795c0e27
BLAKE2b-256 c33e4170a7e42d51bce0705ff72040013ebc21dfd36c30aa165d2efd5be5f82d

See more details on using hashes here.

File details

Details for the file saf_datasets-0.6.8-py3-none-any.whl.

File metadata

  • Download URL: saf_datasets-0.6.8-py3-none-any.whl
  • Upload date:
  • Size: 37.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for saf_datasets-0.6.8-py3-none-any.whl
Algorithm Hash digest
SHA256 3d9005633901a290057c151b0ad311ec262bba074e7b6220e9386c7f165ab192
MD5 0d9a8892912c1fdd179313784ba3713b
BLAKE2b-256 958c334bcf752ce06cd88290c5adf593707b37c98a398381a8be892d6b2dc324

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page