forked from siuying/legco-hansard-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
71 lines (58 loc) · 1.7 KB
/
Rakefile
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
require 'rake'
$LOAD_PATH << "lib"
require 'legco'
require 'open-uri'
require 'fileutils'
namespace :download do
task :list do
lister = Legco::Hansard::Lister.new
data = lister.list
File.open("data/hansard_list.json", 'w') do |f|
f.write JSON.pretty_generate data
end
end
# download hansard as pdf
task :hansard do
output_path = "data/pdf"
data = JSON(open("data/hansard_list.json").read)
data.each do |meeting|
meeting["hansard"].each do |url|
match = url.match(%r{/yr([0-9\-]+?)/})
year = match ? match[1] : ""
filename = File.basename(url)
output_file = "#{output_path}/#{year}-#{filename}"
puts "Download PDF: #{url}"
unless File.exists?(output_file)
system "wget \"#{url}\" -O \"#{output_file}\""
end
end
end
end
end
namespace :convert do
task :txt do
output_path = "data/txt"
converter = Legco::Hansard::PdfConverter.new
Dir.glob("data/pdf/*.pdf").each do |pdf|
filename = File.basename(pdf).split(".").first + ".txt"
output_filename = "#{output_path}/#{filename}"
puts "Converting PDF: #{pdf}"
converter.convert(pdf, output_filename)
end
end
task :json do
output_path = "data/json"
FileUtils.mkdir_p(output_path)
Dir.glob("data/txt/*.txt").each do |text|
parser = Legco::Hansard::Parser.new
filename = File.basename(text).split(".").first + ".json"
output_filename = "#{output_path}/#{filename}"
puts "Converting JSON: #{text}"
doc = Legco::Hansard::Document.new(open(text).read)
parser.parse(doc)
File.open(output_filename, 'w') do |f|
f.write doc.to_json
end
end
end
end