-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
62 lines (38 loc) · 1.3 KB
/
app.py
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
# -*- coding: utf-8 -*-
"""A streamlit app demonstrating the spoonerize package.
"""
import regex
import streamlit
import spoonerize
from spoonerize.wordlists import STOPWORDS, DICTIONARY
EXAMPLE_TEXT = "If you've had too many jelly beans you should take a shower."
#%% Helpers for highlighting changed words.
def mark_text(text):
return '**' + text + '**'
def mark_changes(old, new):
old_words = regex.split('\\b', old)
new_words = regex.split('\\b', new)
new_words_marked = [n if n == o else mark_text(n) for o, n in zip(old_words, new_words)]
return ''.join(new_words_marked)
#%% Input text.
streamlit.title('spoonerisms!')
text = streamlit.text_area('Enter some text:', value=EXAMPLE_TEXT)
#%% Options sidebar.
if streamlit.sidebar.checkbox('valid dictionary words only', value=True):
dictionary = DICTIONARY
else:
dictionary = None
if streamlit.sidebar.checkbox('exclude stopwords (and, but, etc.)', value=True):
stopwords = STOPWORDS
else:
stopwords = ()
maxdist = streamlit.sidebar.radio('maximum distance between words', range(1, 6), index=1)
#%% Apply options and spoonerize.
text_out = spoonerize.spoonerize_text(
text,
maxdist=maxdist,
stopwords=stopwords,
dictionary=dictionary
)
#%% Output text.
streamlit.markdown(mark_changes(text, text_out))