-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
134 lines (110 loc) · 4.44 KB
/
bot.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
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 11 23:38:15 2020
@author: Paulo
"""
import requests
import shutil
from bs4 import BeautifulSoup
import pandas as pd
import os
from os.path import join
import re
# re.findall("<math>[^/]*</math>", soup.text)
# link : soup.find_all("a").attrs["href"]
lovecraft_folder = "C:/Users/Paulo/Documents/GIT/BibleCraftBot-999/lovecraft_books"
quran_folder = "C:/Users/Paulo/Documents/GIT/BibleCraftBot-999/quran"
save_folder = "C:/Users/Paulo/Documents/GIT/BibleCraftBot-999/datasets"
# Getting Lovecraft books
root_url = "http://www.hplovecraft.com/writings/texts/"
resp = requests.get(root_url, stream=True)
soup = BeautifulSoup(resp.content, "lxml")
images_td = soup.find_all("li")
fiction_li = [str(x) for x in images_td if "fiction/" in str(x)]
pages = [x.split("\"")[1] for x in fiction_li]
links = [join(root_url, page) for page in pages]
try:
os.mkdir(save_folder)
except: pass
for link in links:
resp = requests.get(link, stream=True)
soup = BeautifulSoup(resp.content, "lxml")
text = soup.find_all("div")
text = text[1].text.replace("\r\n", " ").replace("\n", "\n\n") #.replace("\u2032", "°") "\u2032" is ° char
book_name = re.search("(\w+).aspx", link).group(1) + ".txt"
with open(join(lovecraft_folder, book_name ), "w", encoding="utf-8") as file:
file.write(text)
texts = []
for book in os.listdir(lovecraft_folder):
with open(join(lovecraft_folder, book), "r", encoding="utf-8") as file:
texts.append(file.read())
texts = "\n".join(texts)
texts = re.sub("[\n ][\n ]+", "\n\n", texts)
with open(join(save_folder, "lovecraft.txt"), "w", encoding="utf-8") as file:
file.write(texts)
# Bible w/o numbers
# Getting the bible
resp = requests.get("http://www.gutenberg.org/ebooks/10.txt.utf-8", stream=True)
text = resp.content.decode("utf-8")
start = text.find("The First Book of Moses")
end_str = "22:21 The grace of our Lord Jesus Christ be with you all. Amen."
end = text.find(end_str)
text = text[start:end + len(end_str)]
text = text.replace("\r\n", "\n")
with open(join(save_folder, "bible_cropped.txt"), "w", encoding="utf-8") as file:
file.write(text) # First charact is \ufeff
with open(join(save_folder, "bible_cropped.txt"), "r", encoding="utf-8") as file:
text = file.read()
text = re.sub("[0-9]+:[0-9]+", "", text)
text = re.sub("([^\n])(\n)([^\n])", lambda x: " ".join(x.group(1, 3)), text)
text = re.sub(" +", " ", text)
text = re.sub("\n ", "\n", text)
text = re.sub("\n\n+", "\n\n", text)
with open(join(save_folder, "bible_cropped_wo_numbers_nor_lb.txt"), "w", encoding="utf-8") as file:
file.write(text)
fusion_files = [join(save_folder, "bible_cropped_wo_numbers_nor_lb.txt"), join(save_folder, "book.txt")]
text = ""
dataset_name = "dataset_won_300space_bible_won_nor_lb.txt"
for file in fusion_files:
with open(file, "r", encoding="utf-8") as file:
text_ = file.read()
text_ = "".join([[" ", x][ord(x) < 300 or ord(x) == 65279] for x in text_])
text_ = re.sub(" +", " ", text_)
text_ = re.sub("\n ", "\n", text_)
text_ = re.sub("\n\n+", "\n\n", text_)
text += text_
text += "\n"
with open(join(save_folder, dataset_name), "w", encoding="utf-8") as file:
file.write(text)
# Quran
link_ = "https://m.clearquran.com/{}.html"
for i in range(1, 114 + 1):
page = str(i).rjust(3, "0")
link = link_.format(page)
resp = requests.get(link, stream=True)
soup = BeautifulSoup(resp.content, "lxml")
texts = soup.find_all("p")
texts = [text.text for text in texts[1:] if not "In the name of God, the Gracious, the Merciful." in text.text]
texts = [re.sub("\n[0-9]+\. ", "", text) for text in texts]
with open(join(quran_folder, "{}.txt".format(page)), "w", encoding="utf-8") as file:
file.write("\n\n".join(texts))
texts = []
for i in range(1, 114 + 1):
page = str(i).rjust(3, "0")
with open(join(quran_folder, "{}.txt".format(page)), "r", encoding="utf-8") as file:
texts.append(file.read())
texts = "\n".join(texts)
with open(join(save_folder, "quran.txt"), "w", encoding="utf-8") as file:
file.write(texts)
forbidden_words = ["nig", "obama"]
def any_element_in(elements, obj):
for element in elements:
if element.lower() in obj.lower():
return True
return False
def crop_sentence(x):
if "." in x:
x = ".".join(x.split(".")[:-1]) + "."
elif "\n" in x:
x = "\n".join(x.split("\n")[:-1]) + "\n"
return x