Skip to main content

Social Tools

Overview

The Social Tools library provides a unified interface for interacting with various social analysis tools, including sentiment analysis, toxicity detection, emotion detection, and other natural language processing (NLP) models. With this library, developers can quickly analyze social media text, chat messages, or other forms of unstructured data for toxicity, sentiment, emotions, and more.

Key Features

  • Sentiment Analysis: Determine whether the text expresses positive, negative, or neutral sentiment.
  • Toxicity Detection: Identify toxic language, hate speech, offensive comments, and inappropriate content.
  • Emotion Detection: Recognize emotions such as happiness, sadness, anger, and more.
  • Custom NLP Models: Integrate additional NLP models for detecting bias, misinformation, and other social signals.

Installation

To install Social Tools, run:

pip install social-tools

Usage

Import the Modules

from social_tools import EmotionDetection, SentimentAnalysis, ToxicityDetection

Toxicity Detection

The ToxicityDetection module allows you to analyze text for toxic comments using pre-trained models like HuggingFace's unitary/toxic-bert.

# Using the unitary/toxic-bert transformer model
tox_detector = ToxicityDetection(tool='transformer', model='unitary/toxic-bert')
result = tox_detector.analyze("I hate you.")
print(result)

This returns:

[{'label': 'toxic', 'score': 0.9475088119506836}]

You can also analyze multiple texts at once:

tox_detector.analyze(["I hate you.", "This is harsh"])

Output:

[
    {'label': 'toxic', 'score': 0.9475088119506836},
    {'label': 'toxic', 'score': 0.002488125581294298}
]

Sentiment Analysis

The SentimentAnalysis module offers several options for analyzing the sentiment of text, including NLTK, SpaCy, and HuggingFace models.

# Using NLTK
sa = SentimentAnalysis(tool='nltk')
result = sa.analyze("This is awesome!")
print(result)

Output:

[{'neg': 0.0, 'neu': 0.313, 'pos': 0.687, 'compound': 0.6588}]

Using SpaCy:

sa = SentimentAnalysis(tool='spacy')
result = sa.analyze("This is awesome!")
print(result)

Output:

[{
    'polarity': 1.0, 
    'subjectivity': 1.0, 
    'sentiment_assessments': [(['awesome', '!'], 1.0, 1.0, None)]
}]

Using HuggingFace Transformer:

sa = SentimentAnalysis(tool='huggingface')
result = sa.analyze("This is awesome!")
print(result)

Output:

[{'label': 'POSITIVE', 'score': 0.9998669624328613}]

Custom HuggingFace Models

You can specify a custom HuggingFace transformer model by passing the model name during initialization:

sa = SentimentAnalysis(tool='huggingface', transformer_model="cardiffnlp/twitter-roberta-base-sentiment-latest")
result = sa.analyze("This is awesome!")
print(result)

Output:

[{'label': 'positive', 'score': 0.9813949465751648}]

Emotion Detection

The EmotionDetection module allows you to detect emotions such as happiness, sadness, and anger. For example, using HuggingFace models:

emotion_detector = EmotionDetection(tool='huggingface')
result = emotion_detector.analyze("I am so happy today!")
print(result)

This will return:

[{'label': 'joy', 'score': 0.95}]

Flexible Input Handling

All analyze functions in each module accept both single strings (str) and lists of strings (List[str]) as input:

# Single input
result = sa.analyze("I love this!")

# Multiple inputs
result = sa.analyze(["I love this!", "This is terrible."])

HuggingFace Transformer Parameters

When using a HuggingFace transformer model, you can pass additional parameters during initialization, such as return_all_scores:

sa = SentimentAnalysis(tool='huggingface', transformer_model="bert-base-uncased", return_all_scores=True)
result = sa.analyze("This is fantastic!")
print(result)

Conclusion

The Social Tools library simplifies the process of analyzing social data by providing multiple sentiment, emotion, and toxicity detection tools in a unified interface. You can integrate popular NLP libraries like NLTK, SpaCy, and HuggingFace models into your workflow seamlessly.

For more information about the supported HuggingFace models and additional parameters, refer to the HuggingFace documentation.

Acknowledgements

This project would not have been possible without the contributions of the following open-source projects:

  • Detoxify: For providing pre-trained models to detect toxic content in text.
  • HuggingFace: For providing a wide variety of pre-trained transformer models and their powerful transformers library.
  • NLTK (Natural Language Toolkit): For providing robust tools for text processing and sentiment analysis.
  • SpaCy: For offering fast and efficient NLP capabilities, along with the spacytextblob extension for sentiment analysis.
  • TextBlob: For providing an easy-to-use interface for text processing, sentiment analysis, and other NLP tasks.

A huge thank you to these projects and their respective communities for building the foundational tools that made this library possible.

Citation

@misc{socialtool,
  title={socialtool},
  author={Ridwan Amure},
  howpublished={Github. https://github.com/instabaines/social_tools_lib/},
  year={2024}
}

if you use the detoxify module in this tool, kindly cite

@misc{Detoxify,
  title={Detoxify},
  author={Hanu, Laura and {Unitary team}},
  howpublished={Github. https://github.com/unitaryai/detoxify},
  year={2020}
}

Metadata

Release files for social-tools 1.0.0

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

Source distribution (sdist)

Source distribution for social-tools 1.0.0
File Size Uploaded Provenance Actions
social_tools-1.0.0.tar.gz 10.8 kB Without provenance Details for social_tools-1.0.0.tar.gz

Release files / social_tools-1.0.0.tar.gz

Download URL social_tools-1.0.0.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
67b717b6df6a8501c06a866c741e5c9fc19a14a8ecee22c9d46f1cd9be275e1e
BLAKE2b-256 checksum
How to use checksums
adcf32b560de4e976b8eb5266726d54a17a4fe60ae7d71ff0e87031545b2ee0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.0

Security for social-tools 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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