Spaces:
Runtime error
Runtime error
File size: 1,577 Bytes
6f00770 6b04b51 6f00770 6b04b51 |
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 |
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)}') |