"""
# Introduction

From Wikipedia: [r/subredditoftheday](https://www.reddit.com/r/subredditoftheday/), .. is a celebration of the interesting communities on reddit.com. Once a day we shine a spotlight on the small, the big, the new and the old. Our mission is to spotlight unique reddit communities and bring the awesome, every damn day.

<center><img src="https://styles.redditmedia.com/t5_2sgno/styles/communityIcon_2wot6zdwqem01.png" width=600></img></center>

### Inspiration comes from Gabriel Preda Analysis https://www.kaggle.com/gpreda/wallstreetbets-reddit-posts-analysis. Thanks to give him credits. 👏
"""
"""
# Analysis preparation

We initialize the packages that we will use in the analysis.
"""
import numpy as np 
import pandas as pd
import matplotlib
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline 
from wordcloud import WordCloud, STOPWORDS
from nltk.sentiment import SentimentIntensityAnalyzer
from textblob import TextBlob
import warnings
warnings.simplefilter("ignore")
"""
We read and glimpse the data.
"""
data_df = pd.read_csv("../input/sub-reddit-of-the-day-posts/subredditoftheday_reddit.csv")
data_df.head()
"""
We also look to things like data quality, for example missing data.
"""
data_df.info()
def missing_data(data):
    total = data.isnull().sum()
    percent = (data.isnull().sum()/data.isnull().count()*100)
    tt = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
    types = []
    for col in data.columns:
        dtype = str(data[col].dtype)
        types.append(dtype)
    tt['Types'] = types
    return(np.transpose(tt))
missing_data(data_df)
"""
Body of posts is missing in approximatively half of the data.
"""
"""
# Data visualization


We will use wordclouds to identify the most frequent words in the titles and body of the posts.

For understanding some of the frequent used terms, it will be useful to consult this resource: [r/subredditoftheday stats](https://subredditstats.com/r/subredditoftheday)

Some examples (from the resource mentioned above):  

1	  pancakeswap	×3717
2	  girlsyouknowirl	×2709
3	  amazing_architecture	×2047
4	  BaddieHermOnlyFans	×1709
5	  SmartGamingDeals	×1315
6	  GrandPieceOnline	×844
7	  thesongofachilles	×668
8	  WKHS	×537
9	  mangacoloring	×466
10	  RushRoyale	×451
11	  FranceDetendue	×410
12	  RustConsole	×395
13	  t3ddyyyy	×383
14	  Ranboo	×356
15	  WANDAVISION	×348
16	  NFT	×336
17	  SpecialHumor	×313
18	  MakersPlace	×296
19	  NarakaBladePoint	×295
20	  pantiesandsocks	×270
21	  Timberborn	×242
22	  LesliePecas	×227
23	  sekulermilliyetciturk	×223
24	  TollbugataBets	×221
25	  GenshinTrades	×213
26	  epoxyhotdog	×200
27	  FuckPierre	×185
28	  InsideJob	×185
29	  AxieInfinity	×182
30	  BinanceSmartChain	×181
"""
def show_wordcloud(data, title=""):
    text = " ".join(t for t in data.dropna())
    stopwords = set(STOPWORDS)
    stopwords.update(["t", "co", "https", "amp", "U", "fuck", "fucking"])
    wordcloud = WordCloud(stopwords=stopwords, scale=4, max_font_size=50, max_words=500,background_color="black").generate(text)
    fig = plt.figure(1, figsize=(16,16))
    plt.axis('off')
    fig.suptitle(title, fontsize=20)
    fig.subplots_adjust(top=2.3)
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.show()
"""
## Title
"""
show_wordcloud(data_df['title'], title = 'Prevalent words in titles')
"""
## Body
"""
show_wordcloud(data_df['body'], title = 'Prevalent words in post bodies')
"""
# Sentiment analysis

## With nltk SentimentIntensityAnalyzer
"""
# borrowed from https://www.kaggle.com/pashupatigupta/sentiments-transformer-vader-embedding-bert
sia = SentimentIntensityAnalyzer()
def find_sentiment(post):
    if sia.polarity_scores(post)["compound"] > 0:
        return "Positive"
    elif sia.polarity_scores(post)["compound"] < 0:
        return "Negative"
    else:
        return "Neutral"       
def plot_sentiment(df, feature, title):
    counts = df[feature].value_counts()
    percent = counts/sum(counts)

    fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12, 5))

    counts.plot(kind='bar', ax=ax1, color='green')
    percent.plot(kind='bar', ax=ax2, color='blue')
    ax1.set_ylabel(f'Counts : {title} sentiments', size=12)
    ax2.set_ylabel(f'Percentage : {title} sentiments', size=12)
    plt.suptitle(f"Sentiment analysis: {title}")
    plt.tight_layout()
    plt.show()
"""
### Title
"""
data_df['title_sentiment'] = data_df['title'].apply(lambda x: find_sentiment(x))
plot_sentiment(data_df, 'title_sentiment', 'Title')
show_wordcloud(data_df.loc[data_df['title_sentiment']=='Positive', 'title'], title = 'Prevalent words in titles (Positive sentiment)')
show_wordcloud(data_df.loc[data_df['title_sentiment']=='Negative', 'title'], title = 'Prevalent words in titles (Negative sentiment)')
show_wordcloud(data_df.loc[data_df['title_sentiment']=='Neutral', 'title'], title = 'Prevalent words in titles (Neutral sentiment)')
"""
### Body
"""
df = data_df.loc[~data_df.body.isna()]
df['body_sentiment'] = df['body'].apply(lambda x: find_sentiment(x))
plot_sentiment(df, 'body_sentiment', 'Body')
show_wordcloud(df.loc[df['body_sentiment']=='Positive', 'body'], title = 'Prevalent words in body (Positive sentiment)')
show_wordcloud(df.loc[df['body_sentiment']=='Negative', 'body'], title = 'Prevalent words in body (Negative sentiment)')
show_wordcloud(df.loc[df['body_sentiment']=='Neutral', 'body'], title = 'Prevalent words in body (Neutral sentiment)')
"""
## With TextBlob
"""
def find_sentiment_polarity_textblob(post):
    blob = TextBlob(post)
    polarity = 0
    for sentence in blob.sentences:
        polarity += sentence.sentiment.polarity
    return polarity

def find_sentiment_subjectivity_textblob(post):
    blob = TextBlob(post)
    subjectivity = 0
    for sentence in blob.sentences:
        subjectivity += sentence.sentiment.subjectivity
    return subjectivity
data_df['title_sentiment_polarity'] = data_df['title'].apply(lambda x: find_sentiment_polarity_textblob(x))
data_df['title_sentiment_subjectivity'] = data_df['title'].apply(lambda x: find_sentiment_subjectivity_textblob(x))
def plot_sentiment_textblob(df, feature, title):
    polarity = df[feature+'_sentiment_polarity']
    subjectivity = df[feature+'_sentiment_subjectivity']

    fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12, 5))

    polarity.plot(kind='kde', ax=ax1, color='magenta')
    subjectivity.plot(kind='kde', ax=ax2, color='green')
    ax1.set_ylabel(f'Sentiment polarity : {title}', size=12)
    ax2.set_ylabel(f'Sentiment subjectivity: {title}', size=12)
    plt.suptitle(f"Sentiment analysis (polarity & subjectivity): {title}")
    plt.tight_layout()
    plt.show()
plot_sentiment_textblob(data_df, "title", 'Title')
df['body_sentiment_polarity'] = df['body'].apply(lambda x: find_sentiment_polarity_textblob(x))
df['body_sentiment_subjectivity'] = df['body'].apply(lambda x: find_sentiment_subjectivity_textblob(x))
plot_sentiment_textblob(df, "body", 'Body')