Skip to content

Assignment 11 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added exit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12,168 changes: 12,168 additions & 0 deletions index.html

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions io_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import json
# from urllib.request import urlopen

def read_tweets(input_file):
with open(input_file, 'r') as f:
tweets = json.load(f)
return tweets

def read_geojson(input_file):
"""
Read a geojson file

Parameters
----------
input_file : str
The PATH to the data to be read

Returns
-------
gj : dict
An in memory version of the geojson
"""
gj = None

with open(input_file, 'r') as f:
gj = json.load(f)

return gj
Binary file added negative.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added negative.psd
Binary file not shown.
Binary file added neutral.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added neutral.psd
Binary file not shown.
Binary file added neutrallll.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added open.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added positive.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions tweet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import random
import utils
from nltk.sentiment.util import demo_liu_hu_lexicon as classifier

class Tweet(object):

def __init__(self, tweet):

self.tweet_id = tweet['id']
self.date = tweet['created_at']
self.text = tweet['text']
self.username = tweet['user']['name']
self.user_description = tweet['user']['description']
self.followers_count = tweet['user']['followers_count']
self.bounds = tweet['place']['bounding_box']['coordinates'][0]
self.sentiment = self.classify(self.text)

def find_lat(self, n):
return self.bounds[n][1]

def find_lng(self, n):
return self.bounds[n][0]

def bounds(self):
lat = random.uniform(self.find_lat(0), self.find_lat(1))
lng = random.uniform(self.find_lng(0), self.find_lng(1))
return lat, lng

def classify(self, text):
return classifier(text)
1 change: 1 addition & 0 deletions tweets.json

Large diffs are not rendered by default.

159 changes: 159 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import math
import random

def manhattan_distance(a, b):
"""
Compute the Manhattan distance between two points

Parameters
----------
a : tuple
A point in the form (x,y)

b : tuple
A point in the form (x,y)

Returns
-------
distance : float
The Manhattan distance between the two points
"""
distance = abs(a[0] - b[0]) + abs(a[1] - b[1])
return distance



def euclidean_distance(a, b):
"""
Compute the Euclidean distance between two points

Parameters
----------
a : tuple
A point in the form (x,y)

b : tuple
A point in the form (x,y)

Returns
-------

distance : float
The Euclidean distance between the two points
"""
distance = math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)
return distance


def shift_point(point, x_shift, y_shift):
"""
Shift a point by some amount in the x and y directions

Parameters
----------
point : tuple
in the form (x,y)

x_shift : int or float
distance to shift in the x direction

y_shift : int or float
distance to shift in the y direction

Returns
-------
new_x : int or float
shited x coordinate

new_y : int or float
shifted y coordinate

Note that the new_x new_y elements are returned as a tuple

Example
-------
>>> point = (0,0)
>>> shift_point(point, 1, 2)
(1,2)
"""
x = getx(point)
y = gety(point)

x += x_shift
y += y_shift

return x, y


def check_coincident(a, b):
"""
Check whether two points are coincident
Parameters
----------
a : tuple
A point in the form (x,y)

b : tuple
A point in the form (x,y)

Returns
-------
equal : bool
Whether the points are equal
"""
return a == b


def check_in(point, point_list):
"""
Check whether point is in the point list

Parameters
----------
point : tuple
In the form (x,y)

point_list : list
in the form [point, point_1, point_2, ..., point_n]
"""
return point in point_list


def getx(point):
"""
A simple method to return the x coordinate of
an tuple in the form(x,y). We will look at
sequences in a coming lesson.

Parameters
----------
point : tuple
in the form (x,y)

Returns
-------
: int or float
x coordinate
"""

return point.x


def gety(point):
"""
A simple method to return the x coordinate of
an tuple in the form(x,y). We will look at
sequences in a coming lesson.

Parameters
----------
point : tuple
in the form (x,y)

Returns
-------
: int or float
y coordinate
"""

return point.y
122 changes: 122 additions & 0 deletions view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import sys
from PyQt4 import QtGui, QtCore, QtWebKit
import tweet
import folium
import io_geojson
from nose.tools import set_trace

class View(QtGui.QMainWindow):

def __init__(self):
self.fetched_tweets = []
super(View, self).__init__()
self.initUI()

def initUI(self):

map_ = folium.Map(location=[33.4484, -112.0740])
map_.save(r"./index.html")

exit = QtGui.QAction(QtGui.QIcon('exit.png'), 'Quit', self)
exit.setStatusTip('Exit')
exit.triggered.connect(self.close)
exit.setShortcut('Ctrl+Q')

open_ = QtGui.QAction(QtGui.QIcon('open.png'), 'Open File', self)
open_.setShortcut('Ctrl+F')
open_.setStatusTip('Import JSON')
open_.triggered.connect(self.mapTweets)

positive_tweets = QtGui.QAction(QtGui.QIcon('positive.png'), 'Positive Tweets', self)
positive_tweets.setStatusTip('Show only positive tweets')
positive_tweets.triggered.connect(self.positiveTweets)

negative_tweets = QtGui.QAction(QtGui.QIcon('negative.png'), 'Negative Tweets', self)
negative_tweets.setStatusTip('Show only negative tweets')
negative_tweets.triggered.connect(self.negativeTweets)

neutral_tweets = QtGui.QAction(QtGui.QIcon('neutral.png'), 'Neutral Tweets', self)
neutral_tweets.setStatusTip('Show only neutral tweets')
neutral_tweets.triggered.connect(self.neutralTweets)


self.webView = QtWebKit.QWebView()
self.webView.setHtml(open(r"./index.html").read())
self.setCentralWidget(self.webView)

menubar = self.menuBar()
menu = menubar.addMenu('&File')
menu.addAction(exit)
menu.addAction(open_)

toolbar = self.addToolBar('Quit')
toolbar.addAction(exit)
toolbar = self.addToolBar('Open File')
toolbar.addAction(open_)
toolbar = self.addToolBar('Positive Tweets')
toolbar.addAction(positive_tweets)
toolbar = self.addToolBar('Neutral Tweets')
toolbar.addAction(neutral_tweets)
toolbar = self.addToolBar('Negative Tweets')
toolbar.addAction(negative_tweets)

self.setGeometry(300, 400, 700, 500)
self.setWindowTitle('Map o Tweets')
self.show()
self.webView.show()


def mapTweets(self):
map_ = folium.Map(location=[33.4484, -112.0740])

file = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '~')
if file == '':
return

tweets = io_geojson.read_tweets(file)
for t in tweets:
data = tweet.Tweet(t)
# folium.Marker([33.4484, -112.0740]).add_to(map_)
folium.Marker([data.bounds[0][1], data.bounds[0][0]], popup = data.text).add_to(map_)
self.fetched_tweets.append(data)

map_.save(r"./index.html")
self.webView.setHtml(open("./index.html").read())
self.webView.show()


def positiveTweets(self):
map_ = folium.Map(location=[33.4484, -112.0740])
for t in self.fetched_tweets:
print(t.sentiment)
# I'm not sure why the sentiment isn't saving correctly but that's my problem I think
# still playing around with the Tweet class but it should save sentiment on creation
# ~ Also probably a smarter way to filter these out than O(n)
if t.sentiment == 'Positive':
folium.Marker([t.bounds[0][1], t.bounds[0][0]], popup = t.text).add_to(map_)

def negativeTweets(self):
map_ = folium.Map(location=[33.4484, -112.0740])
for t in self.fetched_tweets:
print(t.sentiment)
if t.sentiment == 'Negative':
folium.Marker([t.bounds[0][1], t.bounds[0][0]], popup = t.text).add_to(map_)

def neutralTweets(self):
map_ = folium.Map(location=[33.4484, -112.0740])
for t in self.fetched_tweets:
print(t.sentiment)
if t.sentiment == 'Neutral':
folium.Marker([t.bounds[0][1], t.bounds[0][0]], popup = t.text).add_to(map_)



def main():
app = QtGui.QApplication(sys.argv)

view = View()

sys.exit(app.exec_())

if __name__ == '__main__':
main()
Loading