-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangemessage.py
More file actions
77 lines (68 loc) · 2.82 KB
/
changemessage.py
File metadata and controls
77 lines (68 loc) · 2.82 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
import watson_developer_cloud
import string
import random
from random import shuffle
import re
# Change the message string before it is sent to Watson API
class ChangeMessageString:
# Change message string, based lon the type passed to function
def changeMessage(message, changeType, swapThis, withThat):
if changeType == 'lowercaseall':
messageString = message.lower()
return messageString
elif changeType == 'uppercaseall':
messageString = message.upper()
return messageString
elif changeType == 'capitalizefirstletters':
messageString = message.title()
return messageString
elif changeType == 'removeallpunctuation':
table = str.maketrans({key: None for key in string.punctuation})
messageString = message.translate(table)
return messageString
elif changeType == 'duplicatealletters':
dup1 = str() # a new, empty string.
for char in message:
dup1 += char + char # strings concatenate using + and +=
return(dup1)
elif changeType == 'removeallvowels':
table = str.maketrans(dict.fromkeys('aeiouAEIOU'))
messageString = message.translate(table)
# messageString = re.sub(r'[AEIOU]', '', message)
return messageString
elif changeType == 'repeatallcharacters':
n = 3
messageString = ''.join([char*n for char in message])
return messageString
elif changeType == 'duplicatespaces':
messageString = re.sub('([ ])',r'\1\1', message)
return messageString
elif changeType == 'triplespaces':
messageString = re.sub('([ ])',r'\1\1\1', message)
return messageString
elif changeType == 'shufflelettersretainspaces':
words = []
for word in message.split():
if len(word) > 1:
words.append(word[0]
+ ''.join(random.sample(
[char for char in word[1:-1]], len(word) - 2))
+ word[-1])
else:
words.append(word)
result = ' '.join(words)
elif changeType == 'swapletters':
messageString = swapletters(message, swapThis, withThat)
return messageString
else
return message
def swapLetters(message, swapThis, withThat):
messageString = message.replace(swapThis, withThat)
return messageString
def garble_word(match):
first, middle, last = match.groups()
middle = list(middle)
shuffle(middle)
return first + ''.join(middle) + last
def garble(sentence):
return RE_GARBLE.sub(garble_word, sentence)