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.3.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.3-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: st_dc-1.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 e15d56e570b970de7f5d3d6e2211e1869af62bc30ac042f13da26bff297bf09c
MD5 8a448db305335e0e8a91535efb7520b2
BLAKE2b-256 02e493dbe2f50a0fba90986cfa1c8aa786e003f652dfc56e16444ce23aa640be

See more details on using hashes here.

File details

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

File metadata

  • Download URL: st_dc-1.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 86797cef461a109039163675e1f47e0d3af2ce0327f9231e58628a2a4f44a122
MD5 976cc7df4c70726bc2fbd30897e3da65
BLAKE2b-256 f9b55f399538e189bddb628475df46d9dc70d2e5038693eae795d446de1f8024

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