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.2.tar.gz (9.7 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.2-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: st_dc-1.1.2.tar.gz
  • Upload date:
  • Size: 9.7 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.2.tar.gz
Algorithm Hash digest
SHA256 ca17b4bb979776c7cddd7a044a1fee39c75467a9b2120790fe297ff691f614ad
MD5 eec3745fd1270f3b5d7118481786aae2
BLAKE2b-256 fedbb8a5c0ee95e4870299bc5d188ae681377b4f8e0810af608b670837d4a1ea

See more details on using hashes here.

File details

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

File metadata

  • Download URL: st_dc-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.7 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ebf22d6436e7bb8711e6cbfa841e4465533d3144351453ebb72d6a6444feb9c0
MD5 5e48f925347d9f8920e04a8ce7afb9be
BLAKE2b-256 06c22626e7f434489a6867cccc1186ebb4239c94574355d6ffb9d6e6ec417c16

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