Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

now supports case insensitivity, bumped version

  • Loading branch information...
commit e38ac1a4bc231f81c956b5e8f308956ea2935ef8 1 parent 530127f
@bmuller authored
View
4 Gemfile.lock
@@ -1,11 +1,13 @@
PATH
remote: .
specs:
- sexmachine (0.0.4)
+ sexmachine (0.0.5)
+ unicode_utils (>= 1.3.0)
GEM
remote: http://rubygems.org/
specs:
+ unicode_utils (1.3.0)
PLATFORMS
ruby
View
8 README.rdoc
@@ -24,7 +24,13 @@ Additionally, you can give preference to specific countries:
>> d.get_gender("Jamie", :great_britain)
=> :mostly_male
-If you have an alterative data file, you can pass that in as an optional argument to the Detector.
+If you have an alterative data file, you can pass that in as an optional filename argument to the Detector. Additionally, you can create a detector that is not case sensitive (default *is* to be case sensitive):
+
+ >> d = SexMachine::Detector.new(:case_sensitive => false)
+ >> d.get_gender "sally"
+ => :female
+ >> d.get_gender "Sally"
+ => :female
Try to avoid creating many Detectors, as each creation means reading in the data file.
View
25 lib/sexmachine/detector.rb
@@ -1,3 +1,5 @@
+require "unicode_utils/downcase"
+
module SexMachine
class Detector
@@ -7,9 +9,13 @@ class Detector
:greece, :russia, :belarus, :moldova, :ukraine, :armenia, :azerbaijan, :georgia, :the_stans, :turkey, :arabia, :israel, :china,
:india, :japan, :korea, :vietnam, :other_countries ]
- def initialize(fname=nil)
- fname ||= File.expand_path('../data/nam_dict.txt', __FILE__)
- parse fname
+ def initialize(opts = {})
+ opts = {
+ :filename => File.expand_path('../data/nam_dict.txt', __FILE__),
+ :case_sensitive => true
+ }.merge(opts)
+ @case_sensitive = opts[:case_sensitive]
+ parse opts[:filename]
end
def parse(fname)
@@ -22,6 +28,8 @@ def parse(fname)
end
def get_gender(name, country = nil)
+ name = UnicodeUtils.downcase(name) unless @case_sensitive
+
if not @names.has_key?(name)
:andy
elsif country.nil?
@@ -44,13 +52,14 @@ def eat_name_line(line)
parts = line.split(" ").select { |p| p.strip != "" }
country_values = line.slice(30, line.length)
+ name = @case_sensitive ? parts[1] : UnicodeUtils.downcase(parts[1])
case parts[0]
- when "M" then set(parts[1], :male, country_values)
- when "1M", "?M" then set(parts[1], :mostly_male, country_values)
- when "F" then set(parts[1], :female, country_values)
- when "1F", "?F" then set(parts[1], :mostly_female, country_values)
- when "?" then set(parts[1], :andy, country_values)
+ when "M" then set(name, :male, country_values)
+ when "1M", "?M" then set(name, :mostly_male, country_values)
+ when "F" then set(name, :female, country_values)
+ when "1F", "?F" then set(name, :mostly_female, country_values)
+ when "?" then set(name, :andy, country_values)
else raise "Not sure what to do with a sex of #{parts[0]}"
end
end
View
2  lib/sexmachine/version.rb
@@ -1,3 +1,3 @@
module SexMachine
- VERSION = "0.0.4"
+ VERSION = "0.0.5"
end
View
1  sexmachine.gemspec
@@ -15,5 +15,6 @@ Gem::Specification.new do |s|
s.homepage = "https://github.com/bmuller/sexmachine"
s.require_paths = ["lib"]
s.required_ruby_version = Gem::Requirement.new(">= 1.9.0")
+ s.add_dependency('unicode_utils', '>= 1.3.0')
s.rubyforge_project = "sexmachine"
end
Please sign in to comment.
Something went wrong with that request. Please try again.