-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPre_Processor.py
101 lines (78 loc) · 2.49 KB
/
Pre_Processor.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import re
from helpers import stopWords
import pandas as pd
class PreProcess:
def removeSpecialChars(self, sentence: str):
"""
Removes special characters from a given string
Attributes
----------
sentence: str
Returns
-------
str
A new string without special characters.
"""
return re.sub('[^a-zA-Z0-9 \n\'\"]', '', sentence)
def tokenize(self, sentence: str):
"""
Tokenizes the given sentence.
Attributes
----------
sentence: str
Returns
-------
list
A list of words(tokens).
"""
return [token.strip() for token in sentence.lower().split()]
def removeStopWords(self, words: list):
"""
Removes the stop words from the given list of words
Attributes
----------
words:list
List of tokens
Returns
-------
list
List of given words but with no stop words.
"""
return [word for word in words if word not in stopWords]
def processString(self, sentence: str):
"""
Applies the pre-processing steps to a given strinf
Attributes
----------
sentence: str
Returns
-------
tokens: list
A list of processed tokens
"""
sentence = self.removeSpecialChars(sentence)
tokens = self.tokenize(sentence)
tokens = self.removeStopWords(tokens)
return tokens
def preProcess(self, data: pd.DataFrame):
"""
Applies several pre-processing steps such as tokenization, stemming ...etc to the data.
Attributes
----------
data: pd.DataFrame
A pandas dataframe with sentence and category.
Returns
-------
data: pd.DataFrame
Preprocessed data
"""
processedSeries = list()
for sentence in data["sentence"]:
processedSentence = self.processString(sentence)
processedSeries.append(processedSentence)
# print(processedSeries)
# print(pd.Series(processedSeries))
# data["sentence"].update(pd.Series(processedSeries))
data["tokens"] = pd.Series(processedSeries)
# print(data)
return data