scripturelookup 0.1.0__tar.gz → 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {scripturelookup-0.1.0/src/scripturelookup.egg-info → scripturelookup-0.1.2}/PKG-INFO +1 -1
- scripturelookup-0.1.2/src/scripturelookup/data.py +369 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/scripturelookup/lookup.py +358 -36
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/scripturelookup/patterns.py +30 -12
- {scripturelookup-0.1.0 → scripturelookup-0.1.2/src/scripturelookup.egg-info}/PKG-INFO +1 -1
- scripturelookup-0.1.2/src/scripturelookup.egg-info/scm_version.json +8 -0
- scripturelookup-0.1.2/tests/test_detect.py +441 -0
- scripturelookup-0.1.0/src/scripturelookup/data.py +0 -202
- scripturelookup-0.1.0/src/scripturelookup.egg-info/scm_version.json +0 -8
- scripturelookup-0.1.0/tests/test_detect.py +0 -137
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/.github/workflows/publish.yml +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/.gitignore +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/LICENSE +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/README.md +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/pyproject.toml +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/requirements.txt +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/setup.cfg +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/geezify-python-main/LICENSE +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/geezify-python-main/README.md +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/geezify-python-main/arabify.py +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/geezify-python-main/arabify_test.py +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/geezify-python-main/geezify.py +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/geezify-python-main/geezify_test.py +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/geezify-python-main/test_data.py +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/scripturelookup/__init__.py +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/scripturelookup/command_line.py +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/scripturelookup/data/metadata-languages.min.json +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/scripturelookup/data/metadata-scriptures.min.json +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/scripturelookup/numbers.py +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/scripturelookup.egg-info/SOURCES.txt +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/scripturelookup.egg-info/dependency_links.txt +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/scripturelookup.egg-info/entry_points.txt +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/scripturelookup.egg-info/requires.txt +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/scripturelookup.egg-info/scm_file_list.json +0 -0
- {scripturelookup-0.1.0 → scripturelookup-0.1.2}/src/scripturelookup.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
# Python standard libraries
|
|
2
|
+
import os
|
|
3
|
+
import sys
|
|
4
|
+
import json
|
|
5
|
+
import time
|
|
6
|
+
import re
|
|
7
|
+
import unicodedata
|
|
8
|
+
|
|
9
|
+
# Third-party libraries are imported where they're used, rather than here, so that commands that
|
|
10
|
+
# never reach the network don't pay for loading them. Together they add about 0.12 seconds to startup.
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
data_directory = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data')
|
|
14
|
+
os.makedirs(data_directory, exist_ok = True)
|
|
15
|
+
|
|
16
|
+
# Download JSON data
|
|
17
|
+
def download_data(filename, filepath):
|
|
18
|
+
import requests
|
|
19
|
+
|
|
20
|
+
request_url = f'https://cdn.jsdelivr.net/gh/samuelbradshaw/python-scripture-scraper@main/sample/{filename}'
|
|
21
|
+
r = requests.get(request_url)
|
|
22
|
+
r.encoding = 'utf-8'
|
|
23
|
+
if r and r.status_code == 200:
|
|
24
|
+
data = r.content
|
|
25
|
+
with open(filepath, 'wb') as f:
|
|
26
|
+
f.write(data)
|
|
27
|
+
else:
|
|
28
|
+
sys.exit('\nError: Couldn’t download JSON data:\n{request_url}\n')
|
|
29
|
+
|
|
30
|
+
# Load JSON data
|
|
31
|
+
def load_data(filename):
|
|
32
|
+
filepath = os.path.join(data_directory, filename)
|
|
33
|
+
if os.path.isfile(filepath):
|
|
34
|
+
with open(filepath, 'r', encoding='utf-8') as f:
|
|
35
|
+
return json.load(f)
|
|
36
|
+
else:
|
|
37
|
+
download_data(filename, filepath)
|
|
38
|
+
return load_data(filename)
|
|
39
|
+
|
|
40
|
+
# Update JSON data. Returns the names of the files that were downloaded.
|
|
41
|
+
def update_data():
|
|
42
|
+
filenames = ('metadata-languages.min.json', 'metadata-scriptures.min.json',)
|
|
43
|
+
for filename in filenames:
|
|
44
|
+
download_data(filename, os.path.join(data_directory, filename))
|
|
45
|
+
return filenames
|
|
46
|
+
|
|
47
|
+
# Normalize text by removing anything that's not a letter or number, and converting to lowercase. This allows for a fuzzy comparison between input text and a known list of values.
|
|
48
|
+
def normalize_for_compare(text):
|
|
49
|
+
decomposed_text = unicodedata.normalize('NFKD', text)
|
|
50
|
+
normalized_text = ''.join([c for c in decomposed_text if unicodedata.category(c)[0] in ['L', 'N']]).lower()
|
|
51
|
+
return normalized_text
|
|
52
|
+
|
|
53
|
+
# Words that can appear in a scripture reference, by language. Longer words should come before shorter words that they start with, so that (for example) "chapters" is matched before "chapter".
|
|
54
|
+
reference_words = {
|
|
55
|
+
'en': {
|
|
56
|
+
'list_conjunctions': ['and', '&'],
|
|
57
|
+
'range_conjunctions': ['through', 'thru', 'to'],
|
|
58
|
+
'verse_words': ['verses', 'verse', 'vv.', 'v.'],
|
|
59
|
+
'chapter_words': ['chapters', 'chapter', 'chs.', 'ch.'],
|
|
60
|
+
},
|
|
61
|
+
'es': {
|
|
62
|
+
'list_conjunctions': ['y', 'e'],
|
|
63
|
+
'range_conjunctions': ['a', 'al'],
|
|
64
|
+
'verse_words': ['versículos', 'versículo'],
|
|
65
|
+
'chapter_words': ['capítulos', 'capítulo'],
|
|
66
|
+
},
|
|
67
|
+
'fr': {
|
|
68
|
+
'list_conjunctions': ['et'],
|
|
69
|
+
'range_conjunctions': ['à'],
|
|
70
|
+
'verse_words': ['versets', 'verset'],
|
|
71
|
+
'chapter_words': ['chapitres', 'chapitre'],
|
|
72
|
+
},
|
|
73
|
+
'pt': {
|
|
74
|
+
'list_conjunctions': ['e'],
|
|
75
|
+
'range_conjunctions': ['a'],
|
|
76
|
+
'verse_words': ['versículos', 'versículo'],
|
|
77
|
+
'chapter_words': ['capítulos', 'capítulo'],
|
|
78
|
+
},
|
|
79
|
+
}
|
|
80
|
+
reference_word_keys = ('list_conjunctions', 'range_conjunctions', 'verse_words', 'chapter_words',)
|
|
81
|
+
|
|
82
|
+
# Ordinal forms 1–13, for citing an article of faith by position ("First Article of Faith"), and – in the
|
|
83
|
+
# first four entries – for a numbered book cited as "First Nephi" or "1st Nephi". A form's position in the
|
|
84
|
+
# list is the number it stands for, so there are no numbers written out separately to keep in sync.
|
|
85
|
+
# Gendered forms appear where the noun takes them: Portuguese cites "Regras de Fé", so an article there is
|
|
86
|
+
# "Primeira regra de fé", while Spanish cites "Artículos de Fe" and so takes "Primer artículo de fe".
|
|
87
|
+
ordinal_words = {
|
|
88
|
+
'en': [
|
|
89
|
+
('first', '1st',), ('second', '2nd',), ('third', '3rd',), ('fourth', '4th',),
|
|
90
|
+
('fifth', '5th',), ('sixth', '6th',), ('seventh', '7th',), ('eighth', '8th',),
|
|
91
|
+
('ninth', '9th',), ('tenth', '10th',), ('eleventh', '11th',), ('twelfth', '12th',),
|
|
92
|
+
('thirteenth', '13th',),
|
|
93
|
+
],
|
|
94
|
+
'fr': [
|
|
95
|
+
('premier', 'première', '1er', '1re', '1ère',),
|
|
96
|
+
('deuxième', 'second', 'seconde', '2e', '2ème', '2d', '2de',),
|
|
97
|
+
('troisième', '3e', '3ème',), ('quatrième', '4e', '4ème',), ('cinquième', '5e', '5ème',),
|
|
98
|
+
('sixième', '6e', '6ème',), ('septième', '7e', '7ème',), ('huitième', '8e', '8ème',),
|
|
99
|
+
('neuvième', '9e', '9ème',), ('dixième', '10e', '10ème',), ('onzième', '11e', '11ème',),
|
|
100
|
+
('douzième', '12e', '12ème',), ('treizième', '13e', '13ème',),
|
|
101
|
+
],
|
|
102
|
+
# Spanish has two accepted systems for 11 and 12 – the classical "undécimo" and the modern
|
|
103
|
+
# "decimoprimero" – and each ordinal has a masculine, a feminine, and (for 1, 3 and their compounds) an
|
|
104
|
+
# apocopated form used before a masculine noun. Every spelling is listed, so a reference resolves whichever
|
|
105
|
+
# the writer reached for, and whether or not the gender agrees with the name being cited.
|
|
106
|
+
'es': [
|
|
107
|
+
('primero', 'primer', 'primera', '1º', '1er', '1ª',),
|
|
108
|
+
('segundo', 'segunda', '2º', '2ª',),
|
|
109
|
+
('tercero', 'tercer', 'tercera', '3º', '3er', '3ª',),
|
|
110
|
+
('cuarto', 'cuarta', '4º', '4ª',),
|
|
111
|
+
('quinto', 'quinta', '5º', '5ª',),
|
|
112
|
+
('sexto', 'sexta', '6º', '6ª',),
|
|
113
|
+
('séptimo', 'séptima', '7º', '7ª',),
|
|
114
|
+
('octavo', 'octava', '8º', '8ª',),
|
|
115
|
+
('noveno', 'novena', '9º', '9ª',),
|
|
116
|
+
('décimo', 'décima', '10º', '10ª',),
|
|
117
|
+
('undécimo', 'undécima', 'decimoprimero', 'decimoprimera', 'decimoprimer', 'décimo primero', 'décima primera', 'décimo primer', '11º', '11ª',),
|
|
118
|
+
('duodécimo', 'duodécima', 'decimosegundo', 'decimosegunda', 'décimo segundo', 'décima segunda', '12º', '12ª',),
|
|
119
|
+
('decimotercero', 'decimotercera', 'decimotercer', 'décimo tercero', 'décima tercera', 'décimo tercer', '13º', '13ª',),
|
|
120
|
+
],
|
|
121
|
+
# Portuguese cites "Regras de Fé", which is feminine, so the feminine forms are the ones that occur – but
|
|
122
|
+
# the masculine are listed too, along with the classical "undécimo" and "duodécimo" alongside the ordinary
|
|
123
|
+
# two-word spellings.
|
|
124
|
+
'pt': [
|
|
125
|
+
('primeiro', 'primeira', '1º', '1ª',), ('segundo', 'segunda', '2º', '2ª',),
|
|
126
|
+
('terceiro', 'terceira', '3º', '3ª',), ('quarto', 'quarta', '4º', '4ª',),
|
|
127
|
+
('quinto', 'quinta', '5º', '5ª',), ('sexto', 'sexta', '6º', '6ª',),
|
|
128
|
+
('sétimo', 'sétima', '7º', '7ª',), ('oitavo', 'oitava', '8º', '8ª',),
|
|
129
|
+
('nono', 'nona', '9º', '9ª',), ('décimo', 'décima', '10º', '10ª',),
|
|
130
|
+
('décimo primeiro', 'décima primeira', 'undécimo', 'undécima', '11º', '11ª',),
|
|
131
|
+
('décimo segundo', 'décima segunda', 'duodécimo', 'duodécima', '12º', '12ª',),
|
|
132
|
+
('décimo terceiro', 'décima terceira', '13º', '13ª',),
|
|
133
|
+
],
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
# Roman numerals are typography rather than language, so they stand in for a book number in any language.
|
|
137
|
+
# Only 1–4 are needed, since no book is numbered higher.
|
|
138
|
+
roman_numeral_words = ['i', 'ii', 'iii', 'iv']
|
|
139
|
+
|
|
140
|
+
# The highest number any book name starts with ("4 Nephi"). patterns.leading_book_number is built from this.
|
|
141
|
+
highest_book_number = len(roman_numeral_words)
|
|
142
|
+
|
|
143
|
+
# Get a map of ordinal form to the number it stands for, for an article of faith cited by position
|
|
144
|
+
ordinal_forms_cache = {}
|
|
145
|
+
def get_ordinal_forms(lang):
|
|
146
|
+
if lang not in ordinal_forms_cache:
|
|
147
|
+
forms = {}
|
|
148
|
+
for index, group in enumerate(ordinal_words.get(lang, [])):
|
|
149
|
+
for form in group:
|
|
150
|
+
forms[form.lower()] = index + 1
|
|
151
|
+
# The ordinal indicators are hard to type on many keyboards, so "1º" is also accepted as "1o" and
|
|
152
|
+
# "1ª" as "1a". They're derived rather than listed, so a language only spells out what's linguistic.
|
|
153
|
+
plain_form = form.lower().replace('º', 'o').replace('ª', 'a')
|
|
154
|
+
forms.setdefault(plain_form, index + 1)
|
|
155
|
+
ordinal_forms_cache[lang] = forms
|
|
156
|
+
return ordinal_forms_cache[lang]
|
|
157
|
+
|
|
158
|
+
# Get a map of number form to the number it stands for, for the prefix on a numbered book name. It's the
|
|
159
|
+
# ordinals above, stopping at the highest number any book carries, plus the roman numerals that work in
|
|
160
|
+
# every language.
|
|
161
|
+
book_number_forms_cache = {}
|
|
162
|
+
def get_book_number_forms(lang):
|
|
163
|
+
if lang not in book_number_forms_cache:
|
|
164
|
+
forms = {form: number for form, number in get_ordinal_forms(lang).items() if number <= highest_book_number}
|
|
165
|
+
for index, form in enumerate(roman_numeral_words):
|
|
166
|
+
forms.setdefault(form, index + 1)
|
|
167
|
+
book_number_forms_cache[lang] = forms
|
|
168
|
+
return book_number_forms_cache[lang]
|
|
169
|
+
|
|
170
|
+
# Get the maps above keyed for comparison, so the number a matched form stands for can be looked up however
|
|
171
|
+
# the form was spelled. The pattern built from these forms matches text the map has no key for: a character
|
|
172
|
+
# class matches "Decimo" where the map holds "décimo", and whitespace is matched loosely, so "décimo
|
|
173
|
+
# primero" can arrive with a non-breaking space between the words.
|
|
174
|
+
normalized_ordinal_forms_cache = {}
|
|
175
|
+
def get_normalized_ordinal_forms(lang):
|
|
176
|
+
if lang not in normalized_ordinal_forms_cache:
|
|
177
|
+
normalized_ordinal_forms_cache[lang] = {normalize_for_compare(form): number for form, number in get_ordinal_forms(lang).items()}
|
|
178
|
+
return normalized_ordinal_forms_cache[lang]
|
|
179
|
+
|
|
180
|
+
normalized_book_number_forms_cache = {}
|
|
181
|
+
def get_normalized_book_number_forms(lang):
|
|
182
|
+
if lang not in normalized_book_number_forms_cache:
|
|
183
|
+
normalized_book_number_forms_cache[lang] = {normalize_for_compare(form): number for form, number in get_book_number_forms(lang).items()}
|
|
184
|
+
return normalized_book_number_forms_cache[lang]
|
|
185
|
+
|
|
186
|
+
# List conjunctions from every language, split into the ones written as a word ("and", "y", "et") and the ones written as a symbol ("&")
|
|
187
|
+
all_list_conjunctions = sorted({c for words in reference_words.values() for c in words['list_conjunctions']})
|
|
188
|
+
list_conjunction_words = [c for c in all_list_conjunctions if any(char.isalpha() for char in c)]
|
|
189
|
+
list_conjunction_symbols = [c for c in all_list_conjunctions if not any(char.isalpha() for char in c)]
|
|
190
|
+
|
|
191
|
+
# Get alternate spellings of a name where a spelled-out list conjunction is swapped for a symbol, or the other way around. Example: "Doctrine and Covenants" –> "Doctrine & Covenants"
|
|
192
|
+
def get_conjunction_aliases(name):
|
|
193
|
+
aliases = set()
|
|
194
|
+
for word in list_conjunction_words:
|
|
195
|
+
for symbol in list_conjunction_symbols:
|
|
196
|
+
if f' {word} ' in name:
|
|
197
|
+
aliases.add(name.replace(f' {word} ', f' {symbol} '))
|
|
198
|
+
if f' {symbol} ' in name:
|
|
199
|
+
aliases.add(name.replace(f' {symbol} ', f' {word} '))
|
|
200
|
+
return aliases
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
languages = load_data('metadata-languages.min.json')
|
|
204
|
+
scriptures = load_data('metadata-scriptures.min.json')
|
|
205
|
+
|
|
206
|
+
# Accept a symbol conjunction ("&") wherever a name spells the conjunction out, and vice versa. The words come from the language data below rather than being hard-coded, so this covers "Doctrine & Covenants" for "Doctrine and Covenants" and "Doctrina & Convenios" for "Doctrina y Convenios".
|
|
207
|
+
for key, value in list(scriptures['mapToSlug'].items()):
|
|
208
|
+
for alias in get_conjunction_aliases(key):
|
|
209
|
+
scriptures['mapToSlug'].setdefault(alias, value)
|
|
210
|
+
|
|
211
|
+
# Get a map of normalized names to book slugs, for comparing input that doesn't match a known name exactly. It's built the first time it's needed, rather than at import, since normalizing all ~10,000 names takes a moment and input that's already well-formed never needs it.
|
|
212
|
+
map_to_slug_normalized = None
|
|
213
|
+
def get_map_to_slug_normalized():
|
|
214
|
+
global map_to_slug_normalized
|
|
215
|
+
if map_to_slug_normalized is None:
|
|
216
|
+
map_to_slug_normalized = {normalize_for_compare(key): value for key, value in scriptures['mapToSlug'].items()}
|
|
217
|
+
return map_to_slug_normalized
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
# The shortest normalized name that's worth matching loosely. Below this, a single typo is most of the word – "Alma" is one edit away from far too much ordinary text.
|
|
221
|
+
minimum_fuzzy_name_length = 5
|
|
222
|
+
|
|
223
|
+
# Every spelling of a name with one letter taken out. Example: "alma" –> ["lma", "ama", "ala", "alm"]
|
|
224
|
+
# Digits are never dropped, so a number always has to match exactly – it's part of which book is meant, not
|
|
225
|
+
# something that can be mistyped into another book. Without that, "5th Nephi" would resolve to "4th Nephi",
|
|
226
|
+
# which is a real name one substitution away.
|
|
227
|
+
def get_single_character_deletions(key):
|
|
228
|
+
return [key[:i] + key[i + 1:] for i in range(len(key)) if not key[i].isdigit()]
|
|
229
|
+
|
|
230
|
+
# Get a map of every single-character deletion of every known name to its book slug. Comparing deletions on
|
|
231
|
+
# both sides is what makes a one-character difference cheap to find: a missing letter, an extra letter, or a
|
|
232
|
+
# wrong letter all line up on some shared deletion, so a lookup costs one dict get per character instead of
|
|
233
|
+
# a comparison against all ~10,000 names. Names that two different slugs both claim are dropped, since an
|
|
234
|
+
# ambiguous match is worse than none. Built the first time a name fails to match exactly, since input that's
|
|
235
|
+
# spelled correctly never needs it.
|
|
236
|
+
map_to_slug_deletions = None
|
|
237
|
+
def get_map_to_slug_deletions():
|
|
238
|
+
global map_to_slug_deletions
|
|
239
|
+
if map_to_slug_deletions is None:
|
|
240
|
+
map_to_slug_deletions = {}
|
|
241
|
+
for key, value in scriptures['mapToSlug'].items():
|
|
242
|
+
normalized_key = normalize_for_compare(key)
|
|
243
|
+
if len(normalized_key) < minimum_fuzzy_name_length:
|
|
244
|
+
continue
|
|
245
|
+
for deletion in get_single_character_deletions(normalized_key):
|
|
246
|
+
map_to_slug_deletions[deletion] = value if map_to_slug_deletions.get(deletion, value) == value else None
|
|
247
|
+
return map_to_slug_deletions
|
|
248
|
+
|
|
249
|
+
# Look up a book slug for a normalized name that's off by one character. Diacritics are already handled by
|
|
250
|
+
# normalize_for_compare, so only letter-level slips reach this point. Like mapToSlug itself, the index covers
|
|
251
|
+
# every language's names, so a typo resolves no matter which language was asked for.
|
|
252
|
+
def fuzzy_map_to_slug(normalized_name):
|
|
253
|
+
if len(normalized_name) < minimum_fuzzy_name_length:
|
|
254
|
+
return None
|
|
255
|
+
deletions = get_map_to_slug_deletions()
|
|
256
|
+
|
|
257
|
+
# The known name has one character that the input is missing
|
|
258
|
+
slug = deletions.get(normalized_name)
|
|
259
|
+
if slug:
|
|
260
|
+
return slug
|
|
261
|
+
|
|
262
|
+
# The input has one character too many, or one character wrong
|
|
263
|
+
for deletion in get_single_character_deletions(normalized_name):
|
|
264
|
+
slug = deletions.get(deletion)
|
|
265
|
+
if slug:
|
|
266
|
+
return slug
|
|
267
|
+
|
|
268
|
+
return None
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
# Look up the book slug for a name as it was written. The name is tried as-is, then normalized (which folds
|
|
272
|
+
# case, diacritics and punctuation), and finally – unless allow_loose_match is False – as a name that's off
|
|
273
|
+
# by one character. Every caller that resolves a name should come through here, so the three stages stay in
|
|
274
|
+
# one order.
|
|
275
|
+
def get_book_slug(book_string, allow_loose_match = True):
|
|
276
|
+
slug = scriptures['mapToSlug'].get(book_string)
|
|
277
|
+
if slug:
|
|
278
|
+
return slug
|
|
279
|
+
normalized_name = normalize_for_compare(book_string)
|
|
280
|
+
return get_map_to_slug_normalized().get(normalized_name) or (fuzzy_map_to_slug(normalized_name) if allow_loose_match else None)
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
# Get regex patterns for the words that can appear in a scripture reference in a given language. Languages that aren't listed above return empty patterns.
|
|
284
|
+
reference_words_cache = {}
|
|
285
|
+
def get_reference_words(lang):
|
|
286
|
+
if lang not in reference_words_cache:
|
|
287
|
+
words_for_lang = reference_words.get(lang, {})
|
|
288
|
+
reference_words_cache[lang] = {
|
|
289
|
+
key: r'|'.join([re.escape(w) for w in words_for_lang.get(key, [])])
|
|
290
|
+
for key in reference_word_keys
|
|
291
|
+
}
|
|
292
|
+
return reference_words_cache[lang]
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
# Get the BCP 47 language tag for a given language code
|
|
296
|
+
def get_bcp47(lang):
|
|
297
|
+
if lang and 'Hant' in lang:
|
|
298
|
+
lang = 'cmn-Hant'
|
|
299
|
+
elif lang and 'Hans' in lang:
|
|
300
|
+
lang = 'cmn-Hans'
|
|
301
|
+
|
|
302
|
+
bcp47 = languages.get('mapToBcp47', {}).get(lang)
|
|
303
|
+
if not bcp47:
|
|
304
|
+
bcp47 = 'en'
|
|
305
|
+
if lang:
|
|
306
|
+
sys.stdout.write(f'Warning: Couldn’t find BCP 47 language tag for “{lang}” – falling back to “en” (English).\n')
|
|
307
|
+
|
|
308
|
+
return bcp47
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
# Get the content for a given chapter verse from python-scripture-scraper or ChurchofJesusChrist.org
|
|
312
|
+
def request_content(publication_slug, book_slug, chapter, verse_groups, church_url, lang = 'en', source = 'python-scripture-scraper'):
|
|
313
|
+
import requests
|
|
314
|
+
from bs4 import BeautifulSoup
|
|
315
|
+
|
|
316
|
+
text_content = ''
|
|
317
|
+
|
|
318
|
+
if not publication_slug and book_slug and chapter:
|
|
319
|
+
return text_content
|
|
320
|
+
|
|
321
|
+
verse_numbers = []
|
|
322
|
+
if verse_groups:
|
|
323
|
+
for verse_group in verse_groups:
|
|
324
|
+
for verse_number in verse_group:
|
|
325
|
+
verse_numbers.append(str(verse_number))
|
|
326
|
+
|
|
327
|
+
if source == 'python-scripture-scraper':
|
|
328
|
+
request_url = f'https://cdn.jsdelivr.net/gh/samuelbradshaw/python-scripture-scraper@main/sample/en-json/{publication_slug}/{book_slug}/{book_slug}-{chapter}.json'
|
|
329
|
+
r = requests.get(request_url)
|
|
330
|
+
r.encoding = 'utf-8'
|
|
331
|
+
if r and r.status_code == 200:
|
|
332
|
+
chapter_data = r.json()
|
|
333
|
+
|
|
334
|
+
if verse_numbers:
|
|
335
|
+
for paragraph in chapter_data['paragraphs']:
|
|
336
|
+
if paragraph['type'] == 'verse' and paragraph['number'] in verse_numbers:
|
|
337
|
+
text_content += paragraph['number'] + ' ' + paragraph['content'] + '\n\n'
|
|
338
|
+
else:
|
|
339
|
+
for paragraph in chapter_data['paragraphs']:
|
|
340
|
+
text_content += (paragraph['number'] + ' ' if paragraph['number'] else '') + paragraph['content'] + '\n\n'
|
|
341
|
+
|
|
342
|
+
text_content += '---------------------\n'
|
|
343
|
+
text_content += 'Source: https://github.com/samuelbradshaw/python-scripture-scraper/tree/main/sample\n'
|
|
344
|
+
text_content += 'Public domain.\n'
|
|
345
|
+
|
|
346
|
+
elif source == 'ChurchofJesusChrist.org' and church_url:
|
|
347
|
+
r = requests.get(church_url)
|
|
348
|
+
r.encoding = 'utf-8'
|
|
349
|
+
if r and r.status_code == 200:
|
|
350
|
+
soup = BeautifulSoup(r.text, 'html.parser')
|
|
351
|
+
paragraphs = soup.select('header [data-aid], .body-block [data-aid]')
|
|
352
|
+
if verse_numbers:
|
|
353
|
+
for paragraph in paragraphs:
|
|
354
|
+
verse_number_span = paragraph.select_one('.verse-number')
|
|
355
|
+
if verse_number_span and verse_number_span.text.strip() in verse_numbers:
|
|
356
|
+
text_content += paragraph.text.strip() + '\n\n'
|
|
357
|
+
else:
|
|
358
|
+
for paragraph in paragraphs:
|
|
359
|
+
text_content += paragraph.text.strip() + '\n\n'
|
|
360
|
+
|
|
361
|
+
text_content += '---------------------\n'
|
|
362
|
+
text_content += f'Source: {church_url}\n'
|
|
363
|
+
text_content += 'Some content from this source may be subject to copyright.\n'
|
|
364
|
+
|
|
365
|
+
# Pause for 1 second between requests to avoid overloading server
|
|
366
|
+
time.sleep(1)
|
|
367
|
+
|
|
368
|
+
return text_content
|
|
369
|
+
|