Skip to main content

Spacy-GlossBert

A spaCy pipeline component for word sense disambiguation using the GlossBERT model.

Overview

This package provides a spaCy component that performs Word Sense Disambiguation (WSD) using the GlossBERT model. GlossBERT leverages BERT's contextual embeddings to disambiguate word senses by comparing the context with WordNet sense definitions (glosses).

Installation

pip install spacy-glossbert

You'll also need to download the spaCy model:

python -m spacy download en_core_web_sm

Usage

Basic Usage

import spacy
from spacy_glossbert import has_glossbert_wsd, get_synset_info

# Load spaCy with the GlossBERT component
nlp = spacy.load("en_core_web_sm")
nlp.add_pipe("glossbert_wsd", last=True)

# Process a text
doc = nlp("He went to the bank to deposit money.")

# Check if the document has been processed with GlossBERT
if has_glossbert_wsd(doc):
    # Print disambiguated senses
    for token in doc:
        synset = token._.glossbert_synset
        if synset:
            print(f"{token.text}: {token.pos_} -- {synset.name()} - {synset.definition()}")

    # Alternative: Get all sense information as a list of dictionaries
    senses = get_synset_info(doc)
    for sense in senses:
        print(f"{sense['text']}: {sense['synset']} - {sense['definition']}")

Visualization

The package includes visualization utilities using spaCy's displaCy:

from spacy_glossbert import visualize_wsd

# Visualize the disambiguated senses
visualize_wsd(doc)

Configuration Options

# Configure the component
nlp.add_pipe(
    "glossbert_wsd",
    config={
        "pos_filter": ["NOUN", "VERB", "ADJ"],  # Part-of-speech tags to process
        "supervision": True,  # Highlight the target word in context
        "model_name": "kanishka/GlossBERT"  # HuggingFace model name/path
    }
)

How It Works

The GlossBERT component:

  1. Identifies tokens with POS tags specified in pos_filter
  2. Retrieves WordNet synsets (possible senses) for each token
  3. For each candidate sense, creates an input of the form: "context [SEP] definition"
  4. Scores each sense using the GlossBERT model
  5. Assigns the highest-scoring sense to each token

License

This project is licensed under the GNU General Public License v2.0 (GPLv2).

Credits

Author

Igor Morgado morgado.igor@gmail.com

Release files for spacy-glossbert 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-glossbert 0.1.0
File Size Uploaded
spacy_glossbert-0.1.0.tar.gz 7.8 kB Details

Built distribution (wheel)

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

Total release size: 15.8 kB

Release files / spacy_glossbert-0.1.0.tar.gz

Download URL spacy_glossbert-0.1.0.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
934ce7bc28c2f534b1639ef4921b6dbf76d8a5431eb55cd25a67a574530e153e
BLAKE2b-256 checksum
How to use checksums
c5d053618e12bb78879e79d72f0c0888def159434f56145cdeb0e2718e357e7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

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

Download URL spacy_glossbert-0.1.0-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fb4a88978c12f32e8e02e0ffe6e9df2a9167825f2f9f1c5e340459b0290404b0
BLAKE2b-256 checksum
How to use checksums
ce0fc69d1987c07d5800ff50ef3948d1cd4b84f1840b4627668bd21b07cb0520
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release 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