Skip to content

Instantly share code, notes, and snippets.

@gongo
Created October 26, 2009 07:35
Show Gist options
  • Save gongo/218483 to your computer and use it in GitHub Desktop.
Save gongo/218483 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
require 'open-uri'
require 'kconv'
#
#= SKK の人名辞書ファイルから架空の氏名を生成する (Ruby version)
#
# Authors:: 宮國 渡
# Copyright:: Copyright (c) 2009 Wataru MIYAGUNI <[email protected]>
# URL:: http://github.com/gongo
#
#== 参考
#
#* http://gist.github.com/202947
#
#== For example
#
# fake = FakeFullName.new
# name = fake.generate
# puts "#{name.kanji} (#{name.kana})"
# #=> 御園 春生 (みその はるお)
#
#/
class FakeFullName
DICTIONARY_URI = 'http://openlab.jp/skk/skk/dic/SKK-JISYO.jinmei'
def initialize
@last_names = Array.new
@first_names = Array.new
generate_name_list
end
def generate
full_name = Name.new
full_name.first_name = @first_names.choice
full_name.last_name = @last_names.choice
return full_name
end
private
def generate_name_list
dics = get_dictionary
dics.each_line do |d|
if /^;/ =~ d
next
end
name_info_list = d.toutf8.split('/')
kana = name_info_list.shift.strip
classify_name(name_info_list, kana)
end
end
def get_dictionary
return open(DICTIONARY_URI)
end
def classify_name(list, kana)
list.each do |info|
name, type = info.split(';')
case type
when '姓'
@last_names << { :kanji => name, :kana => kana }
when '名'
@first_names << { :kanji => name, :kana => kana }
end
end
end
end
#
#= 名前クラス
#
class Name
attr_accessor :first_name, :last_name
@first_name = nil
@last_name = nil
def kanji
return "#{@last_name[:kanji]} #{@first_name[:kanji]}"
end
def kana
return "#{@last_name[:kana]} #{@first_name[:kana]}"
end
def first_kanji
return @first_name[:kanji]
end
def first_kana
return @first_name[:kana]
end
def last_kanji
return @last_name[:kanji]
end
def last_kana
return @last_name[:kana]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment