/
structure_bible.rb
171 lines (144 loc) · 5.42 KB
/
structure_bible.rb
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
########
# Takes a single txt of the bible (from gutenberg.org) and parse it into a sturcture json file
# Extracts:
# - Title
# - Introduction
# - Chapters
# - Verse
# - Verse footnotes
########
require 'json'
require 'fileutils'
require 'optparse'
require_relative 'book_contents.rb'
def parse_book(target_file, out_path, out_extension, structure)
output_file = "#{out_path}#{structure[:name]}.#{out_extension}"
offset_start = structure[:start]
offset_end = structure[:end]
book = {title: [], intro: [], contents: []}
state = :title # :title, :intro, :chapter, :chapter_intro, :verse, :verse_footnote, :none
cur_chapter = nil
cur_verse = nil
skip_next_whitespace = false
i = 0
File.readlines(target_file, chomp: true).each do |line|
i += 1
next if i < offset_start
break if i >= offset_end
puts "#{i}:#{offset_start}:#{offset_end} line: #{line}"
# if i > 100
# puts "book: #{book.to_json}"
# break
# end
puts "line: #{line}"
if line.empty?
# Empty line
if skip_next_whitespace
skip_next_whitespace = false
next
end
if state == :title
next if book[:title].empty?
state = :intro
elsif state == :intro
next if book[:intro].empty?
state = :chapter
elsif state == :chapter
next if cur_chapter.nil? or book[:contents][cur_chapter][:title].empty?
state = :chapter_intro
# elsif state == :chapter_intro
# next if book[:contents][cur_chapter][:intro].empty?
# state = :none
# elsif state == :verse
# next if book[:contents][cur_chapter][:contents].empty? or cur_verse.nil? or book[:contents][cur_chapter][:contents][cur_verse].empty?
# state = :none
# elsif state == :verse_footnote
# next if book[:contents][cur_chapter][:footnotes].empty? or cur_verse.nil?
# state = :none
end
next
end
# Kings 4 has no :intro and thus we need to be able to skip it.
if state == :intro && line.match(/^(.*) Chapter ([0-9]+)$/)
state = :chapter
cur_verse = nil
end
puts "State: #{state}"
if state == :title
book[:title] << line
elsif state == :intro
book[:intro] << line
else
puts "State: #{state}"
# Hack: ECCLESIASTICUS has a "PROLOGUE" following the intro. Adding to the :intro
if line == 'THE PROLOGUE'
book[:intro] << line
state = :intro
skip_next_whitespace = true
end
# Might need to handle case where Chapter extends multiple lines
if line.match(/(.*) Chapter ([0-9]+)/)
puts "chap: #{line}"
book[:contents] << {title: [line], intro: [], contents: {}, footnotes: {}}
if cur_chapter.nil?
cur_chapter = 0
else
cur_chapter += 1
end
state = :chapter
cur_verse = nil
elsif (s = line.scan(/^([0-9]+)[:]([0-9]+)[\.\:]?(.+)$/); !s.empty?)
puts "HERE #{s}"
puts "cur_chapter: #{cur_chapter}"
next_verse = s[0][1].strip.to_i
if !cur_verse.nil? && cur_verse + 1 != next_verse
puts "WARNING VERSE SEQUENCE ISSUE! #{cur_verse} - #{next_verse}"
end
book[:contents][cur_chapter][:contents][s[0][1].strip.to_i] = [s[0][2].strip]
cur_verse = next_verse
state = :verse
elsif (s = line.scan(/^(.+?)\.\.\./); !s.empty?)
if book[:contents][cur_chapter][:footnotes][cur_verse].nil?
book[:contents][cur_chapter][:footnotes][cur_verse] = []
end
book[:contents][cur_chapter][:footnotes][cur_verse] << line
state = :verse_footnote
else
if state == :chapter
book[:contents][cur_chapter][:title] << line
elsif state == :chapter_intro
book[:contents][cur_chapter][:intro] << line
elsif state == :verse
book[:contents][cur_chapter][:contents][cur_verse] << line
elsif state == :verse_footnote
book[:contents][cur_chapter][:footnotes][cur_verse] << line
end
end
end
end
File.open(output_file, 'w') { |file| file.write(book.to_json) }
end
# ([0-9]+)[:]([0-9]+)\.(.+)$
# - Chapter : Section
# - eventually followed by \n ending the section
#
# ^([\w ]*\w)\.\.\.
# - Footnotes
#
# (.*) Chapter ([0-9]+)
# - Book Chapter #
# - followed by ^\n
# - followed by ^(.+)
# - eventually followed by \n
input_file = 'douay_rheims.txt'
output_path = 'data/douay_rheims_'
OptionParser.new do |opt|
opt.on('--infile [=INPUT_FILE]') { |o| input_file = o }
opt.on('--outpath [=OUTPUT_PATH]') { |o| output_path = o }
end.parse!
FileUtils.mkdir_p File.dirname(output_path)
#NEW_TESTAMENT_STRUCTURE
OLD_TESTAMENT_STRUCTURE.each do |structure|
puts "structure[:name] #{structure[:name]}"
parse_book(input_file, output_path, "json", structure)
end