Upload preprocessing.py
Browse files- preprocessing.py +78 -0
preprocessing.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
replace_list = [
|
| 4 |
+
(r"’", "'"), # Text decoding errors
|
| 5 |
+
(r"â€[œ]?", '"'), # Text decoding errors
|
| 6 |
+
( # Emoticons, HTML tag patterns, and special character sequences
|
| 7 |
+
r"¯\\_\(ツ\)_\/¯|​|</?sp.n>|\?\u200d[♀♂]|\*††\*\*",
|
| 8 |
+
"",
|
| 9 |
+
),
|
| 10 |
+
( # Miscellaneous symbols
|
| 11 |
+
r"[\u260e\u2610\u2611\u261e\u2620\u2639\u2640\u2642\u2661\u2665\u267b\u26a0\u26d4]",
|
| 12 |
+
"",
|
| 13 |
+
),
|
| 14 |
+
( # Dingbats # Miscellaneous symbols
|
| 15 |
+
r"[\u2705\u270a\u270c\u270d\u2714\u2757\u2764\u2795\u2797\u27a1]",
|
| 16 |
+
"",
|
| 17 |
+
),
|
| 18 |
+
( # General punctuations and formatting characters
|
| 19 |
+
r"[\u200b-\u200d\u2022\u202a\u2028\u2039\u203a\u2060-\u2069]",
|
| 20 |
+
"",
|
| 21 |
+
),
|
| 22 |
+
(r"[\u00a7\u00a9\u00ab-\u00ae\u00b0\u00b7\u00bb\u00bf]", ""), # Latin supplements
|
| 23 |
+
(r"[\u0000\u0006-\u0008\u000b-\u001f\u0080-\u009f]", ""), # Control characters
|
| 24 |
+
(r"[\u3040-\u9fff\uac00-\ud7ff]", ""), # CJK characters
|
| 25 |
+
(r"[\u2592\u25a0\u25cb\u25cf]", ""), # Box elements / geometric shapes
|
| 26 |
+
(r"[\U0001f000-\U0001ffff]", ""), # Emoji etc.
|
| 27 |
+
(r"[\ue000-\uf8ff]", ""), # Private use area
|
| 28 |
+
(r"[\ufe00-\ufe0f]", ""), # Variation selectors
|
| 29 |
+
(r"[\u032a\u034f]", ""), # Combining diacritical marks
|
| 30 |
+
(r"[\u061c\u0640]", ""), # Arabic
|
| 31 |
+
(r"[\u0d9a\u0dd4]", ""), # Sinhala
|
| 32 |
+
(r"[\u2116\u2122]", ""), # Letter-like symbols
|
| 33 |
+
(r"[\u2211\u22ef]", ""), # Mathematical operators
|
| 34 |
+
(r"[\u2b07\u2b55]", ""), # Miscellaneous symbols and arrows
|
| 35 |
+
(r"[\uff0a\uff5e]", ""), # Halfwidth and fullwidth forms
|
| 36 |
+
(r"[\u02c4]", ""), # Modifier letter up arrowhead
|
| 37 |
+
(r"[\u2076]", ""), # Superscript six
|
| 38 |
+
(r"[\u20e3]", ""), # Combining enclosing keycap
|
| 39 |
+
(r"[\u2191]", ""), # Upwards arrow
|
| 40 |
+
(r"[\u2320]", ""), # Top half integral
|
| 41 |
+
(r"[\ufeff]", ""), # Zero width no-break space
|
| 42 |
+
(r"[\u00a0\u2002-\u200a\u3000]", " "), # Special space characters
|
| 43 |
+
( # Small quotation mark, accent mark, or prime symbol
|
| 44 |
+
r"[\u00b4\u02bb\u02cb\u2018\u2019\u2032]",
|
| 45 |
+
"'",
|
| 46 |
+
),
|
| 47 |
+
( # Diaeresis, double quotation mark, or double prime symbol
|
| 48 |
+
r"[\u00a8\u201c\u201d\u2033\u275d\u275e]",
|
| 49 |
+
'"',
|
| 50 |
+
),
|
| 51 |
+
( # Various types of hyphens, dashes, or the minus sign
|
| 52 |
+
r"[\u2010\u2011\u2013\u2014\u2015\u2212]",
|
| 53 |
+
"-",
|
| 54 |
+
),
|
| 55 |
+
(r"[\u201a\u201e\uff0c]", ","), # Low quotation mark or a fullwidth comma
|
| 56 |
+
(r"[\u203c\uff01]", "!"), # Double exclamation mark or a fullwidth exclamation mark
|
| 57 |
+
(r"[\u300a\u3010\uff08]", "("), # Various types of left brackets
|
| 58 |
+
(r"[\u300b\u3011\uff09]", ")"), # Various types of right brackets
|
| 59 |
+
(r"[\u2248\uff1d]", "="), # Various types of equals sign
|
| 60 |
+
(r"[\u2026]", "..."), # Horizontal ellipsis
|
| 61 |
+
(r"[\uff1a]", ":"), # Fullwidth colon
|
| 62 |
+
(r"[\uff1b]", ";"), # Fullwidth semicolon
|
| 63 |
+
(r"[\u00d7]", "x"), # Multiplication sign
|
| 64 |
+
(r"[13][a-km-zA-HJ-NP-Z1-9]{25,34}", ""), # Bitcoin addresses
|
| 65 |
+
(r"[\w\-\.]+@([\w\-]+\.)+[\w\-]{2,4}", ""), # Email addresses
|
| 66 |
+
]
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def preprocess_text(text: str) -> str:
|
| 70 |
+
for pattern, repl in replace_list:
|
| 71 |
+
text = re.sub(pattern, repl, text)
|
| 72 |
+
return text
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
preprocess_text(
|
| 77 |
+
"【 Reminder 】 Your system devices has been Hacked 【 National Security Agency 】 Authority-11622272"
|
| 78 |
+
)
|