-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.py
More file actions
72 lines (58 loc) · 2.13 KB
/
Copy pathfetch.py
File metadata and controls
72 lines (58 loc) · 2.13 KB
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
import json
import os
import tempfile
from bs4 import BeautifulSoup
import git
from nltk.tokenize import wordpunct_tokenize
from pylatexenc.latex2text import LatexNodes2Text
import requests
import src.constants as constants
def fname_sort_key(fname: str) -> int:
"""Sorting comparison function for DFP filenames"""
if "-" not in fname:
assert fname == "DaoFP.tex" or not fname.endswith(".tex"), f"{fname = }"
return 0
return int(fname.split("-")[0])
def fetch_dfp() -> None:
"""Fetch The Dao of Functional Programming, tokenize, and save into JSON.
First two lines of code from this [StackOverflow question/answer](https://stackoverflow.com/questions/51239168/how-to-download-single-file-from-a-git-repository-using-python).
"""
print("Fetching DFP...")
# Create temporary dir
t = tempfile.mkdtemp()
# Clone into temporary dir
git.Repo.clone_from(constants.URL_DFP, t, branch="master", depth=1)
# Parse LaTeX, tokenize
l = LatexNodes2Text()
tokseqs = []
for fname in sorted(os.listdir(t), key=fname_sort_key):
if fname.endswith(".tex"):
with open(f"{t}/{fname}", "r", encoding="utf-8") as f:
file_text = l.latex_to_text(f.read())
tokseqs.extend(wordpunct_tokenize(file_text))
# Save
with open(constants.PATH_DFP, "w", encoding="utf-8") as f:
json.dump(tokseqs, f)
def fetch_kjb() -> None:
"""Fetch King James Bible, tokenize, and save into JSON."""
print("Fetching KJB...")
# Fetch, get text
url = constants.URL_KJB
response = requests.get(url)
soup = BeautifulSoup(response.text, features=[])
text = soup.get_text()
# Select proper text
start_text = "The Old Testament"
end_text = "*** END OF THE PROJECT GUTENBERG EBOOK"
text = text[text.find(start_text) : text.find(end_text)]
# Tokenize
tokseqs = wordpunct_tokenize(text)
# Save
with open(constants.PATH_KJB, "w", encoding="utf-8") as f:
json.dump(tokseqs, f)
def fetch_all() -> None:
"""Fetch both, save into JSONs."""
fetch_dfp()
fetch_kjb()
if __name__ == "__main__":
fetch_all()