image_output_1.png
from collections import defaultdict

import matplotlib.pyplot as plt

import numpy as np

import numpy as np

import pandas as pd

import pandas as pd

import re

import warnings

import warnings

import pandas as pd
import warnings
warnings.filterwarnings('ignore')
fm = pd.read_csv('../input/ForumMessages.csv')

import pandas as pd
import warnings
warnings.filterwarnings('ignore')
fm = pd.read_csv('../input/ForumMessages.csv')
rep_ids = fm['ReplyToForumMessageId'].unique().tolist()
rep_fm = fm[fm['Id'].isin(rep_ids)]
nb_replies = fm['ReplyToForumMessageId'].value_counts()
rep_fm['NbReplies'] = rep_fm['Id'].map(nb_replies)
import re
def words_wo_quotes(mes):
    mes_wo_q = re.sub('\\[quote=.*\\[/quote\\]', '', mes, flags=re.DOTALL)
    return set([w for w in re.split('[^a-z]', mes_wo_q.lower()) if len(w) >= 2])
rep_fm['Message'] = rep_fm['Message'].astype(str).apply(words_wo_quotes)
from collections import defaultdict
dict_rep = defaultdict(int)
def fill_dict_rep(x):
    for w in x[0]:
        dict_rep[w] += x[1]
rep_fm[['Message', 'NbReplies']].apply(fill_dict_rep, axis=1)
all_messages = fm['Message'].astype(str).apply(words_wo_quotes)
dict_all = dict.fromkeys(dict_rep.keys(), 0)
def fill_dict_all(x):
    for w in x:
        if w in dict_rep:
            dict_all[w] += 1
all_messages.apply(fill_dict_all)
min_proportion = fm.shape[0] / 100
lwords = []
for w in dict_all:
    if dict_all[w] > min_proportion:
        lwords.append([w, dict_rep[w] / dict_all[w]])
lwords = sorted(lwords, key=lambda h: h[1], reverse=True)
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['figure.figsize'] = (10, 12)
x_axis = [wx[1] for wx in lwords[:20]]
y_axis = [wx[0] for wx in lwords[:20]]
plt.barh(range(60, 0, -3), [1] * 20, height=1.5, alpha=0.4)
plt.barh(range(60, 0, -3), x_axis, height=1.5, alpha=0.4)
plt.yticks(np.arange(60.5, 0.5, -3), y_axis, fontsize=20)
plt.xlabel('Proportion of replies', fontsize=16)
plt.ylabel('Word used', fontsize=16)
plt.title('The 20 best words to use to get replies', fontsize=22)
plt.show()