Class: NameToGender::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/name_to_gender/parser.rb

Constant Summary collapse

DATA_FILE =
File.expand_path("../data/nam_dict.txt", __FILE__).freeze
USA_OFFSET =
32

Instance Method Summary collapse

Constructor Details

#initialize(data_file = DATA_FILE) ⇒ Parser

Returns a new instance of Parser.



6
7
8
# File 'lib/name_to_gender/parser.rb', line 6

def initialize(data_file = DATA_FILE)
  @data_file = data_file
end

Instance Method Details

#parseObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/name_to_gender/parser.rb', line 10

def parse
  lookup = Lookup.new

  File.open(@data_file, "r", :encoding => "iso8859-1:UTF-8") do |file|
    text = file.read
    text.gsub!(/\r\n?/, "\n")
    text.each_line do |line|
      next if line.start_with?("#") || line.start_with?("=")
      next if line[USA_OFFSET] == " "
      gender, name, * = line.split
      lookup.add_name(gender, name)
    end
  end

  lookup
end