Skip to content
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 modified __pycache__/sentiment_class.cpython-36.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions geolocation_data_retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@ def get_valid_coordinates(location: str, geolocator: Nominatim) -> list:

month = month_as_string(args.input_month)

geoloc_filaname = os.path.join(config['geolocation_data_folder'], f'tweets_with_geolocation_{month}_{args.input_year}.csv')
df_with_coordinates.to_csv(geoloc_filaname, sep='\t', encoding='utf-8', index=False)
geoloc_filename = os.path.join(config['geolocation_data_folder'], f'tweets_with_geolocation_{month}_{args.input_year}.csv')
df_with_coordinates.to_csv(geoloc_filename, sep='\t', encoding='utf-8', index=False)

Large diffs are not rendered by default.

297 changes: 187 additions & 110 deletions jupyter_notebooks/Data_and_Sentiment_Analysis_Covid19_Tweets.ipynb

Large diffs are not rendered by default.

51 changes: 50 additions & 1 deletion sentiment_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
-- Last Updated: 19/09/2020
-------------------------------------------------------------------
"""

import os
import time
import matplotlib.pyplot as plt
from datetime import datetime

from collections import Counter
from pandas import DataFrame
Expand All @@ -17,6 +19,14 @@
from nltk.collocations import BigramCollocationFinder
from nltk import word_tokenize

from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter
from geopy.exc import GeocoderTimedOut


geolocator = Nominatim(user_agent="https://developer.twitter.com/en/apps/17403833")

geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1, max_retries=3, error_wait_seconds=2)

def month_as_string(month_as_int: int) -> str:
"""
Expand All @@ -31,6 +41,27 @@ def month_as_string(month_as_int: int) -> str:
return year_dict[month_as_int]


def get_valid_coordinates(location: str, geolocator: Nominatim) -> list:
"""
Given a string which is pointing to specific location (e.g. 'London'),
return the Latitude and Longitude coordinates of each entry.
If an entry does not correspond to a place (e.g. 'abcdef') then return None.
"""
if (location is not None) and (str(location) != 'nan'):
try:
print(f'Location:.... {location}')
try:
coordinates = geolocator.geocode(location)
lat = coordinates.point[0]
long = coordinates.point[1]
return lat, long
except AttributeError:
return 'No latitude', 'No longitude'
except GeocoderTimedOut:
return get_valid_coordinates(location, geolocator)
else:
return 'No latitude', 'No longitude'

class TwitterSentiment:

def __init__(self, input_df, tweet_column):
Expand Down Expand Up @@ -201,3 +232,21 @@ def plot_sentiment(self, sentiment_month=None, year=None, figsize=(10, 8)) -> No
plt.ylabel('Count', labelpad=8)
plt.xlabel('Sentiment', labelpad=8)
plt.show()

def calculate_geolocation_coordinates(self):
self.df.reset_index(drop=True, inplace=True)
for i in range(0, self.df.shape[0]):
if (i != 0) and (i%100 == 0):
time.sleep(120)
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)

print('Index.. {0}'.format(i))
location = self.df['Location'].iloc[i]

latitude, longitude = get_valid_coordinates(location, geolocator)

self.df.loc[i, 'Latitude'] = latitude
self.df.loc[i, 'Longitude'] = longitude
print('Location found in: [{0}, {1}]'.format(latitude, longitude))