# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# **Import libraries**

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# %matplotlib inline
import GetOldTweets3 as got

# **Function to pull tweets based on general search query**

# +
"""
Parameters: 
 - text query you want to search
 - max number of most recent tweets to pull from
 - start_date ('yyyy-mm-dd')
 - end_date ('yyyy-mm-dd')
"""

def text_query_to_csv(text_query, count, start_date, end_date):
    # Creation of query object
    tweetCriteria = got.manager.TweetCriteria().setQuerySearch(text_query)\
                                               .setMaxTweets(count)\
                                               .setSince(start_date)\
                                               .setUntil(end_date)
                                                
    # Creation of list that contains all tweets
    tweets = got.manager.TweetManager.getTweets(tweetCriteria)

    # Creating list of chosen tweet data including data, text, username, retweets, favorites & mentions.
    text_tweets = [[tweet.date, tweet.text, tweet.username, tweet.retweets, tweet.favorites, tweet.mentions] for tweet in tweets]

    # Creation of dataframe from tweets
    tweets_df = pd.DataFrame(text_tweets, columns = ['Datetime', 'Text', 'Username', 'Retweets', 'Favorites', 'Mentions'])

    # Converting raw tweets dataframe to csv file
    tweets_df.to_csv('{}-tweets({} to {}).csv'.format(text_query, start_date, end_date), sep=',')
    
    # Return dataFrame for further exploration
    return tweets_df


# -

# **Define parameters and call function**

# +
# Input search query to scrape tweets and name csv file
# Max recent tweets pulls x amount of most recent tweets from that user
text_query = '<NAME>'
count = 1000
start_date = '2018-01-01'
end_date = '2019-02-28'

# Calling function to query X amount of relevant tweets and create a CSV file
df = text_query_to_csv(text_query, count, start_date, end_date)
# -

df

# **Reisadvies - Sentiment Analysis**

from pattern.nl import sentiment

# Generate sentiment score based on text
df['Sentiment Score'] = df['Text'].apply(lambda x: sentiment(x)).apply(lambda x: x[0])


# Function to categorize based on sentiment score
def sentiment_category(x):
    predictedSentiment = 'neutral'
    if x > 0.4:
        predictedSentiment = 'positive'
    elif x < -0.2:
        predictedSentiment = 'negative'
    return predictedSentiment


# Run function on sentiment score
df['Sentiment Category'] = df['Sentiment Score'].apply(sentiment_category)

# Analyze if categories make sense
pd.set_option('display.max_colwidth', None)
df[['Text', 'Sentiment Score', 'Sentiment Category']].sort_values(by=['Sentiment Score'])

sns.countplot(x='Sentiment Category', data=df, palette='rainbow')

StarcoderdataJupyterScriptsDedupFiltered