-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparse_verdict_csv.rb
executable file
·45 lines (38 loc) · 1.27 KB
/
parse_verdict_csv.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
#!/usr/bin/env ruby
require 'json'
require 'smarter_csv'
verdicts = SmarterCSV.process('./判決書.csv')
def write_json(filename, content)
File.open(filename,"w") do |f|
f.write(JSON.pretty_generate(content))
end
end
def write_file(filename, content)
File.open(filename,"w") do |f|
f.write(content)
end
end
$result_csv = "court_name,type,reason,count\n"
$result = {}
verdicts.each do |v|
# puts v
if ["TPD","SLD","PCD","ILD","KLD","TYD","SCD","MLD","TCD","CHD","NTD","ULD","CYD","TND","KSD","HLD","TTD","PTD","PHD","KMD","LCD"].include?(v[:"法院代號"]) && v[:"宣判年"] == 2016
unless $result.key? v[:"法院名稱"]
$result[v[:"法院名稱"]] = {"刑事"=>{}, "民事"=>{}, "行政"=>{}}
end
unless $result[v[:"法院名稱"]][v[:"案件類別"]].key? v[:"案由"]
$result[v[:"法院名稱"]][v[:"案件類別"]][v[:"案由"]] = 1
else
$result[v[:"法院名稱"]][v[:"案件類別"]][v[:"案由"]] += 1
end
end
end
$result.each_key do |court_name|
$result[court_name].each_key do |type|
$result[court_name][type].each_key do |reason|
$result_csv += "#{court_name},#{type},#{reason},#{$result[court_name][type][reason]}\n"
end
end
end
write_json('result.json', $result)
write_file('result.csv', $result_csv)