-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_preprocessing.py
61 lines (47 loc) · 1.76 KB
/
text_preprocessing.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
53
54
55
56
57
58
59
60
61
import re
import string
from bs4 import BeautifulSoup
from unidecode import unidecode
from textblob import TextBlob
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
import contractions
import pandas as pd
class TextPreprocessor(BaseEstimator, TransformerMixin):
def __init__(self):
self.stop_words = set(stopwords.words('english'))
self.lemmatizer = WordNetLemmatizer()
def fit(self, X, y=None):
return self
def transform(self, X):
return [self._preprocess(text) for text in X]
def _preprocess(self, text):
# Lowercasing
text = text.lower()
# Remove accented characters
text = unidecode(text)
# Fix contractions
text = contractions.fix(text)
# Remove HTML tags
text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
# remove emojis
text = re.sub(r'[\U00010000-\U0010ffff]', '', text)
# Remove non-letters
text = text.replace('\n', ' ').replace('\r', '').replace('\t', ' ')
# Remove numbers
# text = re.sub(r'\d+', '', text)
# remove punctuation
text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
# remove double spaces
text = re.sub(' +', ' ', text)
# Tokenize text
words = word_tokenize(text)
# Remove stopwords and lemmatize
words = [self.lemmatizer.lemmatize(
word) for word in words if word not in self.stop_words]
return ' '.join(words)