Skip to main content

spacy-to-hf

A simple converter from SpaCy Entities (Spans) to Huggingface BILOU formatted data (tokens and ner_tags)

I've always struggled to convert my spacy formatted spans into data that can be trained on using huggingface transformers. But Spacy's Entity format is the most intuitive format for tagging entities for NER.

This repo is a simple converter that leverages spacy.gold.biluo_tags_from_offsets and the SpaCy tokenizations repo that creates a 1-line function to convert spacy formatted spans to tokens and ner_tags that can be fed into any Token Classification Transformer

Try before you buy

You can demo the functionality on streamlit or spaces

Try the app

What is "Spacy" or "HuggingFace" format?

Spacy format simply means having a text input and character level span assignments.
For example:

text = "Hello, my name is Ben"
spans = [{"start": 18, "end": 21, "label": "person"}, ...]

This is the common structure of output data from labeling tools like LabelStudio or LabelBox, because it's easy and human interpretable.

Huggingface format refers to the BIO/BILOU/BIOES tagging format commonly used for fine-tuning transformers. The input text is tokenized, and each token is given a tag to denote whether or not it's a label (and it's location, Beginning, Inside etc). Here's an example: https://huggingface.co/datasets/wikiann image

For more information about this tagging system, see wikipedia

This format is tricky, though, because it is entirely dependant on the tokenizer used. Tokens are not simply space separated words. Each tokenizer has a specific vocabulary of tokens that break down works into unique sub-words. So moving from character level spans to token level tags is a very manual process. That's a core reason I built this tool.

Installation

pip install spacy-to-hf
python -m spacy download en_core_web_sm

Usage

from spacy_to_hf import spacy_to_hf

span_data = [
    {
        "text": "I have a BSc (Bachelors of Computer Sciences) from NYU",
        "spans": [
            {"start": 9, "end": 12, "label": "degree"},
            {"start": 14, "end": 44, "label": "degree"},
            {"start": 51, "end": 54, "label": "university"}
        ]
    }
]
hf_data = spacy_to_hf(span_data, "bert-base-cased")
print(list(zip(hf_data["tokens"][0], hf_data["ner_tags"][0])))

Or, if you want to immediately start fine-tuning or upload this to huggingface, you can run

ds = spacy_to_hf(span_data, "bert-base-cased", as_hf_dataset=True)

print(ds.features["ner_tags"].feature.names)

This will return your data as a HuggingFace Dataset and will automatically string-index your ner_tags into a ClassLabel object

Project Setup

Project setup is credited to @anthonycorletti and his awesome project template repo

Download files

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

Source Distribution

spacy-to-hf-0.0.3.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spacy_to_hf-0.0.3-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file spacy-to-hf-0.0.3.tar.gz.

File metadata

  • Download URL: spacy-to-hf-0.0.3.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for spacy-to-hf-0.0.3.tar.gz
Algorithm Hash digest
SHA256 6726bf8e82eaba3463b53c28ece10d6ac3f55eed9c7341604b6b4c66e41f9a1c
MD5 c57c7f60a0d29af3c51fd71395f735c2
BLAKE2b-256 23c81cc69ac1d7d8671b71da95e262bdb488c3f48d21b65991109976749adaa0

See more details on using hashes here.

File details

Details for the file spacy_to_hf-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: spacy_to_hf-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for spacy_to_hf-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 08ecb3e876cc23aab43145045a122729b2a6b0cdb86ecd86506f25bb8e3ea5cc
MD5 2b25ef9424f6ce533452708c97d39aa5
BLAKE2b-256 34d7729123cc782eff079ab61b02dfeed91ade61e3c663ca84b90c62c1d7d261

See more details on using hashes here.

Release history Release notifications | RSS feed

0.0.4

2 files

This release

0.0.3 This release

2 files

0.0.2

2 files

0.0.1

2 files

0.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page