tweetnlp

tweetnlp

社交媒体文本分析的全能NLP工具集

TweetNLP是一个专注于社交媒体分析的Python库,为Twitter等平台提供全面的文本分析功能。该库集成了多项先进的自然语言处理技术,包括情感分析、表情预测、命名实体识别等。TweetNLP还支持主题分类、讽刺检测、仇恨言论识别和情感识别等多种任务,为社交媒体研究和应用开发提供了强大而灵活的工具集。

TweetNLP自然语言处理社交媒体模型数据集Github开源项目

license PyPI version PyPI pyversions PyPI status

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: Open In Colab
  • Play with the TweetNLP Online Demo: link
  • EMNLP 2022 paper: link
  • 2nd Cardiff NLP Summer Workshop Tutorial: Open In Colab
  • 2nd Cardiff NLP Summer Workshop Tutorial (solutions): Open In Colab

Table of Contents:

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

Get Started

Install TweetNLP via pip on your console.

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

Open In Colab

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).
import tweetnlp # MULTI-LABEL MODEL model = tweetnlp.load_model('topic_classification') # Or `model = tweetnlp.TopicClassification()` model.topic("Jacob Collier is a Grammy-awarded English artist from London.") # Or `model.predict` >>> {'label': ['celebrity_&_pop_culture', 'music']} # Note: the probability of the multi-label model is the output of sigmoid function on binary prediction whether each topic is positive or negative. model.topic("Jacob Collier is a Grammy-awarded English artist from London.", return_probability=True) >>> {'label': ['celebrity_&_pop_culture', 'music'], 'probability': {'arts_&_culture': 0.037371691316366196, 'business_&_entrepreneurs': 0.010188567452132702, 'celebrity_&_pop_culture': 0.92448890209198, 'diaries_&_daily_life': 0.03425711765885353, 'family': 0.00796138122677803, 'fashion_&_style': 0.020642118528485298, 'film_tv_&_video': 0.08062587678432465, 'fitness_&_health': 0.006343095097690821, 'food_&_dining': 0.0042883665300905704, 'gaming': 0.004327300935983658, 'learning_&_educational': 0.010652057826519012, 'music': 0.8291937112808228, 'news_&_social_concern': 0.24688217043876648, 'other_hobbies': 0.020671198144555092, 'relationships': 0.020371075719594955, 'science_&_technology': 0.0170074962079525, 'sports': 0.014291072264313698, 'travel_&_adventure': 0.010423899628221989, 'youth_&_student_life': 0.008605164475739002}} # SINGLE-LABEL MODEL model = tweetnlp.load_model('topic_classification', multi_label=False) # Or `model = tweetnlp.TopicClassification(multi_label=False)` model.topic("Jacob Collier is a Grammy-awarded English artist from London.") >>> {'label': 'pop_culture'} # NOTE: the probability of the sinlge-label model the softmax over the label. model.topic("Jacob Collier is a Grammy-awarded English artist from London.", return_probability=True) >>> {'label': 'pop_culture', 'probability': {'arts_&_culture': 9.20625461731106e-05, 'business_&_entrepreneurs': 6.916998972883448e-05, 'pop_culture': 0.9995898604393005, 'daily_life': 0.00011083036952186376, 'sports_&_gaming': 8.668467489769682e-05, 'science_&_technology': 5.152115045348182e-05}} # GET DATASET dataset_multi_label, label2id_multi_label = tweetnlp.load_dataset('topic_classification') dataset_single_label, label2id_single_label = tweetnlp.load_dataset('topic_classification', multi_label=False)
  • 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).
import tweetnlp # ENGLISH MODEL model = tweetnlp.load_model('sentiment') # Or `model = tweetnlp.Sentiment()` model.sentiment("Yes, including Medicare and social security saving👍") # Or `model.predict` >>> {'label': 'positive'} model.sentiment("Yes, including Medicare and social security saving👍", return_probability=True) >>> {'label': 'positive', 'probability': {'negative': 0.004584966693073511, 'neutral': 0.19360853731632233, 'positive': 0.8018065094947815}} # MULTILINGUAL MODEL model = tweetnlp.load_model('sentiment', multilingual=True) # Or `model = tweetnlp.Sentiment(multilingual=True)` model.sentiment("天気が良いとやっぱり気持ち良いなあ✨") >>> {'label': 'positive'} model.sentiment("天気が良いとやっぱり気持ち良いなあ✨", return_probability=True) >>> {'label': 'positive', 'probability': {'negative': 0.028369612991809845, 'neutral': 0.08128828555345535, 'positive': 0.8903420567512512}} # GET DATASET (ENGLISH) dataset, label2id = tweetnlp.load_dataset('sentiment') # GET DATASET (MULTILINGUAL) for l in ['all', 'arabic', 'english', 'french', 'german', 'hindi', 'italian', 'portuguese', 'spanish']: dataset_multilingual, label2id_multilingual = tweetnlp.load_dataset('sentiment', multilingual=True, task_language=l)
  • 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).
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).
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).
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 (❤, 😍, 😂, 💕, 🔥, 😊, 😎, ✨, 💙, 😘, 📷, 🇺🇸, ☀, 💜, 😉, 💯, 😁, 🎄, 📸, 😜).
import tweetnlp # MODEL model = tweetnlp.load_model('emoji') # Or `model = tweetnlp.Emoji()` model.emoji('Beautiful sunset last night from the pontoon @TupperLakeNY') # Or `model.predict` >>> {'label': '😊'} model.emoji('Beautiful sunset last night from the pontoon @TupperLakeNY', return_probability=True) >>> {'label': '📷', 'probability': {'❤': 0.13197319209575653, '😍': 0.11246423423290253, '😂': 0.008415069431066513, '💕': 0.04842926934361458, '🔥': 0.014528146013617516, '😊': 0.1509675830602646, '😎': 0.08625403046607971, '✨': 0.01616635173559189, '💙': 0.07396604865789413, '😘': 0.03033279813826084, '📷': 0.16525287926197052, '🇺🇸': 0.020336611196398735, '☀': 0.00799981877207756, '💜': 0.016111424192786217, '😉': 0.012984540313482285, '💯': 0.012557178735733032, '😁': 0.031386848539114, '🎄': 0.006829539313912392, '📸': 0.04188741743564606, '😜': 0.011156936176121235}} # GET DATASET dataset, label2id = tweetnlp.load_dataset('emoji')
  • 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.
import tweetnlp # MULTI-LABEL MODEL model = tweetnlp.load_model('emotion') # Or `model = tweetnlp.Emotion()` model.emotion('I love swimming for the same reason I love meditating...the feeling of weightlessness.') # Or `model.predict` >>> {'label': 'joy'} # Note: the probability of the multi-label model is the output of sigmoid function on binary prediction whether each topic is positive or negative. model.emotion('I love swimming for the same reason I love meditating...the feeling of weightlessness.', return_probability=True) >>> {'label': 'joy', 'probability': {'anger': 0.00025800734874792397, 'anticipation': 0.0005329723935574293, 'disgust': 0.00026112011983059347, 'fear': 0.00027552215033210814, 'joy': 0.7721399068832397, 'love': 0.1806265264749527, 'optimism': 0.04208092764019966, 'pessimism': 0.00025325192837044597, 'sadness': 0.0006160663324408233, 'surprise': 0.0005619609728455544, 'trust': 0.002393839880824089}} # SINGLE-LABEL MODEL model = tweetnlp.load_model('emotion') # Or `model = tweetnlp.Emotion()` model.emotion('I love swimming for the same reason I love meditating...the feeling of weightlessness.') # Or `model.predict` >>> {'label': 'joy'} # NOTE: the probability of the

编辑推荐精选

堆友

堆友

多风格AI绘画神器

堆友平台由阿里巴巴设计团队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。

图像生成AI工具AI反应堆AI工具箱AI绘画GOAI艺术字堆友相机AI图像热门
码上飞

码上飞

零代码AI应用开发平台

零代码AI应用开发平台,用户只需一句话简单描述需求,AI能自动生成小程序、APP或H5网页应用,无需编写代码。

Vora

Vora

免费创建高清无水印Sora视频

Vora是一个免费创建高清无水印Sora视频的AI工具

Refly.AI

Refly.AI

最适合小白的AI自动化工作流平台

无需编码,轻松生成可复用、可变现的AI自动化工作流

酷表ChatExcel

酷表ChatExcel

大模型驱动的Excel数据处理工具

基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。

AI工具酷表ChatExcelAI智能客服AI营销产品使用教程
TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
AIWritePaper论文写作

AIWritePaper论文写作

AI论文写作指导平台

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

AI辅助写作AI工具AI论文工具论文写作智能生成大纲数据安全AI助手热门
博思AIPPT

博思AIPPT

AI一键生成PPT,就用博思AIPPT!

博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。

AI办公办公工具AI工具博思AIPPTAI生成PPT智能排版海量精品模板AI创作热门
潮际好麦

潮际好麦

AI赋能电商视觉革命,一站式智能商拍平台

潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。

iTerms

iTerms

企业专属的AI法律顾问

iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。

下拉加载更多