Skip to content

Commit b7765d4

Browse files
iansedanogahjelle
andauthored
QA updates to Replace String Materials (#301)
* QA updates * QA updates 2 * Final QA updates Co-authored-by: Geir Arne Hjelle <[email protected]>
1 parent 98af91d commit b7765d4

File tree

5 files changed

+62
-50
lines changed

5 files changed

+62
-50
lines changed

replace-string-python/transcript_basic.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,3 @@
88
print(transcript.replace("BLASTED", "😤"))
99

1010
print(transcript.replace("BLASTED", "😤").replace("Blast", "😤"))
11-
12-
replacements = [
13-
("BLASTED", "😤"),
14-
("Blast", "😤"),
15-
("2022-08-24T", ""),
16-
("+00:00", ""),
17-
("[support_tom]", "Agent"),
18-
("[johndoe]", "Client"),
19-
]
20-
21-
for old, new in replacements:
22-
transcript = transcript.replace(old, new)
23-
24-
print(transcript)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
REPLACEMENTS = [
2+
("BLASTED", "😤"),
3+
("Blast", "😤"),
4+
("2022-08-24T", ""),
5+
("+00:00", ""),
6+
("[support_tom]", "Agent "),
7+
("[johndoe]", "Client"),
8+
]
9+
10+
transcript = """
11+
[support_tom] 2022-08-24T10:02:23+00:00 : What can I help you with?
12+
[johndoe] 2022-08-24T10:03:15+00:00 : I CAN'T CONNECT TO MY BLASTED ACCOUNT
13+
[support_tom] 2022-08-24T10:03:30+00:00 : Are you sure it's not your caps lock?
14+
[johndoe] 2022-08-24T10:04:03+00:00 : Blast! You're right!
15+
"""
16+
17+
for old, new in REPLACEMENTS:
18+
transcript = transcript.replace(old, new)
19+
20+
print(transcript)
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
from functools import reduce
22

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-
"""
93

10-
replacements = [
4+
REPLACEMENTS = [
115
("BLASTED", "😤"),
126
("Blast", "😤"),
137
("2022-08-24T", ""),
148
("+00:00", ""),
15-
("[support_tom]", "Tom"),
9+
("[support_tom]", "Agent "),
1610
("[johndoe]", "Client"),
1711
]
1812

19-
print(reduce(lambda acc, item: acc.replace(*item), replacements, transcript))
13+
transcript = """
14+
[support_tom] 2022-08-24T10:02:23+00:00 : What can I help you with?
15+
[johndoe] 2022-08-24T10:03:15+00:00 : I CAN'T CONNECT TO MY BLASTED ACCOUNT
16+
[support_tom] 2022-08-24T10:03:30+00:00 : Are you sure it's not your caps lock?
17+
[johndoe] 2022-08-24T10:04:03+00:00 : Blast! You're right!
18+
"""
19+
20+
print(reduce(lambda acc, item: acc.replace(*item), REPLACEMENTS, transcript))
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import re
22

3+
REGEX_REPLACEMENTS = [
4+
(r"blast\w*", "😤"),
5+
(r" [-T:+\d]{25}", ""),
6+
(r"\[support\w*\]", "Agent "),
7+
(r"\[johndoe\]", "Client"),
8+
]
9+
310
transcript = """
411
[support_tom] 2022-08-24T10:02:23+00:00 : What can I help you with?
512
[johndoe] 2022-08-24T10:03:15+00:00 : I CAN'T CONNECT TO MY BLASTED ACCOUNT
613
[support_tom] 2022-08-24T10:03:30+00:00 : Are you sure it's not your caps lock?
714
[johndoe] 2022-08-24T10:04:03+00:00 : Blast! You're right!
815
"""
916

10-
regex_replacements = [
11-
(r"blast\w*", "😤"),
12-
(r" [-T:\+\d]{25}", ""),
13-
(r"\[support\w*\]", "Agent"),
14-
(r"\[johndoe\]", "Client"),
15-
]
16-
17-
for regex_find, regex_replace in regex_replacements:
18-
transcript = re.sub(
19-
regex_find, regex_replace, transcript, flags=re.IGNORECASE
20-
)
17+
for new, old in REGEX_REPLACEMENTS:
18+
transcript = re.sub(new, old, transcript, flags=re.IGNORECASE)
2119

2220
print(transcript)
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import re
22

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}) : (.+)"
113

4+
ENTRY_PATTERN = (
5+
r"\[(.+)\] " # User string, discarding square brackets
6+
r"[-T:+\d]{25} " # Time stamp
7+
r": " # Separator
8+
r"(.+)" # Message
9+
)
1210
BAD_WORDS = ["blast", "dash", "beezlebub"]
1311
CLIENTS = ["johndoe", "janedoe"]
1412

@@ -19,16 +17,25 @@ def censor_bad_words(message):
1917
return message
2018

2119

22-
def censor_clients(user):
23-
for client in CLIENTS:
24-
user = re.sub(rf"\[{client}\]", "Client", user)
25-
return user
20+
def censor_users(user):
21+
if user.startswith("support"):
22+
return "Agent"
23+
elif user in CLIENTS:
24+
return "Client"
25+
else:
26+
raise ValueError(f"unknown client: '{user}'")
2627

2728

2829
def sanitize_message(match):
29-
user, _, message = match.groups()
30-
user = re.sub(r"\[.*support.*\]", "Agent", user)
31-
return f"{censor_clients(user)} : {censor_bad_words(message)}"
30+
user, message = match.groups()
31+
return f"{censor_users(user):<6} : {censor_bad_words(message)}"
3232

3333

34-
print(re.sub(message_pattern, sanitize_message, transcript))
34+
transcript = """
35+
[support_tom] 2022-08-24T10:02:23+00:00 : What can I help you with?
36+
[johndoe] 2022-08-24T10:03:15+00:00 : I CAN'T CONNECT TO MY BLASTED ACCOUNT
37+
[support_tom] 2022-08-24T10:03:30+00:00 : Are you sure it's not your caps lock?
38+
[johndoe] 2022-08-24T10:04:03+00:00 : Blast! You're right!
39+
"""
40+
41+
print(re.sub(ENTRY_PATTERN, sanitize_message, transcript))

0 commit comments

Comments
 (0)