Emotion classification on Twitter Data Using Transformers

guest_blog 14 Jan, 2021
4 min read

Emotion classification featured image


pip install simpletransfomers
    bearer_token: xxxxxxxxxxxxxxxxxxxxxxx

emotion classification data head

Data frame

Class distribution

Class distribution

Preprocessed dataframe

Preprocessed dataframe

from simpletransformers.classification import ClassificationModel, ClassificationArgs

model_args = ClassificationArgs()
model_args.num_train_epochs = 4
model_args.reprocess_input_data = True
model_args.save_best_model = True
model_args.save_optimizer_and_scheduler = False
model_args.overwrite_output_dir = True
model_args.manual_seed = 4
model_args.use_multiprocessing = True
model_args.train_batch_size = 16
model_args.eval_batch_size = 8
model_args.max_seq_length = 64

model = ClassificationModel("xlnet", 
def create_twitter_url(handle, max_results):

    mrf = "max_results={}".format(max_results)
    q = "query=from:{}".format(handle)
    url = "https://api.twitter.com/2/tweets/search/recent?{}&{}".format(
        mrf, q
    return url
def process_yaml():
  with open("keys.yaml") as file:
    return yaml.safe_load(file)
def create_bearer_token(data):
    return data["search_tweets_api"]["bearer_token"]
def twitter_auth_and_connect(bearer_token, url):
    headers = {"Authorization": "Bearer {}".format(bearer_token)}
    response = requests.request("GET", url, headers=headers)
    return response.json()

url = create_twitter_url('user',10)
data = process_yaml()
bearer_token = create_bearer_token(data)

response = twitter_auth_and_connect(bearer_token, url)
text_list = [x['text'] for x in response['data']]
cleaned_text = [re.findall(regex, x)[0] for x in text_list]
Image for post

Emotion counts of those famous social media chains

The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion.


About the Author


Vishnu Nkumar

Machine learning engineer with competent knowledge in innovating solutions capable of improving the business decisions in various domains. Substantial hands-on knowledge in Python, Docker, API frameworks, etc, and have deployed a lot of reusable solutions in the cloud. Ample knowledge in orchestrating the architecture of deploying solutions using either open sourced service or platform services in the cloud

guest_blog 14 Jan, 2021

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers