The Wayback Machine - https://web.archive.org/web/20240524235948/https://www.geeksforgeeks.org/twitter-text-python-ttp-module-python/
Open In App

twitter-text-python (ttp) module – Python

Last Updated : 08 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

twitter-text-python is a Tweet parser and formatter for Python. Amongst many things, the tasks that can be performed by this module are :

  • reply : The username of the handle to which the tweet is being replied to.
  • users : All the usernames mentioned in the tweet.
  • tags : All the hashtags mentioned in the tweet.
  • urls : All the URLs mentioned in the tweet.
  • html : Adds hyperlinks to the fields mentioned above.

Example 1 :




# import the twitter-text-python module
from ttp import ttp
  
# the text to be parsed
tweet_text = ("@twitter Sample tweet containing different components." +
             "# gfg # tweeple Visit : https://twitter.com @TwitterIndia")
  
# instantiating the Parser
p = ttp.Parser()
  
# parsing the text
result = p.parse(tweet_text)
  
# printing the username of the
# account being replied to
print("The username being replied to is : " + result.reply)
  
# printing all the usernames
# mentioned in the tweet
print("\nAll the usernames mentioned are : " + str(result.users))
  
# printing all the hashtags
# mentioned in the tweet
print("\nAll the hashtags mentioned are : " + str(result.tags))
  
# printing all the URLs
# mentioned in the tweet
print("\nAll the URLs mentioned are : " + str(result.urls))
  
# adding hyperlinks to usernames,
# hashtags and URLs
print(result.html)


Output :

The username being replied to is : twitter

All the usernames mentioned are : [‘twitter’, ‘TwitterIndia’]

All the hashtags mentioned are : [‘gfg’, ‘tweeple’]

All the URLs mentioned are : [‘https://twitter.com’]

@twitter Sample tweet containing different components.#gfg #tweeple Visit : https://twitter.com @TwitterIndia

Example 2 : We can also find the position of string (POS) by doing include_spans = True.




# import the twitter-text-python module
from ttp import ttp
  
# the text to be parsed
tweet_text = ("@twitter Sample tweet containing different components." +
             "# gfg # tweeple Visit : https://twitter.com @TwitterIndia")
  
# instantiating the Parser
# with spans
p = ttp.Parser(include_spans = True)
  
# parsing the text
result = p.parse(tweet_text)
  
# printing all the usernames
# mentioned in the tweet with POS
print("All the usernames mentioned are : " + str(result.users))
  
# printing all the hashtags
# mentioned in the tweet with POS
print("\nAll the hashtags mentioned are : " + str(result.tags))
  
# printing all the URLs
# mentioned in the tweet with POS
print("\nAll the URLs mentioned are : " + str(result.urls))


Output :

All the usernames mentioned are : [(‘twitter’, (0, 8)), (‘TwitterIndia’, (130, 143))]

All the hashtags mentioned are : [(‘gfg’, (96, 100)), (‘tweeple’, (101, 109))]

All the URLs mentioned are : [(‘https://twitter.com’, (76, 95))]



Similar Reads

Os Module Vs. Sys Module In Python
Python provides a lot of libraries to interact with the development environment. If you need help using the OS and Sys Module in Python, you have landed in the right place. This article covers a detailed explanation of the OS and Sys Module including their comparison. By the end of this article, you will be able to easily decide which module suits
5 min read
How to Build a Twitter Bot to Post Latest Stock Update using Python
The stock market is volatile and changes rapidly so we are going to create a simple Twitter bot to post the latest stock updates using Python that posts the tweet about the stocks that users have chosen. First, let's understand the prerequisites of our project: Nsepython: It is a Python library to get publicly available data on the current NSEIndia
6 min read
Twitter Automation using Selenium Python
If you are someone like me who considers Twitter to be far better than Instagram, then I might be having something for you. We all know gaining followers on twitter can be pretty tough but sometimes retweeting quality content can gain you, followers, too. Obviously, you are a busy person and you don't have time to sit around on your phone or laptop
6 min read
How to make a Twitter Bot in Python?
Twitter is an American microblogging and social networking service on which users post and interact with messages known as "tweets". In this article we will make a Twitter Bot using Python. Python as well as Javascript can be used to develop an automatic Twitter bot that can do many tasks by its own such as: Retweets the tweets with particular #has
3 min read
Login Twitter using Python Selenium
Project Description:- Here, we're going to study a simple how-to log in twitter by selenium. Selenium is a free tool for automated trying out across exceptional browsers. Requirement: Selenium – Selenium Python Introduction and Installation Below are the steps: First, go to the twitter website using this Link.Then click on investigate element by ur
2 min read
Twitter Sentiment Analysis using Python
This article covers the sentiment analysis of any topic by parsing the tweets fetched from Twitter using Python. What is sentiment analysis? Sentiment Analysis is the process of 'computationally' determining whether a piece of writing is positive, negative or neutral. It’s also known as opinion mining, deriving the opinion or attitude of a speaker.
10 min read
Twitter Sentiment Analysis on Russia-Ukraine War Using Python
In this article, we are going to see how we can perform the Twitter sentiment analysis on the Russia-Ukraine War using Python. Twitter Sentiment Analysis on Russia-Ukraine WarThe role of social media in public opinion has been profound and evident since it started gaining attention. Social media allows us to share information in a great capacity an
9 min read
Twitter Sentiment Analysis WebApp Using Flask
This is a web app made using Python and Flask Framework. It has a registration system and a dashboard. Users can enter keywords to retrieve live Twitter text based on the keyword, and analyze it for customer feelings and sentiments. This data can be visualized in a graph. This project, in particular, mines data using a popular “Tweepy” API. Tweepy
19 min read
OAuth Authentication with Flask - Connect to Google, Twitter, and Facebook
In this article, we are going to build a flask application that will use the OAuth protocol to get user information. First, we need to understand the OAuth protocol and its procedure. What is OAuth? OAuth stands for Open Authorization and was implemented to achieve a connection between online services. The OAuth Community Site defines it as “An ope
8 min read
Python Text To Speech | pyttsx module
pyttsx is a cross-platform text to speech library which is platform independent. The major advantage of using this library for text-to-speech conversion is that it works offline. However, pyttsx supports only Python 2.x. Hence, we will see pyttsx3 which is modified to work on both Python 2.x and Python 3.x with the same code. Use this command for I
2 min read
Practice Tags :