TweetNLP for all the NLP enthusiasts working on Twitter! The Python library tweetnlp provides a collection of useful tools to analyze/understand tweets such as
TweetNLP for all the NLP enthusiasts working on Twitter! The Python library tweetnlp provides a collection of useful tools to analyze/understand tweets such as
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:
Table of Contents:
Install TweetNLP via pip on your console.
pip install tweetnlpIn 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.
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.
……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')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')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')……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').
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).
…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).
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')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).
…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.
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? ']}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.
……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 |
No open issues yet, or sync has not completed.