Spaces:
Runtime error
Runtime error
Create random_noiser.py
Browse files- random_noiser.py +35 -0
random_noiser.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class RandomNoiser():
|
| 2 |
+
|
| 3 |
+
def __init__(self, n_words):
|
| 4 |
+
self.n_words = n_words
|
| 5 |
+
|
| 6 |
+
def noiser(self, text):
|
| 7 |
+
|
| 8 |
+
self.text = text
|
| 9 |
+
|
| 10 |
+
json_dict = {}
|
| 11 |
+
for i in range(0, self.n_words):
|
| 12 |
+
|
| 13 |
+
lower_text = string.ascii_lowercase
|
| 14 |
+
sl = list(lower_text)
|
| 15 |
+
random_character = random.choice(sl)
|
| 16 |
+
text = re.sub('\W+',' ', self.text.lower())
|
| 17 |
+
list_ = text.split(" ")
|
| 18 |
+
if "" in list_:
|
| 19 |
+
list_.remove("")
|
| 20 |
+
ch = random.choice(list_)
|
| 21 |
+
if ch not in ["", " "]:
|
| 22 |
+
k = ch.replace(random.choice(ch), random.choice(sl))
|
| 23 |
+
else:
|
| 24 |
+
k = ch.replace(ch, random.choice(sl))
|
| 25 |
+
if ch not in json_dict.keys():
|
| 26 |
+
json_dict[ch] = k
|
| 27 |
+
|
| 28 |
+
nl = []
|
| 29 |
+
for x in list_:
|
| 30 |
+
if x not in json_dict.keys():
|
| 31 |
+
nl.append(x)
|
| 32 |
+
else:
|
| 33 |
+
nl.append(json_dict[x])
|
| 34 |
+
|
| 35 |
+
return (" ").join(nl)
|