-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclean_stackoverflow_data.py
46 lines (36 loc) · 1.33 KB
/
clean_stackoverflow_data.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
from output_folder import get_output_folder
import pandas as pd
import re
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import os
from output_folder import get_output_folder
def load_questions():
DATASET_PATH = os.getenv('DATASET_PATH')
print(DATASET_PATH)
so_questions = pd.read_csv(DATASET_PATH)
print("Loaded CSV!")
return so_questions
def remove_html_tags(questions):
# Remove code from body
questions.Body = questions.Body.apply(lambda x: re.sub('\<code(.*)code>', '', x))
# Remove HTML Tags from body
questions.Body = questions.Body.apply(lambda x: re.sub('<[^<]+?>', '', x))
questions.Body = questions.Body.apply(lambda x: re.sub('
', '', x))
print("Removed HTML tags!")
def remove_stopwords(questions):
questions.Body = questions.Body.apply(lambda x: ' '.join([w for w in word_tokenize(x) if not w in stopwords.words('english')]))
print("Removed stopwords!")
def save_processed_data(questions):
# Store cleaned and processed data
folder = get_output_folder('processed/')
processed_csv = os.path.join(folder, 'so_questions.csv')
questions.to_csv(processed_csv)
print("Saved new csv!")
def clean_so_data():
questions = load_questions()
remove_html_tags(questions)
remove_stopwords(questions)
save_processed_data(questions)
if __name__ == '__main__':
clean_so_data()