百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
T

tweetnlp

> DevOps
开源

TweetNLP 为所有在 Twitter 上工作的 NLP 爱好者提供了一个名为 Python 的库,其中包含了一系列有用的工具,可用于分析/理解推文,例如:

393 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

TweetNLP 为所有在 Twitter 上工作的 NLP 爱好者提供了一个名为 Python 的库,其中包含了一系列有用的工具,可用于分析/理解推文,例如:

TweetNLP

TweetNLP for all the NLP enthusiasts working on Twitter and social media! The python library tweetnlp provides a collection of useful tools to analyze/understand tweets such as sentiment analysis, emoji prediction, and named-entity recognition, powered by state-of-the-art language modeling specialized on social media.

News (December 2022): We presented a TweetNLP demo paper ("TweetNLP: Cutting-Edge Natural Language Processing for Social Media"), at EMNLP 2022. The final version can be found here.

TweetNLP Hugging Face page All the main TweetNLP models can be found here on Hugging Face.

Resources:

  • Quick Tour with Colab Notebook:
  • Play with the TweetNLP Online Demo: link
  • EMNLP 2022 paper: link
  • 2nd Cardiff NLP Summer Workshop Tutorial:
  • 2nd Cardiff NLP Summer Workshop Tutorial (solutions):

Table of Contents:

  1. Load Model & Dataset
  2. Fine-tune Model

Get Started

Install TweetNLP via pip on your console.

bash
pip install tweetnlp

Model & Dataset

In this section, you will learn how to get the models and datasets with tweetnlp. The models follow huggingface model and the datasets are in the format of huggingface datasets. Easy introductions of huggingface models and datasets should be found at huggingface webpage, so please check them if you are new to huggingface.

Tweet Classification

The classification module consists of six different tasks (Topic Classification, Sentiment Analysis, Irony Detection, Hate Speech Detection, Offensive Language Detection, Emoji Prediction, and Emotion Analysis). In each example, the model is instantiated by tweetnlp.load_model("task-name"), and run the prediction by passing a text or a list of texts as argument to the corresponding function.

  • Topic Classification: The aim of this task is, given a tweet to assign topics related to its content. The task is formed as a supervised multi-label classification problem where each tweet is assigned one or more topics from a total of 19 available topics. The topics were carefully curated based on Twitter trends with the aim to be broad and general and consist of classes such as: arts and culture, music, or sports. Our internally-annotated dataset contains over 10K manually-labeled tweets (check the paper here, or the huggingface dataset page).
…
  • Sentiment Analysis: The sentiment analysis task integrated in TweetNLP is a simplified version where the goal is to predict the sentiment of a tweet with one of the three following labels: positive, neutral or negative. The base dataset for English is the unified TweetEval version of the Semeval-2017 dataset from the task on Sentiment Analysis in Twitter (check the paper here).
…
  • Irony Detection: This is a binary classification task where given a tweet, the goal is to detect whether it is ironic or not. It is based on the Irony Detection dataset from the SemEval 2018 task (check the paper here).
python
import tweetnlp

# MODEL
model = tweetnlp.load_model('irony')  # Or `model = tweetnlp.Irony()` 
model.irony('If you wanna look like a badass, have drama on social media')  # Or `model.predict`
>>> {'label': 'irony'}
model.irony('If you wanna look like a badass, have drama on social media', return_probability=True)
>>> {'label': 'irony', 'probability': {'non_irony': 0.08390884101390839, 'irony': 0.9160911440849304}} 

# GET DATASET
dataset, label2id = tweetnlp.load_dataset('irony')
  • Hate Speech Detection: The hate speech detection task consists of detecting whether a tweet is hateful towards a target community. The underlying model is based on a suite of unified hate speech detection datasets (see reference paper).
python
import tweetnlp

# MODEL
model = tweetnlp.load_model('hate')  # Or `model = tweetnlp.Hate()` 
model.hate('Whoever just unfollowed me you a bitch')  # Or `model.predict`
>>> {'label': 'not-hate'}
model.hate('Whoever just unfollowed me you a bitch', return_probability=True)
>>> {'label': 'non-hate', 'probability': {'non-hate': 0.7263831496238708, 'hate': 0.27361682057380676}}

# GET DATASET
dataset, label2id = tweetnlp.load_dataset('hate')
  • Offensive Language Identification: This task consists in identifying whether some form of offensive language is present in a tweet. For our benchmark we rely on the SemEval2019 OffensEval dataset (check the paper here).
python
import tweetnlp

# MODEL
model = tweetnlp.load_model('offensive')  # Or `model = tweetnlp.Offensive()` 
model.offensive("All two of them taste like ass.")  # Or `model.predict`
>>> {'label': 'offensive'}
model.offensive("All two of them taste like ass.", return_probability=True)
>>> {'label': 'offensive', 'probability': {'non-offensive': 0.16420328617095947, 'offensive': 0.8357967734336853}}

# GET DATASET
dataset, label2id = tweetnlp.load_dataset('offensive')
  • Emoji Prediction: The goal of emoji prediction is to predict the final emoji on a given tweet. The dataset used to fine-tune our models is the TweetEval adaptation from the SemEval 2018 task on Emoji Prediction (check the paper here), including 20 emoji as labels (❤, , , , , , , ✨, , , , , ☀, , , , , , , ).
…
  • Emotion Recognition: Given a tweet, this task consists of associating it with its most appropriate emotion. As a reference dataset we use the SemEval 2018 task on Affect in Tweets (check the paper here). The latest multi-label model includes eleven emotion types.
…

WARNING: The single-label and multi-label emotion model have diiferent label set (single-label has four classes of 'joy'/'optimism'/'anger'/'sadness', while multi-label has eleven classes of 'joy'/'optimism'/'anger'/'sadness'/'love'/'trust'/'fear'/'surprise'/'anticipation'/'disgust'/'pessimism').

Named Entity Recognition

This module consists of a named-entity recognition (NER) model specifically trained for tweets. The model is instantiated by tweetnlp.load_model("ner"), and runs the prediction by giving a text or a list of texts as argument to the ner function (check the paper here, or the huggingface dataset page).

…

Question Answering

This module consists of a question answering model specifically trained for tweets. The model is instantiated by tweetnlp.load_model("question_answering"), and runs the prediction by giving a question or a list of questions along with a context or a list of contexts as argument to the question_answering function (check the paper here, or the huggingface dataset page).

python3
import tweetnlp

# MODEL
model = tweetnlp.load_model('question_answering')  # Or `model = tweetnlp.QuestionAnswering()` 
model.question_answering(
  question='who created the post as we know it today?',
  context="'So much of The Post is Ben,' Mrs. Graham said in 1994, three years after Bradlee retired as editor. 'He created it as we know it today.'— Ed O'Keefe (@edatpost) October 21, 2014"
)  # Or `model.predict`
>>> {'generated_text': 'ben'}

# GET DATASET
dataset = tweetnlp.load_dataset('question_answering')

Question Answer Generation

This module consists of a question & answer pair generation specifically trained for tweets. The model is instantiated by tweetnlp.load_model("question_answer_generation"), and runs the prediction by giving a context or a list of contexts as argument to the question_answer_generation function (check the paper here, or the huggingface dataset page).

…

Language Modeling

The masked language model predicts the masked token in the given sentence. This is instantiated by tweetnlp.load_model('language_model'), and runs the prediction by giving a text or a list of texts as argument to the mask_prediction function. Please make sure that each text has a `` token, since that is eventually the following by the objective of the model to predict.

python
import tweetnlp
model = tweetnlp.load_model('language_model')  # Or `model = tweetnlp.LanguageModel()` 
model.mask_prediction("How many more  until opening day? ", best_n=2)  # Or `model.predict`
>>> {'best_tokens': ['days', 'hours'],
 'best_scores': [5.498564104033932e-11, 4.906026140893971e-10],
 'best_sentences': ['How many more days until opening day? ',
  'How many more hours until opening day? ']}

Tweet Embedding

The tweet embedding model produces a fixed length embedding for a tweet. The embedding represents the semantics by meaning of the tweet, and this can be used for semantic search of tweets by using the similarity between the embeddings. Model is instantiated by tweet_nlp.load_model('sentence_embedding'), and run the prediction by passing a text or a list of texts as argument to the embedding function.

  • Get Embedding
…
  • Similarity Search
…

Resources & Custom Model Loading

Here is a table of the default model used in each task.

Task Model Dataset
Topic Classification (single-label) cardiffnlp/twitter-roberta-base-dec2021-tweet-topic-single-all cardiffnlp/tweet_topic_single
Topic Classification (multi-label) cardiffnlp/twitter-roberta-base-dec2021-tweet-topic-multi-all cardiffnlp/tweet_topic_multi
Sentiment Analysis (Multilingual) cardiffnlp/twitter-xlm-roberta-base-sentiment cardiffnlp/tweet_sentiment_multilingual
Sentiment Analysis cardiffnlp/twitter-roberta-base-sentiment-latest tweet_eval
Irony Detection [cardiffnlp/twitter-roberta-base-irony](https://hugging

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Pythoncomputational-social-sciencelanguage-modelnatural-language-processingpython

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类DevOps
定价开源

> 相关工具

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理