Skip to content

Commit 2f6ca79

Browse files
committed
uploading for TR
1 parent 65b7518 commit 2f6ca79

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed

replace-string-python/bad_json.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import re
2+
import json
3+
4+
data = """{
5+
birthday = 1991
6+
name = Python
7+
creator = Guido van Rossum
8+
}"""
9+
10+
11+
def try_decode(data):
12+
print("Trying to decode...")
13+
try:
14+
output = json.loads(data)
15+
print("success")
16+
return output
17+
except json.decoder.JSONDecodeError as e:
18+
print("FAIL")
19+
print(e)
20+
21+
22+
try_decode(data)
23+
24+
25+
print(re.sub(pattern=r"(.+)(=)(.+)", repl=r'"\1":"\3"', string=data))
26+
try_decode(data)
27+
28+
29+
data = re.sub(pattern=r"\s+(.+)(\s+=\s+)(.+)", repl=r'"\1":"\3",', string=data)
30+
print(data)
31+
try_decode(data)
32+
33+
34+
# Remove last comma
35+
data = re.sub(pattern=r",$", repl="", string=data, flags=re.MULTILINE)
36+
print(data)
37+
data = try_decode(data)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import re
2+
import json
3+
4+
data = """{
5+
birthday = 1991
6+
name = Python
7+
creator = Guido van Rossum
8+
}"""
9+
10+
11+
def try_decode(data):
12+
print("Trying to decode...")
13+
try:
14+
output = json.loads(data)
15+
print("success")
16+
return output
17+
except json.decoder.JSONDecodeError as e:
18+
print("FAIL")
19+
print(e)
20+
21+
22+
def cleaner(match):
23+
return f'"{match.group(1).strip()}":"{match.group(3).strip()}",'
24+
25+
26+
data = re.sub(pattern=r"(.+)(=)(.+)", repl=cleaner, string=data)
27+
print(data)
28+
29+
# Remove last comma
30+
data = re.sub(pattern=r",(\s)+}", repl="}", string=data, flags=re.MULTILINE)
31+
print(data)
32+
33+
try_decode(data)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
transcript = """
2+
[support_tom] 2022-08-24T10:02:23+00:00 : What can I help you with?
3+
[johndoe] 2022-08-24T10:03:15+00:00 : I CAN'T CONNECT TO MY BLASTED ACCOUNT
4+
[support_tom] 2022-08-24T10:03:30+00:00 : Are you sure it's not your caps-lock?
5+
[johndoe] 2022-08-24T10:04:03+00:00 : Blast! You're right!
6+
"""
7+
8+
print(transcript.replace("BLASTED", "😤"))
9+
10+
print(transcript.replace("BLASTED", "😤").replace("Blast", "😤"))
11+
12+
replacements = [
13+
("BLASTED", "😤"),
14+
("Blast", "😤"),
15+
("2022-08-24T", ""),
16+
("+00:00", ""),
17+
("[support_tom]", "Tom"),
18+
("[johndoe]", "Client"),
19+
]
20+
21+
new_transcript = transcript
22+
for replacement in replacements:
23+
new_transcript = new_transcript.replace(*replacement)
24+
25+
print(new_transcript)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from functools import reduce
2+
3+
transcript = """
4+
[support_tom] 2022-08-24T10:02:23+00:00 : What can I help you with?
5+
[johndoe] 2022-08-24T10:03:15+00:00 : I CAN'T CONNECT TO MY BLASTED ACCOUNT
6+
[support_tom] 2022-08-24T10:03:30+00:00 : Are you sure it's not your caps-lock?
7+
[johndoe] 2022-08-24T10:04:03+00:00 : Blast! You're right!
8+
"""
9+
10+
replacements = [
11+
("BLASTED", "😤"),
12+
("Blast", "😤"),
13+
("2022-08-24T", ""),
14+
("+00:00", ""),
15+
("[support_tom]", "Tom"),
16+
("[johndoe]", "Client"),
17+
]
18+
19+
print(reduce(lambda acc, item: acc.replace(*item), replacements, transcript))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import re
2+
3+
transcript = """
4+
[support_tom] 2022-08-24T10:02:23+00:00 : What can I help you with?
5+
[johndoe] 2022-08-24T10:03:15+00:00 : I CAN'T CONNECT TO MY BLASTED ACCOUNT
6+
[support_tom] 2022-08-24T10:03:30+00:00 : Are you sure it's not your caps-lock?
7+
[johndoe] 2022-08-24T10:04:03+00:00 : Blast! You're right!
8+
"""
9+
10+
new_transcript = re.sub(r" blast\w*", "😤", transcript, flags=re.IGNORECASE)
11+
new_transcript = re.sub(r" [-T:\+\d]{25}", "", new_transcript)
12+
new_transcript = re.sub(r"\[support\w*\]", "Agent", new_transcript)
13+
new_transcript = re.sub(r"\[johndoe\]", "Client", new_transcript)
14+
15+
print(new_transcript)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import re
2+
3+
transcript = """
4+
[support_tom] 2022-08-24T10:02:23+00:00 : What can I help you with?
5+
[johndoe] 2022-08-24T10:03:15+00:00 : I CAN'T CONNECT TO MY BLASTED ACCOUNT
6+
[support_tom] 2022-08-24T10:03:30+00:00 : Are you sure it's not your caps-lock?
7+
[johndoe] 2022-08-24T10:04:03+00:00 : Blast! You're right!
8+
"""
9+
10+
message_pattern = r"(\[.+\]) ([-T:\+\d]{25}) : (.+)"
11+
12+
BAD_WORDS = ["blast", "dash", "beezlebub"]
13+
CLIENTS = ["johndoe", "janedoe"]
14+
15+
16+
def censor_bad_words(message):
17+
output = message
18+
for word in BAD_WORDS:
19+
output = re.sub(rf"{word}\w*", "😤", output, flags=re.IGNORECASE)
20+
return output
21+
22+
23+
def censor_clients(user):
24+
output = user
25+
for client in CLIENTS:
26+
output = re.sub(rf"{client}", "Client", output)
27+
return output
28+
29+
30+
def sanitize_message(match):
31+
user, _, message = match.groups()
32+
user = user.strip("[]")
33+
user = re.sub(r".*support.*", "Agent", user)
34+
return f"{censor_clients(user)} : {censor_bad_words(message)}"
35+
36+
37+
print(re.sub(message_pattern, sanitize_message, transcript))

0 commit comments

Comments
 (0)