generated from Amsterdam-Internships/InternshipAmsterdamGeneral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre_processing.py
214 lines (156 loc) · 5.83 KB
/
pre_processing.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Regex
import re
# NLTK
import nltk
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize
from nltk.tokenize import sent_tokenize
# Spacy
import spacy
from spacy.language import Language
from spacy_langdetect import LanguageDetector
# TextBlob
from textblob import TextBlob
# --------------------------------------------------
# FUNCTIONS
def remove_nan(data, column_name):
'''Returns data where values in column_name are not empty (NaN).'''
data = data[data[column_name].notna()]
return data
def clean_translation(text):
'''Returns the (Translated by Google) English text, removes the (Original) text from the text.
sep specifies what separator to separate the text by.'''
sep = "(Original)"
if sep in text:
translation, separator, original = text.partition(sep)
text = translation
text = text.replace("(Translated by Google)", '')
return text
def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
u"\U00002500-\U00002BEF" # chinese char
u"\U00002702-\U000027B0"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
u"\U0001f926-\U0001f937"
u"\U00010000-\U0010ffff"
u"\u2640-\u2642"
u"\u2600-\u2B55"
u"\u200d"
u"\u23cf"
u"\u23e9"
u"\u231a"
u"\ufe0f" # dingbats
u"\u3030"
"]+", flags=re.UNICODE)
return emoji_pattern.sub(r'', text)
#def sent_tok(text):
# '''Sentence tokenize.
# Returns list of sentences in review.'''
#
# sentences = sent_tokenize(text)
#
# return sentences
def clean_string(text):
'''Remove punctuation and special characters.'''
text = text.lower()
text = text.replace("\n", '') # Remove \n
text = text.replace("(translated by google)", '') # Remove (translated by google)
text = re.sub("n’t", ' not', text) # Change n't to not
text = re.sub("'re", ' are', text) # Change 're to are
text = re.sub(r'[^\w\s]', '', text) # Remove punctuation
text = re.sub(" +", " ", text) # Remove multiple spaces
text = re.sub(r"http.*?(?=\s)", "", text) # Remove URL's
text = re.sub("'"," ", text) # Remove apostrophes
return text
def get_lang_detector(nlp, name):
return LanguageDetector()
def remove_non_ENG(text):
nlp = spacy.load("en_core_web_sm")
Language.factory("language_detector", func=get_lang_detector)
nlp.add_pipe('language_detector', last=True)
doc = nlp(text)
lang = doc._.language['language']
score = doc._.language['score']
if str(lang) == 'en':
return text
def remove_stopwords(text):
'''Remove stopwords.'''
stop_words = stopwords.words('english')
filtered_text = ' '.join([word for word in text.split() if word not in (stop_words)])
return filtered_text
def lemmatize_string(text):
'''Lemmatize words in a list.'''
lemmatizer = WordNetLemmatizer()
lemmatized_sentence = ' '.join([lemmatizer.lemmatize(word) for word in text.split()])
return lemmatized_sentence
def word_tok(text):
'''Word tokenize.
Returns list of words in review.'''
words = word_tokenize(text)
return words
#def rating_to_sent(text):
# '''Change Rating to integer score from 1 to 5.'''
#
# texts = str(text)
# text = text.replace("stars", '')
# text = text.replace("star", '')
# text = text.replace(" ", '')
# score = int(text)
#
# '''Change score to positive, negative or neutral'''
#
# if score > 3:
# return 'positive'
# elif score < 3:
# return 'negative'
# else:
# return 'neutral'
def abs_date(text):
'''Change relative date to absolute date'''
text = str(text)
text = text.replace(" ago", "")
text = text.replace("years", "year")
text = text.replace("months", "month")
text = text.replace("weeks", "week")
text = text.replace("days", "day")
text = text.replace("hours", "hour")
text = text.replace("minutes", "minute")
num, metric = text.split(' ')
if num == 'a' or num =='an':
num = 1
num = int(num)
if "year" in metric:
text = 2022 - num
elif "month" in metric and num > 3:
text = 2021
elif "month" in metric and num < 4:
text = 2022
elif "day" in metric or "week" in metric or "hour" in metric or "minute" in metric:
text = 2022
date = int(text)
return date
#def split_train_test(data):
# '''Split dataset into train and test set.'''
#
# shuffled_data = data.sample(frac=1)
#
# shape = shuffled_data.shape[0]
#
# train_size = round(shape * 0.9)
# test_size = round(shape * 0.1)
#
# train_set = data[:train_size]
# test_set = data[test_size:]
#
# return train_set, test_set
def correct_typos(text):
x = TextBlob(text)
corrected_text = x.correct()
return corrected_text