-
Notifications
You must be signed in to change notification settings - Fork 0
/
processors.py
48 lines (27 loc) · 949 Bytes
/
processors.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
# Text processing functions
# Import required modules
import re
def remove_html_tags(text):
"""Remove HTML tags from the text"""
tags = re.compile('<.*?>|&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-f]{1,6});')
text = re.sub(tags, ' ', text)
return text
def remove_punctuation(text):
"""Remove all punctuation from anywhere in the text"""
text = re.sub(r'[^\w\s]', ' ', text)
return text
def remove_digits(text):
"""Remove all digits from anywhere in the text"""
text = re.sub(r'\d+', ' ', text)
return text
def remove_single_chars(text):
"""Remove all single chars from the text"""
text = re.sub(r'\b\w\b', ' ', text)
return text
def remove_extra_spaces(text):
"""Remove extra spaces from the text"""
# Replace multiple spaces with single ones
text = re.sub(r"\s+", ' ', text)
# Remove leading and trailing spaces
text = re.sub(r"^\s+|\s+$", "", text)
return text