-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
82 lines (64 loc) · 4.22 KB
/
Copy pathsearch.py
File metadata and controls
82 lines (64 loc) · 4.22 KB
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
79
80
from json import load
from PIL import Image
import requests
from config import NUMBER_OF_SUBREDDITS, NUMBER_OF_SUBMISSIONS_PER_SUBREDDIT
import streamlit as st
from streamlit_lottie import st_lottie
import reddit
import ml
USERS = 0
INFO = 1
NAMES = 2
def view():
# -- Style --
def load_lottie_url(url):
r = requests.get(url)
if r.status_code != 200:
return None
return r.json()
def local_css(file_name):
with open(file_name) as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
local_css("style/styles.css")
# -- Loading assets --
lottie_gif = load_lottie_url("https://assets6.lottiefiles.com/packages/lf20_5wucvmas.json")
# -- Body --
with st.container():
#st_lottie(lottie_gif, height=220, key=2)
st.markdown("<h6 style='text-align: center; margin: 0; padding-bottom: 0; font-weight:normal'>results for:</h6><h6 style='text-align: center; padding-top:0; font-size:40px'> r/" + st.session_state.search_input + "</h6>", unsafe_allow_html=True)
#st.markdown("<h6 style='text-align: center; margin: 0; padding: 0; font-size:60px'>" + st.session_state.search_input + "</h6>", unsafe_allow_html=True)
with st.container():
buff, search_col, buff2 = st.columns((1, 3, 1))
with search_col:
st.write("##")
try:
with st.spinner("Loading statistics..."):
subreddit_name = reddit.get_real_subreddit_name(st.session_state.search_input, st.session_state.subreddit_data)
interlinked_subreddits_by_user = reddit.get_interlinked_subreddits(subreddit_name, st.session_state.overlap_data)
interlinked_subreddits_by_algebra = ml.get_nearest_subreddit_vectors_by_user(subreddit_name, st.session_state.vector_data)
subreddit_comments_wordcloud = ml.generate_wordcloud(subreddit_name, st.session_state.wordcloud_data)
similar_subreddits_by_comment_tfidf = ml.get_nearest_subreddit_vectors_by_comment_tfidf(subreddit_name, st.session_state.comment_tfidf_vector_data)
subreddit_description = reddit.get_subreddit_description(subreddit_name, st.session_state.subreddit_data)
subreddit_metrics = reddit.get_subreddit_metrics(subreddit_name, st.session_state.subreddit_data).style.hide_index()
st.markdown("<h6 style='font-size:20px; font-weight:bold;'>Description</h6>", unsafe_allow_html=True)
st.markdown("<h6 style='font-style:italic; font-weight:normal;'>" + subreddit_description + "</h6>", unsafe_allow_html=True)
st.write("#")
st.markdown("<h6 style='font-size:20px; font-weight:bold;'>Metrics</h6>", unsafe_allow_html=True)
st.dataframe(subreddit_metrics)
st.write("#")
st.markdown("<h6 style='font-size:20px; font-weight:bold;'>Most related subreddits by user overlap percentage</h6>", unsafe_allow_html=True)
st.table(interlinked_subreddits_by_user)
st.write("#")
st.markdown("<h6 style='font-size:20px; font-weight:bold;'>Most related subreddits by vector Euclidean distance</h6>", unsafe_allow_html=True)
# with st.spinner("Calculating user vector overlap..."):
st.table(interlinked_subreddits_by_algebra)
st.write("#")
st.markdown("<h6 style='font-size:20px; font-weight:bold;'>Most related subreddits by comment TF-IDF angular similarity</h6>", unsafe_allow_html=True)
# with st.spinner("Calculating comment similarity..."):
st.table(similar_subreddits_by_comment_tfidf)
st.write("#")
st.markdown("<h6 style='font-size:20px; font-weight:bold;'>Wordcloud of the most common words used in this subreddit</h6>", unsafe_allow_html=True)
with st.spinner("Generating wordcloud... (this will probably take 15-30 seconds)"):
st.pyplot(subreddit_comments_wordcloud)
except KeyError:
st.warning(f"Sorry! The subreddit you have requested is not within the top {NUMBER_OF_SUBREDDITS} subreddits. Please try another subreddit.")