Skip to main content

Same Text, Different Context: A package for visualizing contextual word embeddings

Project description

ST-DC: Same Text, Different Context

Same Text, Different Context is a Python package for visualizing contextual word embeddings and their neighbors across different contexts. It helps you understand how the meaning of a word shifts depending on its surrounding text. This is achieved by leveraging pretrained language models (like BERT) to extract embeddings for focus words and their context-specific neighbors, and visualizing them in 2D or 3D.


How It Works

Here's an quick explanation of how ST-DC works:

  1. Contextual Embedding Extraction:

    • The package uses a pretrained language model (e.g., BERT) to generate contextual embeddings for a focus word (e.g., pool) in various sentences (contexts).
    • For example:
      • Sentence 1: "The pool is open for swimming."
      • Sentence 2: "The pool of candidates is very competitive."
    • In each sentence, the focus word's embedding represents its meaning in that specific context.
  2. Neighbor Prediction:

    • The tool predicts words most semantically related to the focus word in each context using the model's [MASK] functionality.
    • For example:
      • In the sentence "The [MASK] is open for swimming," neighbors might include water, swim, and diving.
      • In the sentence "The [MASK] of candidates is very competitive," neighbors might include selection, group, and list.
  3. Dimensionality Reduction:

    • Embeddings are originally high-dimensional (e.g., 768 dimensions for BERT).
    • To make visualization possible, these embeddings are reduced to 2D or 3D using techniques like:
      • PCA: Preserves as much variance as possible.
      • t-SNE: Captures local relationships between points.
  4. Visualization:

    • Embeddings for the focus word and its neighbors are plotted in 2D or 3D space:
      • Focus Word Embeddings: Each context is represented as a unique point, color-coded for distinction.
      • Neighbor Words:Words semantically related to the focus word in each context are plotted near the corresponding focus word. This visualization helps you explore how the meaning of the focus word varies between contexts.

Features

  • Extract embeddings: Analyze how the meaning of a word changes in different contexts.
  • Visualize embeddings: Explore embeddings in 2D or 3D space using dimensionality reduction.
  • Neighbor words: Display words closely related to the focus word for each context.

Installation

To install ST-DC, run:

pip install st-dc

Requirements

  • Requires Python 3.11

Usage

Example 1: Visualizing Word "Pool"

from context_explorer import viz
from samples import sentences_dict

# Define the focus word and its contexts
focus_word = "pool"
sentences = [
  "The pool is open for swimming.", 
  "The pool of candidates is very competitive."
  ]

# Visualize embeddings
viz(
    word=focus_word,
    sentences=sentences,
    dim_technique="pca",  # Dimensionality reduction technique (e.g., "pca", "tsne")
    num_neighbors=5,      # Number of neighbors to display
    plot_type="3D"        # Visualization type: "2D" or "3D"
)

Example ST-DC Viz

The above image showcases the resulting visualizatiion. It features the following:

  • An interactive 3D visualization
  • Two distinct colors to represent the different contexts
  • The embedding of the focus word (pool) in the different contexts
  • The n closest words for the focus word in each context
  • A legend to the side showing the color-context mapping

Example 2: Switching to t-SNE and 2D Visualization

focus_word = "king"
sentences = [
    "The king ruled the land wisely.",
    "The chess king was captured during the game.",
]

viz(
    word=focus_word,
    sentences=sentences,
    dim_technique="tsne",
    num_neighbors=5,
    plot_type="2D"
)

FAQ

1. What pretrained models are supported?

Currently, the package uses bert-base-uncased as the default pretrained model. Support for additional models (like RoBERTa or DistilBERT) can be added by modifying the EmbeddingExtractor class.

2. Can I use my own sentences?

Yes, the sentences parameter in the viz function accepts any list of sentences.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgments

  • This package leverages the Hugging Face transformers library for pretrained language models.
  • Visualization is powered by plotly.

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

st_dc-1.1.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

st_dc-1.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file st_dc-1.1.0.tar.gz.

File metadata

  • Download URL: st_dc-1.1.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for st_dc-1.1.0.tar.gz
Algorithm Hash digest
SHA256 d2cc355b96babe0d52de0ccf2d8725954dca34f7d0f475f2dbb5f1760824c200
MD5 173f27a8e7b2861398b94a9d1057ff1a
BLAKE2b-256 9a1a2a9d416d99f90e1735874ee1096aa3b07538bde90a99031f52103dff4784

See more details on using hashes here.

File details

Details for the file st_dc-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: st_dc-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for st_dc-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dba9ac8203c2a350460c1181232e1db94211c2758188441a0fe3b7faec47a5a0
MD5 cd8ce1860fa2d2540d98ccd74d872e35
BLAKE2b-256 feba4c8fa97de499c35955d59c55565515b693b6a7c8fe57b2855a767bdf6cc1

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