Skip to main content

PyPI - Python PyPI - License PyPI - PyPi arXiv

Semantic Component Analysis: Discovering Patterns in Short Texts Beyond Topics

Semantic Component Analysis (SCA) is a powerful tool to analyse your text datasets. If you want to find out how it works and why it is the right tool for you, consider reading our paper.

If you just want to test the method as quickly as possible, continue with the Quick Start section. For everything else, the Manual Installation section should have you covered. If you run into any problems or have suggestions, feel free to create an issue and we will try to adress it in future releases.

Quick Start

The method is available through pypi and part of the semantic_components package. You can install it as

pip install semantic_components

Running SCA is as simple as importing this package, and two lines for instantiating and fitting the model:

from semantic_components.sca import SCA

# fit sca model to data
sca = SCA(alpha_decomposition=0.1, mu=0.9, combine_overlap_threshold=0.5)
scores, residuals, ids = sca.fit(documents, embeddings)

# get representations and explainable transformations
representations = sca.representations  # pandas df
transformed = sca.transform(embeddings)  # equivalent to variable scores above

A full example including computing the embeddings and loading the Trump dataset can be found in example.py. We advise to clone this repository, if you want to run this example and/or our experiments found in the experiments/ directory.

Where applicable, the results/ folder is where the experiment scripts will store their results. Run SCA with save_results=True and verbose=True to enable this behaviour. This will generate a reports.txt containing information and evaluation metrics. Furthermore, there will be .pkl and .txt files with the representations of the semantic components found by the procedure.

OCTIS Evaluation

By default, we do not install octis as it requires older versions of some other packages and thus creates compatibility issues. If you want to use OCTIS evaluation (i.e. topic coherence and diversity), consider installing this package as

pip install semantic_components[octis]

for both versions, we recommend Python 3.10 or higher.

Manual Installation

In order to run the code provided in this repository, a number of non-standard Python packages need to be installed. As of October 2024, Python 3.10.x with the most current versions should work with the implementions provided. Here is a pip install command you can use in your environment to install all of them.

pip install sentence_transformers umap-learn hdbscan jieba scikit-learn pandas octis

Our experiments have been run with the following versions:

hdbscan                  0.8.39
jieba                    0.42.1
numpy                    1.26.4
octis                    1.14.0
pandas                   2.2.3
scikit-learn             1.1.0
sentence-transformers    3.2.0
torch                    2.4.1
transformers             4.45.2
umap-learn               0.5.6

You can clone this repository to your machine as follows:

git clone git@github.com:eichinflo/semantic_components.git

If you work with conda for example, you can run the following commands to get an environment suited to run the code:

cd semantic_components
conda create -n sca python=3.10.15
conda activate sca
pip install sentence_transformers umap-learn hdbscan jieba scikit-learn pandas octis

then, you're ready to run the example script which reproduces part of the results on the Trump dataset:

python example.py

Data

All data used in this work is publicly available. The Trump dataset is available from the Trump Twitter Archive. You can choose to download your version as .csv directly from that page and put it in the data/ directory (to work with the experiment code, rename it to trump_tweets.csv). However, we provide the version we used in this repository.

Besides, we publish the Chinese News dataset, which we acquired to the Twitter API and was kept updated until our academic access got revoked in April 2023. We provide it as a download HERE.

The current version of the Hausa Tweet dataset is available at the NaijaSenti repository.

AI Usage Disclaimer

The code in this repository has been written with the support of code completions of an AI coding assistant, namely GitHub Copilot. Completions were mostly single lines up to a few lines of code and were always checked carefully to ensure their functionality and safety. Furthermore, we did our best to avoid accepting code completions that would be incompatible with the license of our code or could be regarded as plagiarism.

Acknowledgements

We're grateful to Kristin Shi-Kupfer and David Adelani for consulting on the Chinese and Hausa datasets respectively. Furthermore, we would like to mention that the code of the c-TF-IDF representer has been largely adapted from the original BERTopic implementation by Maarten Grootendorst released under MIT license.

Citing This Work

If you're using this work for your project, please consider citing our paper:

@misc{eichin2024semanticcomponentanalysisdiscovering,
      title={Semantic Component Analysis: Discovering Patterns in Short Texts Beyond Topics}, 
      author={Florian Eichin and Carolin Schuster and Georg Groh and Michael A. Hedderich},
      year={2024},
      eprint={2410.21054},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2410.21054}, 
}

Metadata

Release files for semantic-components 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for semantic-components 0.1.1
File Size Uploaded
semantic_components-0.1.1.tar.gz 29.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for semantic-components 0.1.1
File Interpreter ABI Platform
semantic_components-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 55.5 kB

Release files / semantic_components-0.1.1.tar.gz

Download URL semantic_components-0.1.1.tar.gz
Size 29.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e37a1becb5a81731123075318bf4e0f56532b30cb5bef1e9971c1f86b189a11c
BLAKE2b-256 checksum
How to use checksums
e5eff8e0b62fad6de2ef1747b29d120f38ad02016270f84fc189b1f681e43c36
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.15

Release files / semantic_components-0.1.1-py3-none-any.whl

Download URL semantic_components-0.1.1-py3-none-any.whl
Size 26.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
284349bd972f68baf668ec39ce5677b00801f09374c1f3e22fdf17e52067f0bb
BLAKE2b-256 checksum
How to use checksums
e0ada2cdba6153fde2dada8ea8ef7e315675fe9e4d720d672a6fec701353bc60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.15

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

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