Erstellen einer interaktiven Wortwolke in Dash + 2 verschiedene Wortwolken für verschiedene KlicksPython

Python-Programme
Anonymous
 Erstellen einer interaktiven Wortwolke in Dash + 2 verschiedene Wortwolken für verschiedene Klicks

Post by Anonymous »

Ich versuche, eine interaktive Wortwolke in Dash zu erstellen. Ist das überhaupt möglich?
Außerdem versuche ich, zwei verschiedene Wortwolken zu erstellen, wobei jeder Klick eine andere Wortwolke ist:
  • 1 Klick -> zeigt eine dauerhafte Wortwolke an
  • 2 Klick -> zeigt eine andere dauerhafte Wortwolke an. (Ich weiß, dass ich dieselben Klickdaten verwende, aber ich weiß nicht, wie ich sie ändern kann.)
Kann jemand helfen?

Code: Select all

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)

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post