Python Library for Language Change
Project description
LanguageChange
LanguageChange is a Python toolkit for exploring lexical semantic change across corpora and time. It comes with data loaders, embedding pipelines, cluster and comparison methods, evaluation utilities as well as plotting helpers so you can go from raw corpora to change scores and visual analyses in a single workflow.
Key Features
- Ready-to-use benchmarks (SemEval 2020 Task 1, DWUG) plus helpers for your own corpora.
- Static and contextualised representation pipelines (count, PPMI, SVD, transformer-based) with caching.
- Alignment and comparison utilities (e.g. Orthogonal Procrustes) and standard change metrics such as PRT and APD.
- Plotting helpers for DWUG graphs, embeddings clusters to inspect model behaviour visually.
Installation
pip install langchange
LanguageChange targets Python 3.8+ and depends on PyTorch, transformers, and several NLP/visualisation libraries. Installing inside a virtual environment is recommended.
Quickstart
LanguageChange can be used to analyze data from from scratch.
from languagechange.corpora import HistoricalCorpus, LinebyLineCorpus
from languagechange.search import SearchTerm
from sklearn.cluster import AgglomerativeClustering
from languagechange.models.change.metrics import JSD
from languagechange.models.representation.contextualized import XL_LEXEME
from languagechange.pipeline import CDPipeline
from languagechange.visualization import Visualizer
# Read two novels downloaded from gutenberg.org.
# Pride and prejudice (pp): https://www.gutenberg.org/cache/epub/42671/pg42671.txt
# Little women (lw): https://www.gutenberg.org/cache/epub/37106/pg37106.txt
# Loads the data, tokenizes and lemmatizes.
pp_corpus = LinebyLineCorpus("pp.txt", 1813)
lw_corpus = LinebyLineCorpus("lw.txt", 1868)
# Loads into HistoricalCorpus classes to compare across time.
novels = HistoricalCorpus([pp_corpus, lw_corpus])
# Define target word of interest.
target = "agreeable"
search_term = SearchTerm(lemma=target)
term = str(search_term)
# Retrieves the target usages.
pp_usages = pp_corpus.search(search_term)
lw_usages = lw_corpus.search(search_term)
all_usages = pp_usages + lw_usages
# Initialize model, cluster method and vector comparison metrics (JSD).
model = XL_LEXEME()
clustering_algorithm = AgglomerativeClustering(
n_clusters=None,
distance_threshold=0.5,
linkage="complete",
metric="cosine",
)
metric = JSD()
# Initializes pipeline
pipeline = CDPipeline(
all_usages,
model,
metric,
clustering_algorithm,
)
# Run the pipeline
sampled, embeddings, cluster_labels, change_scores = (
pipeline.run_pipeline()
)
# Initializes the visualizer
visualizer = Visualizer(
usages=sampled[term],
embeddings=embeddings[term],
cluster_labels=cluster_labels[term],
target=term,
)
# Visualizes the usage embedding clusters
visualizer.plot_usage_embeddings()
Or be used with several predefined datasets. For example, the test data from SemEval2020 task 1 for unsupervised lexical semantic change detection. See the resources file for all our supported datasets.
# Initializes a DWUG dataset
dwug = DWUG(dataset='DWUG', language='EN', version='3.0.0')
# Initialize the XL-LEXEME model
model = XL_LEXEME()
# Define a target word (with POS) to analyze.
target = "plane_nn"
# Retrieve the target usages for that word.
target_usages = dwug.get_word_usages(target)
# Annotate and cluster the target word using XL-LEXEME
dwug.annotate_and_cluster(target, model)
Development Setup
Clone the repository and install an editable build with the project extras you need:
git clone https://github.com/ChangeIsKey/languagechange.git
cd languagechange
python -m venv .venv && source .venv/bin/activate
pip install -e .
Running the examples may require additional packages listed under each example directory.
Documentation
For walkthrough tutorials and the API reference guide, please visit our official documentation. There are also documented notebooks and scripts in our examples folder.
Citation
The library is under active development. If it supports your research, please cite it as:
@misc{languagechange,
title = {LanguageChange: A Python library for studying semantic change},
author = {{Change is Key!}},
year = {n.d.}
}
Credits
LanguageChange is developed by the Change is Key! team with support from Riksbankens Jubileumsfond (grant M21-0021). Contributions and feedback are very welcome—feel free to open issues or pull requests.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchange-0.2.0.tar.gz.
File metadata
- Download URL: langchange-0.2.0.tar.gz
- Upload date:
- Size: 111.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ecd59c58a940acc7d74f978553b4b5f5433f56f9a10510b46dcf77f2cca5d06
|
|
| MD5 |
cbdd397621599b1d3dc500fb34d052d9
|
|
| BLAKE2b-256 |
ea79132d795c8e4bfd7519c5206cd0e00b1640e8f9c00bb782f1bdf8c1828b69
|
File details
Details for the file langchange-0.2.0-py3-none-any.whl.
File metadata
- Download URL: langchange-0.2.0-py3-none-any.whl
- Upload date:
- Size: 123.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4d3a9673959f2cde7cc1e33ab2c24c38f3e94b2a61b11b9d224440272ec8947
|
|
| MD5 |
bfc58b93620a9a1067e011ae255306f8
|
|
| BLAKE2b-256 |
35f288b86d335492bda331854d8ee6ccf070438dad0d6577fd7c13fa0fcc9738
|