Кроме того, я пытаюсь создать два разных облака слов, где каждый щелчок будет отдельным облаком слов:
- 1 клик -> показывает одно постоянное облако слов
- 2 клик -> показывает другое постоянное облако слов. (Я знаю, что использую те же данные о кликах, но не знаю, как их изменить.)
import pandas as pd
import dash
from dash import html
from dash import dcc
from dash.dependencies import Input, Output
import plotly.express as px
# from nltk.tokenize import word_tokenize
# from nltk.corpus import stopwords
from wordcloud import WordCloud
from wordcloud import STOPWORDS
import re
import plotly.graph_objects as go
import os
app = dash.Dash(__name__)
data_parsed = pd.read_csv("parsed-data.csv").sort_values("Date")
presidents = data_parsed["President name"].unique()
files = os.listdir("all-speech-txt/")
app.layout = html.Div(
[
html.H1("Presidential speeches"),
html.Div(
[
dcc.Graph(
id="president_speeches",
clickAnnotationData=None,
selectedData=[{"label": i, "value": i} for i in presidents],
),
]
),
html.Div([dcc.Graph(id="word_cloud"), dcc.Graph(id="word_clouds2")]),
]
)
# Hvordan kan vi få input fra en anden end dropdown.
# Hvor kan vi se hvilken word cloud vi kan bruge.
@app.callback(
Output(component_id="president_speeches", component_property="figure"),
[
Input(component_id="president_speeches", component_property="value"),
],
)
def display_chart(selected_president):
mydata = data_parsed
speech_counts = (
mydata.groupby(by=["President name"]).size().reset_index(name="counts")
)
fig = px.bar(
speech_counts, x="President name", y="counts", category_orders=(data_parsed)
)
return fig
@app.callback(
Output(component_id="word_cloud", component_property="figure"),
[Input(component_id="president_speeches", component_property="clickData")],
)
def first_wordcloud(clicked_data):
if clicked_data == None:
return go.figure()
president = clicked_data["points"][0]["x"]
df_filtered = open(
"all-speech-txt/" + president + ".txt", "r", encoding="utf-8"
).read()
df_filtered = re.sub(r"[^A-Za-z\s]", "", df_filtered)
df_filtered = df_filtered.lower()
stopwords = set(STOPWORDS)
text = " ".join(word for word in df_filtered.split() if word not in stopwords)
wordcloud = WordCloud(
width=800,
height=500,
margin=2,
max_words=25,
min_word_length=5,
background_color="white",
).generate(text)
fig = px.imshow(wordcloud)
fig.update_layout(title=president, xaxis_visible=False, yaxis_visible=False)
return fig
@app.callback(
Output(component_id="word_clouds2", component_property="figure"),
[Input(component_id="president_speeches", component_property="clickData")],
)
def second_wordcloud(clicked_data):
if clicked_data == None:
return go.figure()
president = clicked_data["points"][0]["x"]
df_filtered = open(
"all-speech-txt/" + president + ".txt", "r", encoding="utf-8"
).read()
df_filtered = re.sub(r"[^A-Za-z\s]", "", df_filtered)
df_filtered = df_filtered.lower()
stopwords = set(STOPWORDS)
text = " ".join(word for word in df_filtered.split() if word not in stopwords)
wordcloud = WordCloud(
width=800,
height=500,
margin=2,
max_words=25,
min_word_length=5,
background_color="white",
).generate(text)
fig = px.imshow(wordcloud)
fig.update_layout(title=president, xaxis_visible=False, yaxis_visible=False)
return fig
# enter: http://localhost:8080
if __name__ == "__main__":
app.run(debug=False, port=8070)
Подробнее здесь: https://stackoverflow.com/questions/798 ... fferent-cl
Мобильная версия