Skip to main content

spacy-redefiner

A spaCy pipeline component that finds the entities you define in a plain-English rulebook, using ReDefiNER.

ReDefiNER is an open model (0.8B parameters, Apache 2.0) that marks the spans of text a rulebook defines: entity types, a one-line description each, and optional rules. To change what is found, edit the rulebook; no training data and no retraining. It is non-generative, runs on CPU or GPU, and supports English only.

Install

pip install spacy-redefiner

The first document downloads the model weights (about 0.8 GB) from Hugging Face.

Quickstart

import spacy

RULEBOOK = """[TYPE] PERSON
[DESCRIPTION] A person's name.

[TYPE] ORGANIZATION
[DESCRIPTION] A company, institution or society.
"""

nlp = spacy.blank("en")
nlp.add_pipe("redefiner", config={"rulebook": RULEBOOK})
doc = nlp("Ada Lovelace corresponded with the Royal Society.")
print([(ent.text, ent.label_, ent._.redefiner_confidence) for ent in doc.ents])
# [('Ada Lovelace', 'PERSON', 0.99), ('Royal Society', 'ORGANIZATION', 0.891)]

The component also works after a trained spaCy pipeline, for example spacy.load("en_core_web_sm"). By default it replaces the pipeline's entities; set overwrite_ents to false to keep them and add only new, non-overlapping ones.

Personal data

jungroo-ai/redefiner-pii holds a ready-made PII rulebook (20 types) and its settings. With it, no rulebook is needed:

nlp = spacy.blank("en")
nlp.add_pipe("redefiner", config={"model": "jungroo-ai/redefiner-pii"})
doc = nlp("Call Meera Nair on 020 7946 0155 or write to meera.nair@example.org.")
print([(ent.text, ent.label_) for ent in doc.ents])
# [('Meera Nair', 'PERSON'), ('020 7946 0155', 'PHONE'), ('meera.nair@example.org', 'EMAIL')]

The model can miss personal data and can mark text that is not personal. Check its output on your own documents before relying on it, and don't use it as the only safeguard.

Writing a rulebook

A rulebook has one [TYPE] section per entity type with a [DESCRIPTION] line, and can add rule lines, a [RULES] section for rules that apply to every type, and a [TYPE] NOT_AN_ENTITY section for what must never be found. Descriptions matter: one line per type does much better than type names alone. See the rulebook guide and the templates.

rulebook takes the rulebook text or the path of a rulebook file.

Settings

Setting Default Meaning
model "jungroo-ai/redefiner-0.8b" A Hugging Face repo or local folder. A page such as jungroo-ai/redefiner-pii brings its own rulebook and settings
rulebook null Rulebook text or a file path. Needed unless the model page has a default rulebook
spans_key null Write the spans to doc.spans[spans_key] instead of doc.ents
overwrite_ents true With doc.ents: replace existing entities, or (false) keep them and add non-overlapping spans
min_confidence 0.0 Drop spans below this confidence
threshold model default (0.5) Span score needed before a span gets a type
none_bias model default (2) Higher finds more borderline spans
max_span_tokens model default Longest span in model tokens (16 by default; 64 on the PII page). Raise it for long IDs or URLs
device null "cpu", "cuda" or "cuda:N"; default: a GPU when PyTorch can use one
revision null Hugging Face revision of the model
batch_size 16 Texts per model forward pass
alignment_mode "expand" How model spans snap to spaCy tokens: "strict", "contract" or "expand"

In a spaCy config file:

[components.redefiner]
factory = "redefiner"
rulebook = "rules/contracts.txt"
spans_key = "redefiner"

Output

  • doc.ents, or doc.spans[spans_key] when a key is set.
  • Each span's confidence is in span._.redefiner_confidence. A span group also keeps the confidences in group.attrs["scores"], as spaCy's span categorizer does.
  • nlp.to_disk() saves the rulebook text with the pipeline, so a saved pipeline does not depend on the rulebook file.
  • Components with the same model and settings share one loaded model.

Speed and limits

  • On an Apple M2 CPU, the first document with a new rulebook took about 7 seconds while the rulebook was read; it is then cached, and later short texts take under a second. A GPU is much faster for large batches.
  • English only.
  • Long texts are read in parts of 256 tokens, so the same name can get different labels in different parts.
  • Rules that go against what the model already knows, such as calling a person's name something other than a person, are mostly ignored. Try a rulebook on a few dozen of your own texts first.

Development

pip install -e ".[dev]"
pytest              # fast tests with a fake model
pytest -m slow      # the real model on CPU

Licence

Apache License 2.0. See LICENSE. Made by Jungroo AI Labs.

Metadata

Release files for spacy-redefiner 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spacy-redefiner 0.1.0
File Size Uploaded Provenance Actions
spacy_redefiner-0.1.0.tar.gz 12.4 kB Without provenance Details for spacy_redefiner-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for spacy-redefiner 0.1.0
File Interpreter ABI Platform Actions
spacy_redefiner-0.1.0-py3-none-any.whl Python 3 none any Details for spacy_redefiner-0.1.0-py3-none-any.whl

Total release size: 23.8 kB

Release files / spacy_redefiner-0.1.0.tar.gz

Download URL spacy_redefiner-0.1.0.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
45014ca95c938af9933866bb326bc1f1daed4163b3a97b4188cf28598e0e3921
BLAKE2b-256 checksum
How to use checksums
358a5d54743c23f89e8f82ad1bf56ec400b7a8a98817ae8bf92249efb9d0a98f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / spacy_redefiner-0.1.0-py3-none-any.whl

Download URL spacy_redefiner-0.1.0-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
abac80c94fe585f7efe26f7321200616d87a5c892cfb465a2d1f9eba14aa0404
BLAKE2b-256 checksum
How to use checksums
715d8cad421932ffb4a927ea10d294892fc24ddca7eeb02c3e797a592a84ea52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Security for spacy-redefiner 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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