Skip to main content

Weak annotators for information extraction (NER)

Project description

Weak Annotators (NER)

Experiments with weak annotators for NER using different models and methods including LLMs.

Installation

pip install git+https://github.com/imvladikon/weak_annotators.git

Usage

  1. Using UniversalNER extractor:
from weak_annotators import UniversalNerExtractor

text = """
The patient was prescribed 100 mg of aspirin daily for 3 days.
""".strip()
labels = ["DRUG", "DISEASE", "SYMPTOM", "DURATION"]
extractor = UniversalNerExtractor(labels=labels)
print(extractor(text))
# [Span(start=37, end=44, text='aspirin', label='DRUG'), Span(start=55, end=61, text='3 days', label='DURATION')]

It returns a list of Spans but if pass return_dict=True it will return a list of dictionaries:

print(extractor(text, return_dict=True))
# [{'start': 37, 'end': 44, 'text': 'aspirin', 'label': 'DRUG'}, {'start': 55, 'end': 61, 'text': '3 days', 'label': 'DURATION'}]
  1. Using medalpaca LLM:

It requires labels descriptions:

from weak_annotators import MedAlpacaExtractor

labels = ["DRUG", "DISEASE", "SYMPTOM", "DURATION"]
labels_descriptions = {
    "DRUG": "Drug or medication",
    "DISEASE": "Any disease, syndrome, or medical condition",
    "SYMPTOM": "Any symptom or sign of a disease or medical condition",
    "DURATION": "Any period of time",
}
extractor = MedAlpacaExtractor(labels=labels, labels_description=labels_descriptions)

text = """
The patient was prescribed 100 mg of aspirin daily for 3 days.
""".strip()

annotations = extractor(text)
print(annotations)

Optionally, it's possible to pass prompt_template to MedAlpacaExtractor.

prompt_template = "Extract entities of type {} from the following text:"
extractor = MedAlpacaExtractor(labels=labels, labels_description=labels_descriptions, prompt_template=prompt_template)
  1. Using flair (TARS extractor):
from weak_annotators import FlairExtractor

labels = ["DRUG", "DISEASE", "SYMPTOM", "DURATION"]
extractor = FlairExtractor(labels=labels)
print(extractor(text))

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

weak_annotators-0.0.1-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file weak_annotators-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for weak_annotators-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bbe518f38ad46c875b465b1dddd4844a6f0ae5037146fab5e25575220539f50e
MD5 d7eddbc4ae6209d738e851ea50fadd5c
BLAKE2b-256 8cb3dfb1bec1420eccfee880e05b2768dacf2c4df27bb046590d4b46f7bb7d04

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