Skip to main content

A spaCy custom component that extends the SpanRuler with a second opinion

Project description

Second Opinion Ruler

second_opinion_ruler is a spaCy component that extends SpanRuler with a second opinion. For each pattern you can provide a callback (available in registry.misc) on the matched Span - with this you can decide to discard the match, add additional spans to the match and/or mutate the matched span, e.g. add a parsed datetime to a custom attribute.

Installation

pip install second_opinion_ruler

Usage

import spacy
from spacy.tokens import Span
from spacy.util import registry

# create date as custom attribute extension
Span.set_extension("date", default=None)

# add datetime parser to registry.misc
# IMPORTANT: first argument has to be Span and the return type has to be list[Span]
@registry.misc("to_datetime.v1")
def to_datetime(span: Span, format: str, attr: str = "date") -> list[Span]:

    # parse the date
    date = datetime.datetime.strptime(span.text, format)

    # add the parsed date to the custom attribute
    span._.set(attr, date)

    # just return matched span
    return [span]

# load a model
nlp = spacy.blank("en")

# add the second opinion ruler
ruler = nlp.add_pipe("second_opinion_ruler", config={
    "validate": True,
    "annotate_ents": True,
})

# add a pattern with a second opinion handler (on_match)
ruler.add_patterns([
    {
        "label": "DATE",
        "pattern": "21.04.1986",
        "on_match": {
            "id": "to_datetime.v1",
            "kwargs": {"format": "%d.%m.%Y", "attr": "my_date"},
        },
    }
])

doc = nlp("This date 21.04.1986 will be a DATE entity while the structured information will be extracted to `Span._.extructure`")

# verify
assert doc.ents[0]._.date == datetime.datetime(1986, 4, 21)

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

second-opinion-ruler-0.1.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

second_opinion_ruler-0.1.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file second-opinion-ruler-0.1.0.tar.gz.

File metadata

  • Download URL: second-opinion-ruler-0.1.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.7 Linux/5.15.0-1020-azure

File hashes

Hashes for second-opinion-ruler-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4f113fdf822161f007a3731f489b894ad455c3df34aed1a372c0b51036a87f94
MD5 12368d3008b1512ee9972bd94dc0ee92
BLAKE2b-256 5406c830953470399dc8daa8601d0d7d54e548e6ad47f02e53728b6a31bc0a95

See more details on using hashes here.

File details

Details for the file second_opinion_ruler-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: second_opinion_ruler-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.7 Linux/5.15.0-1020-azure

File hashes

Hashes for second_opinion_ruler-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 16a2d6eddeedcf2c97445232c692ddc97a09098cb0de3487e7e88c06a33aca68
MD5 d05ef4acc9fc971c072bc06e85c8fc1a
BLAKE2b-256 66ddd3e4f8612f7a8224570e0cc6f3f31951ea8d742529d535aaf305fdeecb44

See more details on using hashes here.

Supported by

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