78 lines
2.2 KiB
Python
78 lines
2.2 KiB
Python
|
|
# Copyright (C) 2023 François Pelletier - Je valide ça, service-conseil
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
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)}")
|
|
st.markdown("Développé avec ❤ par [François Pelletier](https://linktr.ee/jevalideca)")
|
|
|
|
|
|
def main():
|
|
write_app()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|