Skip to content

Instantly share code, notes, and snippets.

@willnet
Created July 6, 2013 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willnet/5939294 to your computer and use it in GitHub Desktop.
Save willnet/5939294 to your computer and use it in GitHub Desktop.
tokyorubyistmeetup のペアプロ課題その2(wip)
# -*- coding: utf-8 -*-
require "csv"
module PostalCode
def self.find(postal_code)
match = CSV.read(File.join(File.dirname(__FILE__), "../data/ken_all.csv")).find do |row|
row[2] == postal_code.tr("-", "")
end
match && match[6,3]
end
def self.reverse_lookup(address)
prefecture, suffix, others = address.split(/(県)|(都)|(道)|(府)/)
array = CSV.read(File.join(File.dirname(__FILE__), '..', "#{prefecture + suffix}.csv")).find do |row|
address =~ /#{row[1]}/
end
"#{array[0][0..2]}-#{array[0][3..6]}"
end
def convert_kanji_to_number(kanji)
kanji.split('十').map do |number|
convert_one_to_nine(number)
end
# 二十三 -> 23 (十 can be ignored!)
# 二十 -> 20 (十 add 0)
# 十 -> 10
end
def convert_one_to_nine(kanji)
case kanji
when '一'; 1
when '二'; 2
when '三'; 3
when '四'; 4
when '五'; 5
when '六'; 6
when '七'; 7
when '八'; 8
when '九'; 9
when '十'; 10
when '十一'; 11
when '十二'; 12
when '十三'; 13
when '十四'; 14
when '十五'; 15
when '十六'; 16
when '十七'; 17
when '十八'; 18
when '十九'; 19
when '二十'; 20
end
end
end
#!/usr/bin/env ruby
require 'csv'
require 'pry'
hash = {}
CSV.read(File.join(File.dirname(__FILE__), "./data/ken_all.csv")).each do |row|
prefecture = row[6]
postal_code = row[2]
address = row[6..8]
if hash[prefecture].nil? || hash[prefecture].empty?
hash[prefecture] = [[postal_code, address]]
else
hash[prefecture] << [postal_code, address]
end
end
hash.each do |prefecture_name, prefecture_data|
CSV.open("#{prefecture_name}.csv", "wb") do |csv|
prefecture_data.each do |data|
postal_code = data[0]
address = data[1].join
csv << [postal_code, address]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment