data= pd.read_csv("../input/reddit_worldnews_start_to_2016-11-22.csv")

data.shape


"""
Explanation: Credit to Aleksey Bilogur notebooks for inspiration
Lets start by importing the data and having a glimpse of the dataset
End of explanation
"""


data.columns

"""
Explanation: Columns in the dataset
End of explanation
"""


data.dtypes

"""
Explanation: The columns and data type
End of explanation
"""


data.head()

"""
Explanation: A glimpse of the dataset rows and columns
End of explanation
"""


data['up_votes'].sort_values(ascending=False).value_counts().head()

data['down_votes'].sort_values(ascending=False).value_counts().head()

"""
Explanation: Analysis starts here
Posts with the most upvotes
End of explanation
"""


[title for title in data.sort_values('up_votes', ascending=False)['title'][:10]]

"""
Explanation: we can see from above that they are no posts with downvotes
Let us check for most upoted headline
End of explanation
"""


import seaborn as sns
%matplotlib inline
sns.set_style("dark")

data.groupby('date_created')['up_votes'].mean().plot()
data.groupby('date_created')['up_votes'].mean().rolling(window=120).mean().plot(figsize= (12, 4))

"""
Explanation: Let us look at the mean number of posts over a rolling window of 120 days
End of explanation
"""


data.over_18.value_counts()

"""
Explanation: Let us check how many stories were deemed to be over 18
End of explanation
"""


nsfwstory= data[data['over_18']== True]
[story for story in nsfwstory.sort_values('up_votes', ascending=False)['title'][:10]]

"""
Explanation: 320, let's explore the top 10 from over 18 stories
End of explanation
"""


attf= data.author.value_counts()[:20]
attf.plot.bar(figsize= (12, 4))

"""
Explanation: But who is posting to r/worldnews?
End of explanation
"""


taff= data[data['author'] == "davidreiss666"]
#taff.sort_values('up_votes', ascending= False)['title'].value_counts()
[story for story in taff.sort_values('up_votes', ascending=False)['title'][:10]]
#htaff= taff[taff['up_votes'] > 5]

"""
Explanation: It looks like davidreiss666 is the top poster. Let us check the top 10 posts
End of explanation
"""


cand= ['Obama', 'Hillary', 'Trump']
for sole in cand:
   print( data.title.str.contains(sole).value_counts(),sole )



#data.title.str.contains('Trump').value_counts()

from nltk import word_tokenize
tokens = data.title.map(word_tokenize)

def tell_me_about(x):
    x_l = x.lower()
    x_t = x.title()
    return data.loc[tokens.map(lambda sent: x_l in sent or x_t in sent).values]

"""
Explanation: Let us find out how many times Obama, Hillary and Trump were mentioned from 2008 - 2016
End of explanation
"""


tell_me_about("Obama")['title'].values.tolist()[:10]

tell_me_about("Hilary")['title'].values.tolist()[:10]

tell_me_about("Donald")['title'].values.tolist()[:10]

tell_me_about("Trump")['title'].values.tolist()[:10]

"""
Explanation: Have a glimpse of articles about Obama, Trump and Hilary
End of explanation
"""


tell_me_about("Apple")['title'].values.tolist()[:10]

"""
Explanation: And for fun let's look at some headlines related to Apple Inc
End of explanation
"""


