import re import streamlit as st import nltk def remove_space_before_punctuation(text): """ Removes space before punctuation. """ text = re.sub(r"\s+\*\*([.;,?!])\*\*", "\1", text) return text def make_bold_part(token, num): """ Makes bold text. """ if len(token) <= 2: return token else: return f"**{token[0:num]}**{token[num:]}" def split_tokens(text, num, lang): """ Splits a text into tokens. """ paragraphs = text.split("\n\n") new_paragraphs = [] for paragraph in paragraphs: tokens = nltk.word_tokenize(paragraph, language=lang) tokens = [make_bold_part(token, num) for token in tokens] par = remove_space_before_punctuation(" ".join(tokens)) new_paragraphs.append(par) return "\n\n".join(new_paragraphs) def write_app(): """ Write a streamlit app with an input box, a numerical slider from 1 to 5 and an output box. :return: """ st.title("Texte Rapide") st.sidebar.title("Choisis le niveau de gras") num = st.sidebar.slider("Number", 1, 10, 5) st.sidebar.text(f"Niveau de gras: {num}") # choose a language lang = st.sidebar.selectbox(label="Langue", options=["french", "english"], index=0) st.sidebar.text(f"Langue: {lang}") text = st.text_area("Text", "Entre ton texte ici") bouton = st.button("Envoyer") if bouton: st.markdown(f"{split_tokens(text, num, lang)}") def main(): write_app() if __name__ == "__main__": main()