-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSanitzer.py
More file actions
39 lines (27 loc) · 975 Bytes
/
DataSanitzer.py
File metadata and controls
39 lines (27 loc) · 975 Bytes
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
__author__ = 'Raphael'
import re
import nltk
# nltk.download()
from nltk.corpus import stopwords
# RULE_APOSTROPHE = re.compile(r"^[^*$<,>?!']*$")
ENGLISH = "english"
POSSESSIVE_S = "\'s"
CONTRACTION_T = "'"
REF_STOP_WORDS = set(stopwords.words(ENGLISH))
DEBUG = False
class DataSanitizer(object):
@staticmethod
def filterWords(text):
if DEBUG: print 'Before Cleaning: ' + text.rstrip("\r\n")
text = text.replace(POSSESSIVE_S, "") #remove possessive 's
text = text.replace(CONTRACTION_T, "") #remove contraction 't
text = re.sub("[^a-zA-Z]", " ", text) # filter out everything that is not a letter
text = text.lower()
words = text.split()
words = DataSanitizer.removeStopWords(words)
text = " ".join(words)
if DEBUG: print 'After Cleaning: ' + text
return text
@staticmethod
def removeStopWords(words):
return [w for w in words if not w in REF_STOP_WORDS]