poem_recitation / app.py
ahalamora's picture
Upload app.py
6b04b51
raw
history blame
1.58 kB
import streamlit as st
import string
import random
def replace_with_stars(text, replacement_prob):
result = ""
for char in text:
if char in string.punctuation + ',。!?;:‘’“”【】《》、': # 包含中文标点
result += char
elif char == '\n': # 保留换行符
result += char
else:
if random.random() < replacement_prob:
result += '囗'
else:
result += char
return result
st.title('初中古诗词背诵APP')
with open('poems/poems.txt') as f:
data = f.read()
data = data.split('\n\n')
poems = {}
for poem in data:
poem = poem.split('\n')
id = int(poem[0])
name = poem[1]
poet = poem[2]
content = '\n\n'.join(poem[3:])
poems[name] = {
'id': id,
'name': name,
'poet': poet,
'content': content
}
# 写一个下拉菜单
poem_name = st.selectbox('选择要背诵的诗词', poems.keys())
with st.sidebar:
recitation_mode = st.toggle('背诵模式', value=False)
if recitation_mode:
prob_slider_ = False
replacement_prob = st.slider('遮盖占比', 0.0, 1.0, 0.8, 0.1)
if not recitation_mode:
st.markdown(f'### {poems[poem_name]["name"]}')
st.markdown(f'#### {poems[poem_name]["poet"]}')
st.markdown(f'{poems[poem_name]["content"]}')
else:
st.markdown(f'### {poems[poem_name]["name"]}')
st.markdown(f'#### {poems[poem_name]["poet"]}')
st.markdown(f'{replace_with_stars(poems[poem_name]["content"], replacement_prob)}')