File size: 2,382 Bytes
d763e2a
 
ee9ec43
 
9cbeac4
 
 
ee9ec43
9cbeac4
 
 
 
 
d763e2a
ee9ec43
 
9cbeac4
 
 
ee9ec43
9cbeac4
ee9ec43
 
9cbeac4
ee9ec43
9cbeac4
 
 
 
 
ee9ec43
9cbeac4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import gradio as gr

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.decomposition import LatentDirichletAllocation
from sklearn.feature_extraction.text import CountVectorizer

def concat_comments(sup_comment: list[str], comment: list[str]) -> list[str]:
    format_s = "{s}\n{c}"
    return [
        format_s.format(s=s, c=c) for s, c in zip(sup_comment, comment)
    ]



def main(button, chose_context):
    df = pd.read_csv('./data/results.csv', index_col=0)
    print(chose_context)

    data = concat_comments(df.sup_comment, df.comment)


    subreddits = df.subreddit.value_counts().index[:22]

    weight_counts = {
        t: [
            df[df.Topic_key_word == t].subreddit.value_counts()[subreddit] / df.subreddit.value_counts()[subreddit] for subreddit in subreddits
        ] for t in topics
    }

    irony_percs = {
        t: [
            len(
                df[df.subreddit == subreddit][(df[df.subreddit == subreddit].Topic_key_word == t) & (df[df.subreddit == subreddit].label == 1)]
            ) / 
            len(
                df[df.subreddit == subreddit]
            ) for subreddit in subreddits
        ] for t in topics
    }
    width = 0.9

    fig, ax = plt.subplots(figsize = (10, 7))
    plt.axhline(0.5, color = 'red', ls=":", alpha = .3)

    bottom = np.zeros(len(subreddits))

    for k, v in weight_counts.items():
        p = ax.bar(subreddits, v, width, label=k, bottom=bottom)
        ax.bar(subreddits, irony_percs[k], width - 0.01, bottom=bottom, color = 'black', edgecolor = 'white', alpha = .2, hatch = '\\')
        bottom += v

    ax.set_title("Perc of topics for each subreddit")
    ax.legend(loc="upper right")
    plt.xticks(rotation=70)

    plt.show()


with gr.Blocks() as demo:
    button = gr.Radio(
        label="Plot type",
        choices=['scatter_plot', 'heatmap', 'us_map', 'interactive_barplot', "radial", "multiline"], value='scatter_plot'
    )
    chose_context = gr.Radio(
        label="Context LDA",
        choices=['comment', 'sup comment', 'sup comment + comment'], value='scatter_plot'
    )
    plot = gr.Plot(label="Plot")
    button.change(main, inputs=[button, chose_context], outputs=[plot])
    demo.load(main, inputs=[button], outputs=[plot])


# iface = gr.Interface(fn=greet, inputs="text", outputs="text")
if __name__ == "__main__":
    demo.launch()