-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_texts.py
57 lines (44 loc) · 1.42 KB
/
clean_texts.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
import os
import roman
import re
def load_text(filename):
file = open(filename, "r")
text = file.read()
return text
def save_text(filename, text):
try:
os.makedirs(os.path.dirname(filename))
except FileExistsError:
pass
file = open(filename, "w+")
file.write(text)
def is_number(string): # checks if string is number or Roman Numeral
if string.isdigit():
return True
try:
roman.fromRoman(string)
except roman.InvalidRomanNumeralError:
return False
return True
def clean_text(filename):
text = load_text(filename)
text = re.sub(r'\W+', ' ', text).strip("_")
text = ''.join([word for word in text if not is_number(word)])
text = text.lower()
text = text[70:-20]
write_filename = "./cleaned_texts/" + "/".join(filename.split("/")[2:])
save_text(write_filename, text)
print(write_filename)
# print(text)
def clean_all_texts(texts_dir="./texts"):
authors_dirs = os.listdir(texts_dir)
for authors_dir in authors_dirs:
if authors_dir in [".DS_Store"]:
continue
author_texts = os.listdir(texts_dir + "/" + authors_dir)
for author_text in author_texts:
author_text_filename = texts_dir + "/" + authors_dir + "/" + author_text
# print("author text", author_text_filename)
clean_text(author_text_filename)
if __name__ == "__main__":
clean_all_texts()