-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare.py
38 lines (30 loc) · 1.06 KB
/
prepare.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
import regex as re
import unicodedata
import os
import sys
from bs4 import BeautifulSoup as bs
from bs4 import Comment
from stemming.porter2 import stem
directory = os.path.join(".", "cc_data")
output = "./cc_clean_data/"
def remove_punctuation(text):
return re.sub(r"\p{P}+", "", text)
i = 0
print(directory)
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".dump"):
i += 1
if (i % 1000 == 0):
print(i / 1000, "%")
f = open("./cc_data/" + file, 'r')
soup = bs(f.read(), "html.parser")
comments = soup.findAll(text = lambda text:isinstance(text, Comment))
[comment.extract() for comment in comments]
ps = [s.get_text() for s in soup.find_all('p')]
text = " ".join(ps)
text = re.sub("\s", " ", text)
text = remove_punctuation(text)
text = " ".join([stem(w.lower()) for w in text.strip().split()])
result = open(output + file, 'w')
result.write(text)