Skip to main content

spacy-to-hf

A simple converter from Spacy Entities to Huggingface BILOU formatted data

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 ner_tags that can be fed into any Token Classification Transformer

Installation

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

Usage

from spacy_to_hf import spacy_to_hf
from datasets import Dataset

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")
list(zip(hf_data["tokens"][0], hf_data["ner_tags"][0]))
ds = Dataset.from_dict(hf_data)

From here, you can label-index your ner_tags and prepare for fine-tuning

Project Setup

Project setup is credited to @anthonycorletti and his awesome repo https://github.com/anthonycorletti/python-project-template

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.0.tar.gz (9.3 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.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for spacy-to-hf-0.0.0.tar.gz
Algorithm Hash digest
SHA256 cfff0888951a13f239b5a112117f76c97db16fb42d9358117b53522888ef9d15
MD5 c12cd8d439ff825d8b6280332c2d6b64
BLAKE2b-256 35d5f83587e0eb2cb1fe058746278ce8b5b6421d1d04b664d2a0bf594bba33f9

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for spacy_to_hf-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c46c8fb1fb179f79d0ca00deb38e3802e088e5aaae28ccd3d93554106b164b9b
MD5 59c78226c172dc1218cd25a8ce6969e8
BLAKE2b-256 ee99d5aa2a6aecbe22aad503dfb23fc7b1ec8579e2c45f6b016c2cdd0a53124d

See more details on using hashes here.

Release history Release notifications | RSS feed

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

This release

0.0.0 This release

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