-
Notifications
You must be signed in to change notification settings - Fork 0
/
replacements.py
52 lines (37 loc) · 1.22 KB
/
replacements.py
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
import csv
from utils import replace_case_insensitive
import emoji
import re
_replacements = []
_double_question_marks_regex = re.compile('\?+')
_double_exclamation_marks_regex = re.compile('\!+')
def init_replacements():
with open("replacements.csv", encoding="utf-8") as csvfile:
reader = csv.reader(csvfile, delimiter=",")
for row in reader:
if len(row) == 0:
continue
_replacements.append({
"from": row[0],
"to": row[1] if len(row) > 1 else ""
})
def replace_message(str):
if str is None:
return str
str = " " + str + " "
prefixes = [" ", "/", "-", ".", ":", ",", "(", ")"]
suffixes = [" ", "/", "-", ".", ":", ",", "(", ")"]
for p in prefixes:
for s in suffixes:
str = _replace_message_impl(str, p, s)
str = str.strip()
str = emoji.get_emoji_regexp().sub(r'', str)
str = _double_question_marks_regex.sub('?', str)
str = _double_exclamation_marks_regex.sub('!', str)
return str
def _replace_message_impl(str, prefix, suffix):
for r in _replacements:
repl_from = prefix + r["from"].strip() + suffix
repl_to = prefix + r["to"].strip() + suffix
str = replace_case_insensitive(str, repl_from, repl_to)
return str