File size: 2,026 Bytes
7f5d38e
 
 
 
 
c90f05d
 
7f5d38e
 
 
 
 
c90f05d
 
7f5d38e
c90f05d
 
 
 
 
 
7f5d38e
 
 
 
 
 
 
 
 
 
c90f05d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f5d38e
 
 
 
c90f05d
 
 
 
7f5d38e
c90f05d
 
7f5d38e
 
c90f05d
 
 
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
import streamlit as st
import pandas as pd
import numpy as np
import plotly.graph_objs as go
from keras.preprocessing.text import Tokenizer
import requests
from bs4 import BeautifulSoup

# Set up the Streamlit app
st.set_page_config(page_title='Keras and Plotly Example')
st.sidebar.title('Word Frequency')

# Load data from Wikipedia
def load_wiki_data(pages):
    data = []
    for page in pages:
        url = f'https://en.wikipedia.org/wiki/{page}'
        response = requests.get(url)
        soup = BeautifulSoup(response.content, 'html.parser')
        text = soup.get_text()
        data.append(text)
    df = pd.DataFrame({'text': data})
    return df

# Create a bar chart of word frequency
def plot_word_frequency(text):
    tokenizer = Tokenizer()
    tokenizer.fit_on_texts(text)
    word_counts = tokenizer.word_counts
    words = list(word_counts.keys())
    counts = list(word_counts.values())

    # Categorize words by type and assign color based on type
    word_types = {}
    for word in words:
        if word.isalpha():
            if word.isupper():
                word_types[word] = 'uppercase'
            elif word.istitle():
                word_types[word] = 'titlecase'
            else:
                word_types[word] = 'lowercase'
        else:
            word_types[word] = 'other'

    colors = {'uppercase': 'red', 'titlecase': 'green', 'lowercase': 'blue', 'other': 'gray'}
    color_list = [colors[word_types[word]] for word in words]

    fig = go.Figure([go.Bar(x=words, y=counts, marker={'color': color_list})])
    fig.update_layout(title='Word Frequency')
    st.plotly_chart(fig)

# Main Streamlit app
pages = ['Python_(programming_language)', 'Data_science', 'Machine_learning']
if st.sidebar.button('Load Wikipedia Data'):
    df = load_wiki_data(pages)
    st.sidebar.write('Data loaded')
else:
    df = pd.DataFrame({'text': []})
    st.sidebar.write('Click "Load Wikipedia Data" to load data')

st.write(df)
text = df['text'].tolist()
if text:
    plot_word_frequency(text)