import numpy as np
import pandas as pd
import seaborn as sns

import os
import sys
sys.path.append(os.path.join(os.getcwd(), "src"))

import util.io as mio
import util.plotting as mplot
from model.conversationDataframe import ConversationDataframe
from stats.wordsCountStats import WordsCountStats
from stats.iConvStats import IConvStats
from util import statsUtil

%matplotlib notebook
sns.set_context("paper")

%load_ext autoreload
%autoreload 2

# filepath to your conversation file. You can use my conversations examples for testing
FILEPATH = "src/resources/unittest/test_plotting.txt"
# load conversation
conv = ConversationDataframe(FILEPATH)
conv.loadMessages()
msgs = conv.messages
msgs.head()

"""
Explanation: Table of Contents

Intro
Word Count by Sender
Top N Words
Top Words Total
Top Words by Sender
Top Unbalanced Words


Words Used Just By


Word Count by Month
Total
By Sender


Word Count by Hour
Total
By Sender


Word Trend [WIP]
Total
Words with highest variability



Intro
This notebook explores different words-specific stats together with their visualization.
End of explanation
"""


# load sender stats
sender_stats = conv.stats.generateStats(IConvStats.STATS_NAME_WORDCOUNT, ngram_range=(1,1))
sender_word_count = sender_stats.wordsCount
sender_word_count.head(3)

"""
Explanation: Word Count by Sender
Consider ovarall word usage comparisong between senders
End of explanation
"""


total_word_count = sender_stats.getWordsCount()
total_word_count.sort_values(ascending=False)[:5]

"""
Explanation: Top N Words
Most frequent words, overall (total) and sender specific.
Top Words Total
End of explanation
"""


sender_stats.getWordsCount(sender='s1').sort_values(ascending=False)[:5]

"""
Explanation: Top Words by Sender
End of explanation
"""


unbalances_word_count = (sender_word_count.loc['s1']-sender_word_count.loc['s2']).abs()
unbalances_word_count.sort_values(ascending=False)[:5]

"""
Explanation: Top Unbalanced Words
Words with higher frequency difference between senders
End of explanation
"""


sender_stats.getWordsUsedJustBy('s2', 's1').head(5)

"""
Explanation: Words Used Just By
End of explanation
"""


# load month stats
month_stats = conv.stats.generateStats(IConvStats.STATS_NAME_WORDCOUNT, groupByColumns=['year', 'month'])
month_word_count = month_stats.wordsCount
month_word_count.head()
#filepath = conv.statsFolder + '\\' + 'wordCount.txt'
#mio.printDataFrameToFile(stats, filepath)

"""
Explanation: Word Count by Month
Consider words usage along time aggregated by months.
End of explanation
"""


tot_month_word_count = month_stats.getWordsCount(['17','your','youth'])
tot_month_word_count.head()

mplot.plotWordsCount(month_stats, ['17','your','youth'])

"""
Explanation: Total
End of explanation
"""


mplot.plotWordsCount(month_stats, ['17','your','youth'], sender='s1')

# get data to plot (target words)
target_words = ['your','youth']
stats_to_plot = statsUtil.transformStats(month_word_count[target_words], 'word', 'val')

mplot._genericFactorPlot(stats_to_plot, 'month', 'val', {}, "Word count", "count", col='year', row='word')

"""
Explanation: By Sender
End of explanation
"""


# load hours stats
hour_stats = conv.stats.generateStats(IConvStats.STATS_NAME_WORDCOUNT, groupByColumns=['hour'])
hour_word_count = hour_stats.wordsCount
hour_word_count.head(3)

"""
Explanation: Word Count by Hour
Consider words usage along time aggregated by hour.
End of explanation
"""


# plot total for target words
mplot.plotWordsCount(hour_stats, ['17','your','youth'])

"""
Explanation: Total
End of explanation
"""


# get data to plot (target words)
target_words = ['17','your','youth']
stats_to_plot = statsUtil.transformStats(hour_word_count[target_words], 'word', 'val')

mplot._genericFactorPlot(stats_to_plot, 'hour', 'val', {}, "", "", row='word')

"""
Explanation: By Sender
End of explanation
"""


# load month stats
month_stats = conv.stats.generateStats(IConvStats.STATS_NAME_WORDCOUNT, groupByColumns=['year', 'month'])
month_word_count = month_stats.wordsCount

"""
Explanation: Word Trend [WIP]
About words usage change along time.
End of explanation
"""


total_month_word_trend = month_stats._computeWordsTrend(month_word_count)

total_month_word_trend.head()

change_threshold = 20
total_month_word_trend[(total_month_word_trend>change_threshold)|(total_month_word_trend<(-change_threshold))]\
                .dropna(axis=1, how='all').dropna(axis=0, how='all')

"""
Explanation: Total
End of explanation
"""


total_month_word_trend.apply(lambda x:np.std(x), axis=0).sort_values(ascending=False)[:5]

"""
Explanation: Words with highest variability
End of explanation
"""


