Repository navigation
Expand file tree
/
Copy pathstreamlit_text_scrambler.py
More file actions
135 lines (120 loc) · 5.03 KB
/
Copy pathstreamlit_text_scrambler.py
File metadata and controls
135 lines (120 loc) · 5.03 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import streamlit as st
import random
import re
# python -m streamlit run streamlit_text_scrambler1.py
st.title("Text Scrambler App")
# want 3 text boxes:
# 1 to take user input
# 1 to display original (user) input text
# 1 to show scrambled text
user_text = st.text_area(label="User text", placeholder="Enter text here...")
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
numbers = "0123456789"
use_same_mappings = st.checkbox("Use same mappings")
if str(user_text) == "":
user_text = "Enter text here..."
else:
user_text = user_text
# functions to define how to scramble text
def phrase_generation(user_text):
# for each word in text, replace each letter by a random letter from letters
phrase = []
for word in user_text.split(" "):
new_word = []
for letter in word:
# check if letter is not a word, if so do not adjust it
if letter in "[!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]":
new = letter
# check if letter is a number, if so pick a random number
elif letter in numbers:
#new_letter_num = random.randint(0,len(numbers)-1)
#new = numbers[new_letter_num]
new = str(random.randint(0,9))
elif letter in "\n":
new = letter
else:
new_letter_num = random.randint(0,len(letters)-1)
new = letters[new_letter_num]
if letter.isupper():
new = new.upper()
new_word.append(new)
new_phrase = ''.join(new_word)
phrase.append(new_phrase)
phrase = ' '.join(phrase)
return phrase
def phrase_generation_same_mappings(user_text):
# for each word in text, replace each letter by a random letter from letters
xyz=[]
xtras=[]
phrase = []
for word in user_text.split(" "):
new_word = []
for letter in word:
letteri = letter.lower()
if letteri in xyz:
t=[i for i,j in enumerate(xyz) if j==letteri][0] # capture first instance
# use existing match
#xtras.append(xtras[t])
#t=t[0]
#xyz.append(letteri)
new = xtras[t]
else:
xyz.append(letteri)
# check if letter is not a word, if so do not adjust it
if letter in "[!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]":
new = letter
xtras.append(new)
# check if letter is a number, if so pick a random number
elif letter in numbers:
#new_letter_num = random.randint(0,len(numbers)-1)
#new = numbers[new_letter_num]
new = str(random.randint(0,9))
xtras.append(new)
elif letter in "\n":
new = letter
xtras.append(new)
else:
if len(xtras) > 0:
# loop until find an unused letter (eventually all 26 letters will be used)
# get a random number, check if already captured and only continue when not existing entry
num_chk = True
while num_chk:
new_letter_num = random.randint(0,len(letters)-1)
new = letters[new_letter_num]
if new in xtras:
num_chk = True
else:
num_chk = False
xtras.append(new)
else:
new_letter_num = random.randint(0,len(letters)-1)
new = letters[new_letter_num]
xtras.append(new)
if letter.isupper():
new = new.upper()
new_word.append(new)
new_phrase = ''.join(new_word)
phrase.append(new_phrase)
phrase = ' '.join(phrase)
return phrase
def capitalize(text):
punc_filter = re.compile('([.!?;]\s*)')
split_with_punctuation = punc_filter.split(text)
for i,j in enumerate(split_with_punctuation):
if len(j) > 1:
split_with_punctuation[i] = j[0].upper() + j[1:]
text = ''.join(split_with_punctuation)
return text
col1, col2 = st.columns(2)
with col1:
st.header("Original text")
#st.text(user_text)
st.text_area(label="", value=user_text)
with col2:
st.header("Scrambled text")
#st.text(phrase_generation(user_text))
if use_same_mappings:
st.text_area(label="", value=phrase_generation_same_mappings(user_text))
else:
st.text_area(label="", value=phrase_generation(user_text))
#st.text(capitalize(phrase_generation(user_text)))