Skip to content

Commit 87f1e65

Browse files
committed
Initial posting of files for course
1 parent 1b0475c commit 87f1e65

File tree

222 files changed

+199129
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

222 files changed

+199129
-0
lines changed

StartHere.html

+13,466
Large diffs are not rendered by default.

StartHere.ipynb

+403
Large diffs are not rendered by default.

ch11/1513-0_RomeoAndJulietOriginalDownload.txt

+5,668
Large diffs are not rendered by default.

ch11/Ch11.html

+16,211
Large diffs are not rendered by default.

ch11/Ch11.ipynb

+2,315
Large diffs are not rendered by default.

ch11/Ch11_fully_executed.ipynb

+2,315
Large diffs are not rendered by default.

ch11/EdwardTheSecond.txt

+3,066
Large diffs are not rendered by default.

ch11/RomeoAndJuliet.txt

+5,260
Large diffs are not rendered by default.

ch11/RomeoAndJulietHeart.png

477 KB
Loading

ch11/mask_circle.png

46.4 KB
Loading

ch11/mask_heart.png

49.8 KB
Loading

ch11/mask_oval.png

29.4 KB
Loading

ch11/mask_star.png

45.9 KB
Loading

ch11/pg20288_OriginalEdwardTheSecondDownload.txt

+3,445
Large diffs are not rendered by default.

ch12/Ch12.html

+16,476
Large diffs are not rendered by default.

ch12/Ch12.ipynb

+2,588
Large diffs are not rendered by default.

ch12/Ch12_fully_executed.ipynb

+2,588
Large diffs are not rendered by default.

ch12/TrendingTwitter.png

93 KB
Loading

ch12/keys.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
consumer_key = 'YourConsumerKey'
2+
consumer_secret = 'YourConsumerSecret'
3+
access_token = 'YourAccessToken'
4+
access_token_secret = 'YourAccessTokenSecret'
5+
6+
mapquest_key = 'YourAPIKey'

ch12/locationlistener.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# locationlistener.py
2+
"""Receives tweets matching a search string and stores a list of
3+
dictionaries containing each tweet's screen_name/text/location."""
4+
import tweepy
5+
from tweetutilities import get_tweet_content
6+
from IPython.display import clear_output
7+
8+
class LocationListener(tweepy.StreamListener):
9+
"""Handles incoming Tweet stream to get location data."""
10+
11+
def __init__(self, api, counts_dict, tweets_list, topic, limit=10):
12+
"""Configure the LocationListener."""
13+
self.tweets_list = tweets_list
14+
self.counts_dict = counts_dict
15+
self.topic = topic
16+
self.TWEET_LIMIT = limit
17+
super().__init__(api) # call superclass's init
18+
19+
def on_status(self, status):
20+
"""Called when Twitter pushes a new tweet to you."""
21+
# get each tweet's screen_name, text and location
22+
tweet_data = get_tweet_content(status, location=True)
23+
24+
# ignore retweets and tweets that do not contain the topic
25+
if (tweet_data['text'].startswith('RT') or
26+
self.topic.lower() not in tweet_data['text'].lower()):
27+
return
28+
29+
self.counts_dict['total_tweets'] += 1 # original tweet
30+
31+
# ignore tweets with no location
32+
if not status.user.location:
33+
return
34+
35+
self.counts_dict['locations'] += 1 # tweet with location
36+
self.tweets_list.append(tweet_data) # store the tweet
37+
clear_output()
38+
print(f'{status.user.screen_name}: {tweet_data["text"]}\n')
39+
40+
# if TWEET_LIMIT is reached, return False to terminate streaming
41+
return self.counts_dict['locations'] < self.TWEET_LIMIT
42+
43+
44+
45+
##########################################################################
46+
# (C) Copyright 2019 by Deitel & Associates, Inc. and #
47+
# Pearson Education, Inc. All Rights Reserved. #
48+
# #
49+
# DISCLAIMER: The authors and publisher of this book have used their #
50+
# best efforts in preparing the book. These efforts include the #
51+
# development, research, and testing of the theories and programs #
52+
# to determine their effectiveness. The authors and publisher make #
53+
# no warranty of any kind, expressed or implied, with regard to these #
54+
# programs or to the documentation contained in these books. The authors #
55+
# and publisher shall not be liable in any event for incidental or #
56+
# consequential damages in connection with, or arising out of, the #
57+
# furnishing, performance, or use of these programs. #
58+
##########################################################################

ch12/sentimentlistener.py

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# sentimentlisener.py
2+
"""Script that searches for tweets that match a search string
3+
and tallies the number of positive, neutral and negative tweets."""
4+
import keys
5+
import preprocessor as p
6+
import sys
7+
from textblob import TextBlob
8+
import tweepy
9+
10+
class SentimentListener(tweepy.StreamListener):
11+
"""Handles incoming Tweet stream."""
12+
13+
def __init__(self, api, sentiment_dict, topic, limit=10):
14+
"""Configure the SentimentListener."""
15+
self.sentiment_dict = sentiment_dict
16+
self.tweet_count = 0
17+
self.topic = topic
18+
self.TWEET_LIMIT = limit
19+
20+
# set tweet-preprocessor to remove URLs/reserved words
21+
p.set_options(p.OPT.URL, p.OPT.RESERVED)
22+
super().__init__(api) # call superclass's init
23+
24+
def on_status(self, status):
25+
"""Called when Twitter pushes a new tweet to you."""
26+
# get the tweet's text
27+
try:
28+
tweet_text = status.extended_tweet.full_text
29+
except:
30+
tweet_text = status.text
31+
32+
# ignore retweets
33+
if tweet_text.startswith('RT'):
34+
return
35+
36+
tweet_text = p.clean(tweet_text) # clean the tweet
37+
38+
# ignore tweet if the topic is not in the tweet text
39+
if self.topic.lower() not in tweet_text.lower():
40+
return
41+
42+
# update self.sentiment_dict with the polarity
43+
blob = TextBlob(tweet_text)
44+
if blob.sentiment.polarity > 0:
45+
sentiment = '+'
46+
self.sentiment_dict['positive'] += 1
47+
elif blob.sentiment.polarity == 0:
48+
sentiment = ' '
49+
self.sentiment_dict['neutral'] += 1
50+
else:
51+
sentiment = '-'
52+
self.sentiment_dict['negative'] += 1
53+
54+
# display the tweet
55+
print(f'{sentiment} {status.user.screen_name}: {tweet_text}\n')
56+
57+
self.tweet_count += 1 # track number of tweets processed
58+
59+
# if TWEET_LIMIT is reached, return False to terminate streaming
60+
return self.tweet_count < self.TWEET_LIMIT
61+
62+
def main():
63+
# configure the OAuthHandler
64+
auth = tweepy.OAuthHandler(keys.consumer_key, keys.consumer_secret)
65+
auth.set_access_token(keys.access_token, keys.access_token_secret)
66+
67+
# get the API object
68+
api = tweepy.API(auth, wait_on_rate_limit=True,
69+
wait_on_rate_limit_notify=True)
70+
71+
# create the StreamListener subclass object
72+
search_key = sys.argv[1]
73+
limit = int(sys.argv[2]) # number of tweets to tally
74+
sentiment_dict = {'positive': 0, 'neutral': 0, 'negative': 0}
75+
sentiment_listener = SentimentListener(api,
76+
sentiment_dict, search_key, limit)
77+
78+
# set up Stream
79+
stream = tweepy.Stream(auth=api.auth, listener=sentiment_listener)
80+
81+
# start filtering English tweets containing search_key
82+
stream.filter(track=[search_key], languages=['en'], is_async=False)
83+
84+
print(f'Tweet sentiment for "{search_key}"')
85+
print('Positive:', sentiment_dict['positive'])
86+
print(' Neutral:', sentiment_dict['neutral'])
87+
print('Negative:', sentiment_dict['negative'])
88+
89+
# call main if this file is executed as a script
90+
if __name__ == '__main__':
91+
main()
92+
93+
##########################################################################
94+
# (C) Copyright 2019 by Deitel & Associates, Inc. and #
95+
# Pearson Education, Inc. All Rights Reserved. #
96+
# #
97+
# DISCLAIMER: The authors and publisher of this book have used their #
98+
# best efforts in preparing the book. These efforts include the #
99+
# development, research, and testing of the theories and programs #
100+
# to determine their effectiveness. The authors and publisher make #
101+
# no warranty of any kind, expressed or implied, with regard to these #
102+
# programs or to the documentation contained in these books. The authors #
103+
# and publisher shall not be liable in any event for incidental or #
104+
# consequential damages in connection with, or arising out of, the #
105+
# furnishing, performance, or use of these programs. #
106+
##########################################################################

0 commit comments

Comments
 (0)