Live-streaming tweets and plot its sentimental value in colors on a world map

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
8
down vote

favorite












I want to have someone reviewing and helping me get some good ideas in how to improve my current project.



All information about the project is in the following github link.



I would also appreciate ideas on how to plot in a more efficient way.



Example of the program with 3 minutes in action



Code:



import tweepy, json
from tweepy.api import API
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from textblob import TextBlob
import re

""" Variables
================"""
searches = ['trump'] # max 250 input searches in a list of strings
maxTweets = 100000 # max number of tweets to analyse.

# Get the Authentication keys from another file. (Easier to push to git)
with open("/Users/se/Documents/Lek med Python/pyCharmProjects/dataScienceFromScratch/src/twitterKeys.txt","r") as file:
CONSUMER_KEY = file.readline().rstrip()
CONSUMER_SECRET = file.readline().rstrip()
ACCESS_TOKEN = file.readline().rstrip()
ACCESS_TOKEN_SECRET = file.readline().rstrip()

# Twitter API needs to be validated
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

# The class takes in a stream of tweets and filter out some specific properties.
# It is a live streaming so it will be kind of slow and in this purpose i filter
# the Coordinates which only around 3-4% of all tweets have.
class Stream2Screen(tweepy.StreamListener):
def __init__(self, api=None):
self.api = api or API()
self.counter = 0 # Counts amount of tweets already got



# This is the method that recieves the tweet in the name of data.
def on_data(self, data):
datadict = json.loads(data) # easier to handle in a json file

# Checks so that the coordinate is in the tweet
if (datadict.get('place') != None):
coordinate = calcCenterOfPolygone(datadict['place']['bounding_box']['coordinates'][0])
tweet = cleanTweets(datadict['text'])
sentiment = sentimentAnalysis(tweet)
plotOnMap(coordinate, sentiment.polarity)
print(tweet)
print(sentiment, "n")
self.counter += 1
if(self.counter % 100 == 0):
print(self.counter)

# if we sent a False as return value we close the "pipeline" of streams
if self.counter < maxTweets:
return True
else:
return False

def on_error(self, status_code):
print(status_code)
return True

# Trial to get sentimental value of a tweet using TextBlob
def sentimentAnalysis(tweet):
tweet = TextBlob(cleanTweets(tweet))
return tweet.sentiment

def cleanTweets(tweet):
return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z t])|(w+://S+)", " ", tweet).split())


# Inputs 4 Coordinates in a list and calc the mid point.
def calcCenterOfPolygone(coordinates):
return [sum([x[0] for x in coordinates]) / 4,
sum([x[1] for x in coordinates]) / 4]

# Plots the coordinate on the map
def plotOnMap(centerPoint, polarity):
plt.ion()
color = 'black'
if (polarity < 0): color='r'
elif (polarity > 0): color='g'
m.plot(centerPoint[0], centerPoint[1], marker='o', markersize=5, color=color)
draw_map(m)
plt.pause(0.000001)

# draws the map the first time
def draw_map(m, scale=0.2):
m.shadedrelief(scale=scale)


fig = plt.figure(figsize=(12, 6))
m = Basemap(projection='cyl', resolution=None,
llcrnrlat=-90, urcrnrlat=90,
llcrnrlon=-180, urcrnrlon=180,)

# Starts the twitter feed and searches for the keyword
stream = tweepy.streaming.Stream(auth, Stream2Screen())

while True:
try:
stream.filter(track=searches)
except:
print("error"*10)
continue

draw_map(m)
plt.show()
plt.draw()






share|improve this question

















  • 1




    Does the code currently work as good as expected? Does it reach the required accuracy?
    – Mast
    Feb 6 at 10:11










  • I can say its an working progress. It currently work as it intends to. and a loot of information is on my page about precision. But i'm sort of new to the process so cant be sure its a good optimisation!
    – Edvardsson
    Feb 6 at 12:24






  • 1




    @Mast I've edited out the off-topic portion of the question.
    – 200_success
    Feb 7 at 18:38
















up vote
8
down vote

favorite












I want to have someone reviewing and helping me get some good ideas in how to improve my current project.



All information about the project is in the following github link.



I would also appreciate ideas on how to plot in a more efficient way.



Example of the program with 3 minutes in action



Code:



import tweepy, json
from tweepy.api import API
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from textblob import TextBlob
import re

""" Variables
================"""
searches = ['trump'] # max 250 input searches in a list of strings
maxTweets = 100000 # max number of tweets to analyse.

# Get the Authentication keys from another file. (Easier to push to git)
with open("/Users/se/Documents/Lek med Python/pyCharmProjects/dataScienceFromScratch/src/twitterKeys.txt","r") as file:
CONSUMER_KEY = file.readline().rstrip()
CONSUMER_SECRET = file.readline().rstrip()
ACCESS_TOKEN = file.readline().rstrip()
ACCESS_TOKEN_SECRET = file.readline().rstrip()

# Twitter API needs to be validated
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

# The class takes in a stream of tweets and filter out some specific properties.
# It is a live streaming so it will be kind of slow and in this purpose i filter
# the Coordinates which only around 3-4% of all tweets have.
class Stream2Screen(tweepy.StreamListener):
def __init__(self, api=None):
self.api = api or API()
self.counter = 0 # Counts amount of tweets already got



# This is the method that recieves the tweet in the name of data.
def on_data(self, data):
datadict = json.loads(data) # easier to handle in a json file

# Checks so that the coordinate is in the tweet
if (datadict.get('place') != None):
coordinate = calcCenterOfPolygone(datadict['place']['bounding_box']['coordinates'][0])
tweet = cleanTweets(datadict['text'])
sentiment = sentimentAnalysis(tweet)
plotOnMap(coordinate, sentiment.polarity)
print(tweet)
print(sentiment, "n")
self.counter += 1
if(self.counter % 100 == 0):
print(self.counter)

# if we sent a False as return value we close the "pipeline" of streams
if self.counter < maxTweets:
return True
else:
return False

def on_error(self, status_code):
print(status_code)
return True

# Trial to get sentimental value of a tweet using TextBlob
def sentimentAnalysis(tweet):
tweet = TextBlob(cleanTweets(tweet))
return tweet.sentiment

def cleanTweets(tweet):
return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z t])|(w+://S+)", " ", tweet).split())


# Inputs 4 Coordinates in a list and calc the mid point.
def calcCenterOfPolygone(coordinates):
return [sum([x[0] for x in coordinates]) / 4,
sum([x[1] for x in coordinates]) / 4]

# Plots the coordinate on the map
def plotOnMap(centerPoint, polarity):
plt.ion()
color = 'black'
if (polarity < 0): color='r'
elif (polarity > 0): color='g'
m.plot(centerPoint[0], centerPoint[1], marker='o', markersize=5, color=color)
draw_map(m)
plt.pause(0.000001)

# draws the map the first time
def draw_map(m, scale=0.2):
m.shadedrelief(scale=scale)


fig = plt.figure(figsize=(12, 6))
m = Basemap(projection='cyl', resolution=None,
llcrnrlat=-90, urcrnrlat=90,
llcrnrlon=-180, urcrnrlon=180,)

# Starts the twitter feed and searches for the keyword
stream = tweepy.streaming.Stream(auth, Stream2Screen())

while True:
try:
stream.filter(track=searches)
except:
print("error"*10)
continue

draw_map(m)
plt.show()
plt.draw()






share|improve this question

















  • 1




    Does the code currently work as good as expected? Does it reach the required accuracy?
    – Mast
    Feb 6 at 10:11










  • I can say its an working progress. It currently work as it intends to. and a loot of information is on my page about precision. But i'm sort of new to the process so cant be sure its a good optimisation!
    – Edvardsson
    Feb 6 at 12:24






  • 1




    @Mast I've edited out the off-topic portion of the question.
    – 200_success
    Feb 7 at 18:38












up vote
8
down vote

favorite









up vote
8
down vote

favorite











I want to have someone reviewing and helping me get some good ideas in how to improve my current project.



All information about the project is in the following github link.



I would also appreciate ideas on how to plot in a more efficient way.



Example of the program with 3 minutes in action



Code:



import tweepy, json
from tweepy.api import API
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from textblob import TextBlob
import re

""" Variables
================"""
searches = ['trump'] # max 250 input searches in a list of strings
maxTweets = 100000 # max number of tweets to analyse.

# Get the Authentication keys from another file. (Easier to push to git)
with open("/Users/se/Documents/Lek med Python/pyCharmProjects/dataScienceFromScratch/src/twitterKeys.txt","r") as file:
CONSUMER_KEY = file.readline().rstrip()
CONSUMER_SECRET = file.readline().rstrip()
ACCESS_TOKEN = file.readline().rstrip()
ACCESS_TOKEN_SECRET = file.readline().rstrip()

# Twitter API needs to be validated
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

# The class takes in a stream of tweets and filter out some specific properties.
# It is a live streaming so it will be kind of slow and in this purpose i filter
# the Coordinates which only around 3-4% of all tweets have.
class Stream2Screen(tweepy.StreamListener):
def __init__(self, api=None):
self.api = api or API()
self.counter = 0 # Counts amount of tweets already got



# This is the method that recieves the tweet in the name of data.
def on_data(self, data):
datadict = json.loads(data) # easier to handle in a json file

# Checks so that the coordinate is in the tweet
if (datadict.get('place') != None):
coordinate = calcCenterOfPolygone(datadict['place']['bounding_box']['coordinates'][0])
tweet = cleanTweets(datadict['text'])
sentiment = sentimentAnalysis(tweet)
plotOnMap(coordinate, sentiment.polarity)
print(tweet)
print(sentiment, "n")
self.counter += 1
if(self.counter % 100 == 0):
print(self.counter)

# if we sent a False as return value we close the "pipeline" of streams
if self.counter < maxTweets:
return True
else:
return False

def on_error(self, status_code):
print(status_code)
return True

# Trial to get sentimental value of a tweet using TextBlob
def sentimentAnalysis(tweet):
tweet = TextBlob(cleanTweets(tweet))
return tweet.sentiment

def cleanTweets(tweet):
return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z t])|(w+://S+)", " ", tweet).split())


# Inputs 4 Coordinates in a list and calc the mid point.
def calcCenterOfPolygone(coordinates):
return [sum([x[0] for x in coordinates]) / 4,
sum([x[1] for x in coordinates]) / 4]

# Plots the coordinate on the map
def plotOnMap(centerPoint, polarity):
plt.ion()
color = 'black'
if (polarity < 0): color='r'
elif (polarity > 0): color='g'
m.plot(centerPoint[0], centerPoint[1], marker='o', markersize=5, color=color)
draw_map(m)
plt.pause(0.000001)

# draws the map the first time
def draw_map(m, scale=0.2):
m.shadedrelief(scale=scale)


fig = plt.figure(figsize=(12, 6))
m = Basemap(projection='cyl', resolution=None,
llcrnrlat=-90, urcrnrlat=90,
llcrnrlon=-180, urcrnrlon=180,)

# Starts the twitter feed and searches for the keyword
stream = tweepy.streaming.Stream(auth, Stream2Screen())

while True:
try:
stream.filter(track=searches)
except:
print("error"*10)
continue

draw_map(m)
plt.show()
plt.draw()






share|improve this question













I want to have someone reviewing and helping me get some good ideas in how to improve my current project.



All information about the project is in the following github link.



I would also appreciate ideas on how to plot in a more efficient way.



Example of the program with 3 minutes in action



Code:



import tweepy, json
from tweepy.api import API
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from textblob import TextBlob
import re

""" Variables
================"""
searches = ['trump'] # max 250 input searches in a list of strings
maxTweets = 100000 # max number of tweets to analyse.

# Get the Authentication keys from another file. (Easier to push to git)
with open("/Users/se/Documents/Lek med Python/pyCharmProjects/dataScienceFromScratch/src/twitterKeys.txt","r") as file:
CONSUMER_KEY = file.readline().rstrip()
CONSUMER_SECRET = file.readline().rstrip()
ACCESS_TOKEN = file.readline().rstrip()
ACCESS_TOKEN_SECRET = file.readline().rstrip()

# Twitter API needs to be validated
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

# The class takes in a stream of tweets and filter out some specific properties.
# It is a live streaming so it will be kind of slow and in this purpose i filter
# the Coordinates which only around 3-4% of all tweets have.
class Stream2Screen(tweepy.StreamListener):
def __init__(self, api=None):
self.api = api or API()
self.counter = 0 # Counts amount of tweets already got



# This is the method that recieves the tweet in the name of data.
def on_data(self, data):
datadict = json.loads(data) # easier to handle in a json file

# Checks so that the coordinate is in the tweet
if (datadict.get('place') != None):
coordinate = calcCenterOfPolygone(datadict['place']['bounding_box']['coordinates'][0])
tweet = cleanTweets(datadict['text'])
sentiment = sentimentAnalysis(tweet)
plotOnMap(coordinate, sentiment.polarity)
print(tweet)
print(sentiment, "n")
self.counter += 1
if(self.counter % 100 == 0):
print(self.counter)

# if we sent a False as return value we close the "pipeline" of streams
if self.counter < maxTweets:
return True
else:
return False

def on_error(self, status_code):
print(status_code)
return True

# Trial to get sentimental value of a tweet using TextBlob
def sentimentAnalysis(tweet):
tweet = TextBlob(cleanTweets(tweet))
return tweet.sentiment

def cleanTweets(tweet):
return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z t])|(w+://S+)", " ", tweet).split())


# Inputs 4 Coordinates in a list and calc the mid point.
def calcCenterOfPolygone(coordinates):
return [sum([x[0] for x in coordinates]) / 4,
sum([x[1] for x in coordinates]) / 4]

# Plots the coordinate on the map
def plotOnMap(centerPoint, polarity):
plt.ion()
color = 'black'
if (polarity < 0): color='r'
elif (polarity > 0): color='g'
m.plot(centerPoint[0], centerPoint[1], marker='o', markersize=5, color=color)
draw_map(m)
plt.pause(0.000001)

# draws the map the first time
def draw_map(m, scale=0.2):
m.shadedrelief(scale=scale)


fig = plt.figure(figsize=(12, 6))
m = Basemap(projection='cyl', resolution=None,
llcrnrlat=-90, urcrnrlat=90,
llcrnrlon=-180, urcrnrlon=180,)

# Starts the twitter feed and searches for the keyword
stream = tweepy.streaming.Stream(auth, Stream2Screen())

while True:
try:
stream.filter(track=searches)
except:
print("error"*10)
continue

draw_map(m)
plt.show()
plt.draw()








share|improve this question












share|improve this question




share|improve this question








edited Feb 7 at 18:37









200_success

123k14143401




123k14143401









asked Feb 6 at 6:55









Edvardsson

415




415







  • 1




    Does the code currently work as good as expected? Does it reach the required accuracy?
    – Mast
    Feb 6 at 10:11










  • I can say its an working progress. It currently work as it intends to. and a loot of information is on my page about precision. But i'm sort of new to the process so cant be sure its a good optimisation!
    – Edvardsson
    Feb 6 at 12:24






  • 1




    @Mast I've edited out the off-topic portion of the question.
    – 200_success
    Feb 7 at 18:38












  • 1




    Does the code currently work as good as expected? Does it reach the required accuracy?
    – Mast
    Feb 6 at 10:11










  • I can say its an working progress. It currently work as it intends to. and a loot of information is on my page about precision. But i'm sort of new to the process so cant be sure its a good optimisation!
    – Edvardsson
    Feb 6 at 12:24






  • 1




    @Mast I've edited out the off-topic portion of the question.
    – 200_success
    Feb 7 at 18:38







1




1




Does the code currently work as good as expected? Does it reach the required accuracy?
– Mast
Feb 6 at 10:11




Does the code currently work as good as expected? Does it reach the required accuracy?
– Mast
Feb 6 at 10:11












I can say its an working progress. It currently work as it intends to. and a loot of information is on my page about precision. But i'm sort of new to the process so cant be sure its a good optimisation!
– Edvardsson
Feb 6 at 12:24




I can say its an working progress. It currently work as it intends to. and a loot of information is on my page about precision. But i'm sort of new to the process so cant be sure its a good optimisation!
– Edvardsson
Feb 6 at 12:24




1




1




@Mast I've edited out the off-topic portion of the question.
– 200_success
Feb 7 at 18:38




@Mast I've edited out the off-topic portion of the question.
– 200_success
Feb 7 at 18:38















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186891%2flive-streaming-tweets-and-plot-its-sentimental-value-in-colors-on-a-world-map%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186891%2flive-streaming-tweets-and-plot-its-sentimental-value-in-colors-on-a-world-map%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?