Create a Tag Cloud of Tweets Using Python

No comments
Here I will show you a simple code (the full version is on GitHub: https://github.com/johnbyron/pyTagTweets) to create a tag cloud based on the content of tweets talking about a given query and save it as a png.

Before starting you will need to install some Python libraries:

First of all you will need to authenticate using the 4 secrets associated to your Twitter developer account (more info here: https://dev.twitter.com/discussions/631):

 my_oauth = OAuth1(***,  
          client_secret=***,  
          resource_owner_key=***,  
          resource_owner_secret=***)  

Then you will need to create the URL associated with your query:

 complete_url = 'https://api.twitter.com/1.1/search/tweets.json?q=hello  

Here for example we are retrieving the most recent tweets containing the world "hello".

After that, you will just need to create your tag cloud, here we are using a loop to update the tagcloud every wait_time milliseconds.

 while True:  
   my_text = ''  
   r = requests.get(complete_url, auth=my_oauth)  
   tweets = r.json()  
   for tweet in tweets['statuses']:  
     text = tweet['text'].lower()  
     text = ''.join(ch for ch in text if ch not in punctuation) # exclude punctuation from tweets  
     important_words = text  
     my_text += important_words  
   words = my_text.split()  
   counts = Counter(words)  
   for word in stop_words:  
     del counts[word]  
   for key in counts.keys():  
     if len(key) < 3 or key.startswith('http'):  
       del counts[key]  
   final = counts.most_common(max_words)  
   max_count = max(final, key=operator.itemgetter(1))[1]  
   final = [(name, count / float(max_count))for name, count in final]  
   tags = make_tags(final, maxsize=max_word_size)  
   create_tag_image(tags, 'cloud_large.png', size=(width, height), layout=layout, fontname='Lobster', background = background_color)  
   print "new png created"  
   time.sleep(wait_time)  

If you want to download the complete version of the code and try it yourself you can find it here: https://github.com/johnbyron/pyTagTweets

Feel free to comment if you encounter any problem.

No comments :

Post a Comment