-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextPreprocessingTransformer.py
48 lines (33 loc) · 1.31 KB
/
TextPreprocessingTransformer.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
from tqdm import tqdm
import re
import nltk
from sklearn.base import TransformerMixin
class TextPreprocessingTransformer(TransformerMixin):
def __init__(self):
pass
def fit(self, X, y=None):
return self
def transform(self, X):
documents = []
for sen in tqdm(range(0, len(X))):
# Remove all the special characters
document = re.sub(r'\W', ' ', str(X[sen]))
# Remove numbers
document = re.sub(r'[0-9]', ' ', document)
# remove all single characters
document = re.sub(r'\s+[a-zA-Z]\s+', ' ', document)
# Remove single characters from the start
document = re.sub(r'\^[a-zA-Z]\s+', ' ', document)
# Substituting multiple spaces with single space
document = re.sub(r'\s+', ' ', document, flags=re.I)
# Removing prefixed 'b'
document = re.sub(r'^b\s+', '', document)
# Converting to Lowercase
document = document.lower()
# Lemmatization
snowball = nltk.SnowballStemmer(language= "german")
document = document.split()
document = [snowball.stem(word) for word in document]
document = ' '.join(document)
documents.append(document)
return documents