Sequence Tagger for Partially Annotated Dataset in spaCy
Project description
spacy-partial-tagger
This is a library to build a CRF tagger for a partially annotated dataset in spaCy. You can build your own NER tagger only from dictionary. The algorithm of this tagger is based on Effland and Collins. (2021).
Overview
Dataset Preparation
Prepare spaCy binary format file. This library expects tokenization is character-based. For more detail about spaCy binary format, see this page.
You can prepare your own dataset with spaCy's entity ruler as follows:
import spacy
from spacy.tokens import DocBin
from spacy_partial_tagger.tokenizer import CharacterTokenizer
nlp = spacy.blank("en")
nlp.tokenizer = CharacterTokenizer(nlp.vocab) # Use a character-based tokenizer
patterns = [{"label": "LOC", "pattern": "Tokyo"}, {"label": "LOC", "pattern": "Japan"}]
ruler = nlp.add_pipe("entity_ruler")
ruler.add_patterns(patterns)
doc = nlp("Tokyo is the capital of Japan.")
doc_bin = DocBin()
doc_bin.add(doc)
# Replace /path/to/data.spacy with your own path
doc_bin.to_disk("/path/to/data.spacy")
In the example above, entities are extracted by character-based matching. However, in some cases, character-based matching may not be suitable (e.g., the element symbol na
for sodium matches name). In such cases, token-based matching can be used as follows:
import spacy
from spacy.tokens import DocBin
from spacy_partial_tagger.tokenizer import CharacterTokenizer
text = "Selegiline - induced postural hypotension in Parkinson's disease: a longitudinal study on the effects of drug withdrawal."
patterns = [
{"label": "Chemical", "pattern": [{"LOWER": "selegiline"}]},
{"label": "Disease", "pattern": [{"LOWER": "hypotension"}]},
{
"label": "Disease",
"pattern": [{"LOWER": "parkinson"}, {"LOWER": "'s"}, {"LOWER": "disease"}],
},
]
# Add an entity ruler to the pipeline.
nlp = spacy.blank("en")
ruler = nlp.add_pipe("entity_ruler")
ruler.add_patterns(patterns)
# Extract entities from the text.
doc = nlp(text)
entities = [(ent.start_char, ent.end_char, ent.label_) for ent in doc.ents]
# Create a DocBin object.
nlp = spacy.blank("en")
nlp.tokenizer = CharacterTokenizer(nlp.vocab)
doc_bin = DocBin()
doc = nlp.make_doc(text)
doc.ents = [
doc.char_span(start, end, label=label) for start, end, label in entities
]
doc_bin.add(doc)
doc_bin.to_disk("/path/to/data.spacy")
Training
Train your model as follows:
python -m spacy train config.cfg --output outputs --paths.train /path/to/train.spacy --paths.dev /path/to/dev.spacy --gpu-id 0
You could download config.cfg
here.
Or you could setup your own. This library would train models through spaCy. If you are not familiar with spaCy's config file format, please check the documentation.
Don't forget to replace /path/to/train.spacy
and /path/to/dev.spacy
with your own.
Evaluation
Evaluate your model as follows:
python -m spacy evaluate outputs/model-best /path/to/test.spacy --gpu-id 0
Don't forget to replace /path/to/test.spacy
with your own.
Installation
pip install spacy-partial-tagger
If you use M1 Mac, you might have problems installing fugashi
. In that case, please try brew install mecab
before the installation.
References
- Thomas Effland and Michael Collins. 2021. Partially Supervised Named Entity Recognition via the Expected Entity Ratio Loss. Transactions of the Association for Computational Linguistics, 9:1320–1335.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for spacy_partial_tagger-0.9.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8b6b1d936a2d2f60f4c5ee306e1c58099c5e6bca7faf93d575b9245ac6c8e44 |
|
MD5 | af84c14fe89641a09fed5e594812706f |
|
BLAKE2b-256 | 43846e6226915e369f471cac4ebd7f252c6c93871af3c7248a3133b43366f8df |
Hashes for spacy_partial_tagger-0.9.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6993be2c8d20f358609dcbf746d800b802d14503b6b85f13b99d1fc947a745b7 |
|
MD5 | 08c1590d4cf5ce2366f6b86950ba7ca2 |
|
BLAKE2b-256 | 6038bb48f10e5d305d86e0af0ffce1b325ffa8d6e655667343a934ac5f982afc |