Skip to content

Commit

Permalink
Split csv column
Browse files Browse the repository at this point in the history
  • Loading branch information
tikkss committed Dec 13, 2023
1 parent 641558f commit 24e251d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 29 deletions.
4 changes: 2 additions & 2 deletions example/house-of-representative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
house_of_representative.each do |record|
# Select support of one hundred or more members and promulgated
next unless 100 <= record.supporters_of_submitted_bill.size
next if record.promulgated_on_and_law_number == "/"
next if record.promulgated_on.nil?

p [
record.supporters_of_submitted_bill.size,
record.promulgated_on_and_law_number,
record.promulgated_on,
record.title,
]
end
50 changes: 41 additions & 9 deletions lib/datasets/house-of-representative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,36 @@ class HouseOfRepresentative < Dataset
:submitter,
:submitter_in_house_groups,
:house_of_representatives_of_accepted_bill_on_preliminary_consideration,
:house_of_representatives_of_preliminary_refer_on_and_commission,
:house_of_representatives_of_preliminary_refer_on,
:house_of_representatives_of_preliminary_refer_commission,
:house_of_representatives_of_accepted_bill_on,
:house_of_representatives_of_refer_on_and_commission,
:house_of_representatives_of_finished_consideration_on_and_result,
:house_of_representatives_of_finished_deliberation_on_and_result,
:house_of_representatives_of_refer_on,
:house_of_representatives_of_refer_commission,
:house_of_representatives_of_finished_consideration_on,
:house_of_representatives_of_consideration_result,
:house_of_representatives_of_finished_deliberation_on,
:house_of_representatives_of_deliberation_result,
:house_of_representatives_of_attitude_of_in_house_group_during_deliberation,
:house_of_representatives_of_support_in_house_group_during_deliberation,
:house_of_representatives_of_opposition_in_house_group_during_deliberation,
:house_of_councillors_of_accepted_bill_on_preliminary_consideration,
:house_of_councillors_of_preliminary_refer_on_and_commission,
:house_of_councillors_of_preliminary_refer_on,
:house_of_councillors_of_preliminary_refer_commission,
:house_of_councillors_of_accepted_bill_on,
:house_of_councillors_of_refer_on_and_commission,
:house_of_councillors_of_finished_consideration_on_and_result,
:house_of_councillors_of_finished_deliberation_on_and_result,
:promulgated_on_and_law_number,
:house_of_councillors_of_refer_on,
:house_of_councillors_of_refer_commission,
:house_of_councillors_of_finished_consideration_on,
:house_of_councillors_of_consideration_result,
:house_of_councillors_of_finished_deliberation_on,
:house_of_councillors_of_deliberation_result,
:promulgated_on,
:law_number,
:submitters,
:supporters_of_submitted_bill)

SPLIT_COLUMN_CHAR_ON_HEADER = "/".freeze
SPLIT_COLUMN_CHAR_ON_FIELD = SPLIT_COLUMN_CHAR_ON_HEADER

def initialize
super()

Expand All @@ -50,6 +62,7 @@ def each

open_data do |csv|
csv.each do |row|
row = split_csv_column(row)
%w(議案提出会派 衆議院審議時賛成会派 衆議院審議時反対会派 議案提出者一覧 議案提出の賛成者).each do |array_column_name|
row[array_column_name] = parse_array(row[array_column_name])
end
Expand All @@ -74,5 +87,24 @@ def open_data
def parse_array(column_value)
column_value.to_s.split("; ")
end

def split_csv_column(row)
new_headers = row.headers
new_fields = row.fields
new_headers.each_with_index do |header, idx|
next unless header.include?(SPLIT_COLUMN_CHAR_ON_HEADER)

header.split(SPLIT_COLUMN_CHAR_ON_HEADER).tap do |substrings|
new_headers[idx] = substrings[0]
new_headers.insert(idx + 1, substrings[1])
end

new_fields[idx].split(SPLIT_COLUMN_CHAR_ON_FIELD).tap do |substrings|
new_fields[idx] = substrings[0]
new_fields.insert(idx + 1, substrings[1])
end
end
CSV::Row.new(new_headers, new_fields)
end
end
end
54 changes: 36 additions & 18 deletions test/test-house-of-representative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,30 @@ def record(*args)
"熊代 昭彦君外四名",
%w(自由民主党 社会民主党・市民連合 新党さきがけ),
nil,
"/",
nil,
nil,
"平成10年 3月 4日",
"平成10年 3月11日/内閣",
"平成10年 3月17日/可決",
"平成10年 3月19日/可決",
"平成10年 3月11日",
"内閣",
"平成10年 3月17日",
"可決",
"平成10年 3月19日",
"可決",
nil,
[],
[],
nil,
"/",
nil,
"平成10年 1月12日/労働・社会政策",
"平成10年 3月 3日/修正",
"平成10年 3月 4日/修正",
"平成10年 3月25日/7",
nil,
nil,
"平成10年 1月12日",
"労働・社会政策",
"平成10年 3月 3日",
"修正",
"平成10年 3月 4日",
"修正",
"平成10年 3月25日",
"7",
[],
[]),
record(212,
Expand All @@ -58,21 +67,30 @@ def record(*args)
"中司 宏君外三名",
%w(日本維新の会),
nil,
"/",
nil,
"令和 5年10月20日/議院運営",
"/",
"/",
nil,
nil,
"令和 5年10月20日",
"議院運営",
nil,
nil,
nil,
nil,
nil,
[],
[],
nil,
"/",
nil,
"/",
"/",
"/",
"/",
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
%w(中司宏君 岩谷良平君 守島正君 吉田とも代君),
%w(足立康史君 阿部司君 阿部弘樹君 青柳仁士君 赤木正幸君 浅川義治君 井上英孝君 伊東信久君 池下卓君 池畑浩太朗君 一谷勇一郎君 市村浩一郎君 浦野靖人君 漆間譲司君 遠藤敬君 遠藤良太君 小野泰輔君 奥下剛光君 金村龍那君 沢田良君 杉本和巳君 住吉寛紀君 空本誠喜君 高橋英明君 馬場伸幸君 早坂敦君 林佑美君 藤田文武君 藤巻健太君 堀場幸子君 掘井健智君 三木圭恵君 美延映夫君 岬麻紀君 山本剛正君 和田有一朗君)),
],
Expand Down

0 comments on commit 24e251d

Please sign in to comment.