forked from davisd/python-scriptures
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME
334 lines (216 loc) · 7.97 KB
/
README
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
=================
Python Scriptures
=================
python-scriptures is a Python 2 and Python 3 compatible package and regular
expression library for validating, extracting and normalizing biblical
scripture references from blocks of text.
For more information, see http://www.davisd.com/projects/python-scriptures/
Typical usage is as follows::
#!/usr/bin/env python
>>> import scriptures
>>> scriptures.extract('This is a test Rom 3:23-28 and 1 JOHn 2')
[('Romans', 3, 23, 3, 28), ('I John', 2, 1, 2, 29)]
Range validation is performed automatically and invalid references are not
extracted.
>>> import scriptures
>>> scriptures.extract('Romans 3:23 is real, but Romans 2:30 is invalid.')
[('Romans', 3, 23, 3, 23)]
Multi-Chapter references work:
>>> import scriptures
>>> scriptures.extract('You can specify a range of chapters like Rev 2-3')
[('Revelation of Jesus Christ', 2, 1, 3, 22)]
References with single chapter books do not require the chapter be specified.
>>> import scriptures
>>> scriptures.extract('You can specify a single verse such as Jude 4')
[('Jude', 1, 4, 1, 4)]
>>> scriptures.extract('Or specify multiple verses with jude 2-5...')
[('Jude', 1, 2, 1, 5)]
Installation
============
A setup script (setup.py) is provided. To install, simply run the script with
the install command:
$ python setup.py install
Or just put the scriptures package somewhere on the Python path.
API
===
Return Values
-------------
When a "scripture reference" is returned, it is always a five value tuple
consisting of:
('Book name', start chapter, start verse, end chapter, end verse)
Functions
---------
There are four public functions exposed by this package.
extract
~~~~~~~
Extract a list of tupled scripture references from a block of text.
Arguments:
text -- the block of text containing potential scripture references
Example:
>>> import scriptures
>>> scriptures.extract('This is a test Rom 3:23-28 and 1 JOHn 2')
[('Romans', 3, 23, 3, 28), ('I John', 2, 1, 2, 29)]
reference_to_string
~~~~~~~~~~~~~~~~~~~
Get a display friendly string from a scripture reference.
Arguments:
bookname -- the full or abbreviated book name
chapter -- the starting chapter
Optional Arguments:
verse -- the starting verse
end_chapter -- the ending chapter
end_verse -- the ending verse
Examples:
>>> import scriptures
>>> scriptures.reference_to_string('acts', 1)
'Acts 1'
>>> scriptures.reference_to_string('John', 3, 16)
'John 3:16'
>>> scriptures.reference_to_string('Rom', 3, 23, 3, 28)
'Romans 3:23-28'
>>> scriptures.reference_to_string('ecc', 1, 2, 2)
'Ecclesiastes 1:2-2:26'
>>> scriptures.reference_to_string('john', 1, 1, 2, 25)
'John 1-2'
Single Chapter Book Examples:
>>> import scriptures
>>> scriptures.reference_to_string('jude', 1, 4)
'Jude 4'
>>> scriptures.reference_to_string('2john', 1, 4, 1, 7)
'II John 4-7'
normalize_reference
~~~~~~~~~~~~~~~~~~~
Get a complete five value tuple scripture reference with full book name from
partial data.
Arguments:
bookname -- the full or abbreviated book name
chapter -- the starting chapter
Optional Arguments:
verse -- the starting verse
end_chapter -- the ending chapter
end_verse -- the ending verse
Examples:
>>> import scriptures
>>> scriptures.normalize_reference('acts', 1)
('Acts', 1, 1, 1, 26)
>>> scriptures.normalize_reference('John', 3, 16)
('John', 3, 16, 3, 16)
>>> scriptures.normalize_reference('Rom', 3, 23, 3, 28)
('Romans', 3, 23, 3, 28)
>>> scriptures.normalize_reference('ecc', 1, 2, 2)
('Ecclesiastes', 1, 2, 2, 26)
is_valid_reference
~~~~~~~~~~~~~~~~~~
Check to see if a scripture reference is valid.
Arguments:
bookname -- the full or abbreviated book name
chapter -- the starting chapter
Optional Arguments:
verse -- the starting verse
end_chapter -- the ending chapter
end_verse -- the ending verse
Examples:
>>> import scriptures
>>> scriptures.is_valid_reference('John', 3, 16)
True
>>> scriptures.is_valid_reference('ecc', 1, 2, 2)
True
>>> scriptures.is_valid_reference('Romans', 2, 30)
False
>>> scriptures.is_valid_reference('Romans', 2, 20, 2, 29)
True
Regular Expressions
-------------------
There are two compiled regular expression patterns exposed by this package.
book_re
~~~~~~~
Match a valid abbreviation or book name.
Examples:
>>> import scriptures
>>> import re
>>> re.findall(scriptures.book_re, 'Matt test Ecclesiastes and 2 peter')
['Matt', 'Ecclesiastes', '2 peter']
scripture_re
~~~~~~~~~~~~
Match a scripture reference pattern from a valid abbreviation or book name.
Examples:
>>> import scriptures
>>> import re
>>> re.findall(scriptures.scripture_re, 'Matt 3 & Acts 1:2-3 Rev 2:1-3:2')
[('Matt', '3', '', '', ''), ('Acts', '1', '2', '', '3'),
('Rev', '2', '1', '3', '2')]
Unicode
=======
This library is unicode compatible and recognizes the \u2013 en dash and \u2014
em dash.
Additional Texts
================
This library currently provides the following texts:
* protestant - scriptures.texts.protestant.ProtestantCanon
* deuterocanon - scriptures.texts.deutercanon.Deuterocanon
* kjv1611 - scriptures.texts.kjv1611.KingJames1611
Usage
-----
To use the additional texts, simply instantiate the text object and use the api
functions and regular expressions on the new instance.
Example
~~~~~~~
>>> from scriptures.texts.kjv1611 import KingJames1611
>>> myKJV1611 = KingJames1611()
>>> myKJV1611.extract('the protestant books are implemented- Matthew 1:1-5')
[(u'Matthew', 1, 1, 1, 5)]
>>> myKJV1611.extract('and Deuterocanon (apocrypha)- wisdom of solomon 1:1')
[(u'The Wisdom of Solomon', 1, 1, 1, 1)]
>>> myKJV1611.extract('and I Esd 1:1, II Esd 1:1, Prayer of Manasseh 1:1')
[(u'I Esdras', 1, 1, 1, 1), (u'II Esdras', 1, 1, 1, 1), (u'Prayer of Manasseh', 1, 1, 1, 1)]
Custom Texts
============
As of v3.0.0, the library makes makes extending the library through custom texts
trivial through additional modules. Please consider contributing your text
modules to this project by creating texts under scriptures/texts/ and submitting
a pull request.
Creating a New Text
-------------------
To create a new Text,
1) Create a class that inherits from scriptures.base.Text
2) Implement the "books" dictionary
3) Instantiate your new text and use it
The four api functions will be available from your instance:
* extract
* reference_to_string
* normalize_reference
* is_valid_reference
The two regular expressions are also available:
* book_re
* scripture_re
Example
~~~~~~~
>>> from scriptures.texts.base import Text
>>> class MyText(Text):
>>> books = {
>>> 'test1': ('Test Book 1', 'TBook1', 'test(?:\s)?1', [5, 4, 3, 4, 5]),
>>> 'test2': ('Test Book 2', 'TBook2', 'test(?:\s)?2', [5, 4, 3, 4, 5])
>>> }
>>>
>>> mytext = MyText()
>>> mytext.extract('Ok, testing- test1 1:3-5 and test2 2:4')
[('Test Book 1', 1, 3, 1, 5), ('Test Book 2', 2, 4, 2, 4)]
Creating a New Text Using Another As a Starting Point
-----------------------------------------------------
If you would like to use an existing set of books as a starting point, simply
update your books dictionary using the books from the Text class (or classes)
that you'd like to use as a starting point.
For an example of this, see the "KingJames1611" text class in
scriptures/text/kjv1611.py
The KingJames1611 Text class uses the ProtestantCanon and Deuterocanon books as
a starting point and adds I Esdras, II Esdras, and the Prayer of Manasseh.
Test Suite
==========
Unit tests are provided to verify chapter and verse style normalization, output
formatting, and book names and abbreviations.
To run the test suite, cwd to just outside of the scriptures package and:
$ python -m unittest discover
Author
======
David Davis <[email protected]>
http://www.davisd.com