Class: Kiba::Extend::Transforms::Name::SplitInverted
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::Name::SplitInverted
- Defined in:
- lib/kiba/extend/transforms/name/split_inverted.rb
Overview
Note:
Makes many egregiously oversimplified Western assumptions. Do not trust it to split all names properly in all cases. Handles only the most common English language name patterns.
Splits an inverted name form into name parts.
Examples
In these examples, you can see the inverted name in the :iname
field of each row.
Used in pipeline as:
transform Name::SplitInverted, source: :iname
Results in:
{:iname=>"Smith, Robert", :firstname=>"Robert", :middlename=>nil, :lastname=>"Smith", :suffix=>nil}
{:iname=>"Smith, Robert J.", :firstname=>"Robert", :middlename=>"J.", :lastname=>"Smith", :suffix=>nil}
{:iname=>"Smith-Jones, Robert J.", :firstname=>"Robert", :middlename=>"J.", :lastname=>"Smith-Jones", :suffix=>nil}
{:iname=>"Smith, Robert James", :firstname=>"Robert", :middlename=>"James", :lastname=>"Smith", :suffix=>nil}
{:iname=>"Smith, R. James", :firstname=>"R.", :middlename=>"James", :lastname=>"Smith", :suffix=>nil}
{:iname=>"Smith, Robert (Bob)", :firstname=>"Robert", :middlename=>"(Bob)", :lastname=>"Smith", :suffix=>nil}
{:iname=>"Smith, Robert James (Bob)", :firstname=>"Robert", :middlename=>"James (Bob)", :lastname=>"Smith", :suffix=>nil}
{:iname=>"Smith, R. J.", :firstname=>"R.", :middlename=>"J.", :lastname=>"Smith", :suffix=>nil}
{:iname=>"Smith, R.J.", :firstname=>"R.", :middlename=>"J.", :lastname=>"Smith", :suffix=>nil}
{:iname=>"Smith, R J", :firstname=>"R", :middlename=>"J", :lastname=>"Smith", :suffix=>nil}
{:iname=>"Smith, RJ", :firstname=>"R", :middlename=>"J", :lastname=>"Smith", :suffix=>nil}
{:iname=>"Smith, RJR", :firstname=>"R", :middlename=>"JR", :lastname=>"Smith", :suffix=>nil}
{:iname=>"Smith, RJRR", :firstname=>"RJRR", :middlename=>nil, :lastname=>"Smith", :suffix=>nil}
{:iname=>"Smith, R.", :firstname=>"R.", :middlename=>nil, :lastname=>"Smith", :suffix=>nil}
{:iname=>"Smith", :firstname=>nil, :middlename=>nil, :lastname=>nil, :suffix=>nil}
{:iname=>"Smith, Robert, Jr.", :firstname=>"Robert", :middlename=>nil, :lastname=>"Smith", :suffix=>"Jr."}
{:iname=>"Smith, R.J., Sr.", :firstname=>"R.", :middlename=>"J.", :lastname=>"Smith", :suffix=>"Sr."}
{:iname=>"Smith, R. J., Sr.", :firstname=>"R.", :middlename=>"J.", :lastname=>"Smith", :suffix=>"Sr."}
{:iname=>"R.J. Smith & Co.", :firstname=>nil, :middlename=>nil, :lastname=>nil, :suffix=>nil}
{:iname=>"Smith, James, Robert & Co.", :firstname=>"James", :middlename=>nil, :lastname=>"Smith", :suffix=>"Robert & Co."}
{:iname=>"Robert \"Bob\" Smith", :firstname=>nil, :middlename=>nil, :lastname=>nil, :suffix=>nil}
{:iname=>"", :firstname=>nil, :middlename=>nil, :lastname=>nil, :suffix=>nil}
{:iname=>nil, :firstname=>nil, :middlename=>nil, :lastname=>nil, :suffix=>nil}
{:foo=>"bar", :firstname=>nil, :middlename=>nil, :lastname=>nil, :suffix=>nil}
Used in pipeline as:
transform Name::SplitInverted, source: :iname, targets: %i[f m l s]
Results in:
{:iname=>"Smith, R.J., Sr.", :f=>"R.", :l=>"Smith", :m=>"J.", :s=>"Sr."}
Used in pipeline as:
transform Name::SplitInverted, source: :iname, fallback: :lastname
Results in:
{:iname=>"Smith", :firstname=>nil, :lastname=>"Smith", :middlename=>nil, :suffix=>nil}
Instance Method Summary collapse
-
#initialize(source:, targets: %i[firstname middlename lastname suffix], fallback: :all_nil) ⇒ SplitInverted
constructor
A new instance of SplitInverted.
-
#process(row) ⇒ Object
Constructor Details
#initialize(source:, targets: %i[firstname middlename lastname suffix], fallback: :all_nil) ⇒ SplitInverted
Returns a new instance of SplitInverted.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/kiba/extend/transforms/name/split_inverted.rb', line 83 def initialize(source:, targets: %i[firstname middlename lastname suffix], fallback: :all_nil) @source = source @targets = targets @firstname = targets[0] @middlename = targets[1] @lastname = targets[2] @suffix = targets[3] @fallback = fallback unless fallback == :all_nil || targets.any?(fallback) raise ArgumentError, "fallback must equal :all_nil or one of the target field names" end end |
Instance Method Details
#process(row) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/kiba/extend/transforms/name/split_inverted.rb', line 100 def process(row) create_nil_fields(row) inverted = row.fetch(source, "") if splittable?(inverted) split(inverted, row) else do_not_split(inverted, row) end end |