Question: Twitter Project This project aims to build one big program that enables users to search through a bank of twitter posts for all posts containing

Twitter Project

This project aims to build one big program that enables users to search through a bank of twitter posts for all posts containing a keyword and then display a picture of where those posts originated from on a global gps map. All work should be done in a python file called twitter_pic.py.

5 Days of Tweets

This assignment includes a text file for all the tweets done between 2011-08-28 and 2011-09-03. There are about 1.7 million tweets over those 5 days. I have created 2 versions of the text file so you can choose to work with a smaller file until you have the code correctly working.

5_tweets.txt

tweets.txt

Each line in the file has the following format (all lower case):
[41.386263640000003, -81.494450689999994] 6 2011-08-28 19:02:28 yay. little league world series! 
Throughout this document, these lines are referred to as tweet lines. They contain 4 tab (\t) separated items (bolded if used in this project):

The gps location of the tweet [latitude, longitude].

A geographic area ID.

A time stamp.

The tweet message.

The goal of the assignment is to produce the image below.

 Twitter Project This project aims to build one big program that

To build this program, you must first define the functions described below.

containsWord()

2 Input Parameters: a word (string) and a message (string)

Return: True or False depending on if the word is in the message

Checks to see if one string (called word) is inside another (called msg). Although you could loop through the characters of the 2 strings, it would be complex and inefficient. The best approach is to use the 'in' operator directly. Look at the examples below:

"bob" in "lisallybobtom" ? True

"bob" in "lisallyjohntom" ? False

Below are some example function calls and return values. You should test them by having your program call the function and print out the return value. (You can erase your test code later.)

containsWord("pizza", "my pizza is tasty") ? True

containsWord("dog", "my pizza is tasty") ? False

getTweet()

1 Input Parameter: a tweet line

Return: tweet message

Hint: Split by tabs

Below are some example function calls and return values. You should test them by having your program call the function and print out the return value. (You can erase your test code later.)

getTweet("[41.3, -81.4]\t6\t2011-08-28 19:02:28\tyay. little league world series!") ? "yay. little league world series!"

getTweet("[25.8, -80.2]\t5\t2011-09-03 05:40:14\tpizza rustica #ftw") ? "pizza rustica #ftw"

getLatitude()

1 Input Parameter: a tweet line

Return: the latitude value as a float.

Hint: You will need combinations of splitting and slicing to make this work.

Steps:

Convert a string like: "[25.8, -80.2]\t5\t2011-09-03 05:40:14\tpizza rustica #ftw" into "[25.8, -80.2]"

Convert a string like: "[25.8, -80.2]" into "[25.8"

Convert a string like: "[25.8" into "25.8"

Convert a string like: "25.8" into a float and return.

Below are some example function calls and return values. You should test them by having your program call the function and print out the return value. (You can erase your test code later.)

getLatitude("[41.37, -81.46]\t6\t2011-08-28 19:02:28\tyay. little league world series!") ? 41.37

getLatitude("[25.8, -80.2]\t5\t2011-09-03 05:40:14\tpizza rustica #ftw") ? 25.8

getLongitude()

1 Input Parameter: a tweet line

Return: the longitude value as a float.

Hint: You will need combinations of splitting and slicing to make this work.

Below are some example function calls and return values. You should test them by having your program call the function and print out the return value. (You can erase your test code later.)

getLongitude("[41.37, -81.46]\t6\t2011-08-28 19:02:28\tyay. little league world series!") ? -81.46

getLongitude("[25.8, -80.2]\t5\t2011-09-03 05:40:14\tpizza rustica #ftw") ? -80.2

getGpsPixelX()

1 Input Parameter: a tweet line

Return: the gps pixel x coordinate

Hint: You will need to get the longitude and use the following formula:
x = (longitude + 180) * 500.0/360

Below are some example function calls and return values. You should test them by having your program call the function and print out the return value. (You can erase your test code later.)

getGpsPixelX("[41.3, -81.4]\t6\t2011-08-28 19:02:28\tyay. little league world series!") ? 136.944444444

getGpsPixelX("[25.8, -80.2]\t5\t2011-09-03 05:40:14\tpizza rustica #ftw") ? 138.611111111

getGpsPixelY()

1 Input Parameter: a tweet line

Return: the gps pixel y coordinate

Hint: You will need to get the latitude and use the following formula:
y = 500 - ((latitude + 180) * 500.0/360)

Below are some example function calls and return values. You should test them by having your program call the function and print out the return value. (You can erase your test code later.)

getGpsPixelY("[41.3, -81.4]\t6\t2011-08-28 19:02:28\tyay. little league world series!") ? 192.638888889

getGpsPixelY("[25.8, -80.2]\t5\t2011-09-03 05:40:14\tpizza rustica #ftw") ? 214.166666667

drawGpsPoint()

1 Input Parameter: a tweet line

Return: None

Hint: You will need to get the pixel x and y values and use the following code:
canvas.create_rectangle(x, y, x+1, y+1, fill="white", width=0)

Below is an test/example twitter_pic.py file (excluding your function definitions) which draws 2 gps points.

from Tkinter import * # set up the graphics library. window = Tk() canvas = Canvas(window, width=500, height=500, background="black") canvas.pack() #define all custom functions here. #draw the gps points drawGpsPoint("[41.3, -81.4]\t6\t2011-08-28 19:02:28\tyay. little league world series!") drawGpsPoint("[25.8, -80.2]\t5\t2011-09-03 05:40:14\tpizza rustica #ftw") #complete the drawing mainloop() 

This should produce the following image (with 2 tiny points): enables users to search through a bank of twitter posts for all

The main code below the functions

At this point, you will have all your custom functions defined, and be ready to use them to create the complete program. Your twitter_pic.py file should be similar to the one below.

from Tkinter import * # set up the graphics library. window = Tk() canvas = Canvas(window, width=500, height=500, background="black") canvas.pack() #define all custom functions here. #main code here #complete the drawing mainloop() 

The main code will have to use all of your custom functions to accomplish the following steps.

1)Open tweets.txt, read it, and split into lines.

2)Display (print) that the tweets have been loaded and ask the user to enter in the search word.

3)Initialize a count variable to keep a running total of how many times the search word has been found.

4)For each line in the list of lines:

.Get the tweet message.

.If the keyword is contained in the message.

. Increase the count by 1.

. Draw the gps point.

5)Create the summary string 'The word "?" appears ? times.' with the the correct substituions for question marks.

6)Put the summary string on the canvas by calculating the correct x and y values necessary to place the string at the bottom center using the following canvas.create_text() function.
canvas.create_text(?x?, ?y?, text=summary, fill="white") 

Below is an example where the user wanted to search for the word "pizza".

PS C:\Users\ssiva\Desktop> python twitter_pic.py All Tweets Loaded Enter search word: pizza PS C:\Users\ssiva\Desktop> 
posts containing a keyword and then display a picture of where those

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!