<jupyter_start><jupyter_text>**Import libraries**<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
import GetOldTweets3 as got<jupyter_output><empty_output><jupyter_text>**Function to pull tweets based on general search query**<jupyter_code>"""
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<jupyter_output><empty_output><jupyter_text>**Define parameters and call function**<jupyter_code># 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<jupyter_output><empty_output><jupyter_text>**Reisadvies - Sentiment Analysis**<jupyter_code>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')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10322797579018157, 'fraction_numerical': 0.008742434431741762, 'mean_word_length': 3.7676282051282053, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '330822', 'license': 'MIT', 'n_tokens_mistral': 929, 'n_tokens_neox': 876, 'n_words': 254, 'path': 'twitter/tweets_sentiment.ipynb', 'repo_name': 'casperdamen123/Webscraping', 'size': '2988'}