Class: Paxx::NameNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/paxx/names/name_normalizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src_name) ⇒ NameNormalizer

Returns a new instance of NameNormalizer.



6
7
8
9
# File 'lib/paxx/names/name_normalizer.rb', line 6

def initialize src_name
  @name = src_name
  @first_name, @last_name = splitt
end

Instance Attribute Details

#first_nameObject (readonly)

Returns the value of attribute first_name.



4
5
6
# File 'lib/paxx/names/name_normalizer.rb', line 4

def first_name
  @first_name
end

#full_nameObject (readonly)

Returns the value of attribute full_name.



4
5
6
# File 'lib/paxx/names/name_normalizer.rb', line 4

def full_name
  @full_name
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



4
5
6
# File 'lib/paxx/names/name_normalizer.rb', line 4

def last_name
  @last_name
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/paxx/names/name_normalizer.rb', line 4

def name
  @name
end

Instance Method Details

#as_id(txt = name) ⇒ Object



47
48
49
# File 'lib/paxx/names/name_normalizer.rb', line 47

def as_id(txt=name)
  subst_norwegian_chars(txt.delete(" ").delete("-")).downcase if txt
end

#as_short_refObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/paxx/names/name_normalizer.rb', line 63

def as_short_ref
  counter = 0
  unique = !block_given?
  xref =  ""
  start = camelize(as_id(first_name))

  begin
    shortref = compose_short_ref start,xref

    if !unique
      unique = yield shortref
      if !unique
        counter += 1
        xref =  last_part(camelize(as_id(last_name)),counter)
      end
    end
  end while !unique  && counter < 100
  shortref
end

#camelize(txt = name) ⇒ Object



30
31
32
# File 'lib/paxx/names/name_normalizer.rb', line 30

def camelize(txt=name)
  txt.split(/[^a-zøæåØÆÅ0-9]/i).map { |w| w.capitalize }.join if txt
end

#compose_short_ref(first, last) ⇒ Object



51
52
53
# File 'lib/paxx/names/name_normalizer.rb', line 51

def compose_short_ref first,last
  "#{first}#{last}"
end

#last_part(name, counter) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/paxx/names/name_normalizer.rb', line 55

def last_part name,counter
  if name && counter < name.length
    name[0..counter]
  else
    "#{name}#{counter-name.length+1}"
  end
end

#normalize(txt = name) ⇒ Object



15
16
17
# File 'lib/paxx/names/name_normalizer.rb', line 15

def normalize(txt=name)
  txt.to_s.gsub("\n", ' ').squeeze(' ').strip
end

#splittObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/paxx/names/name_normalizer.rb', line 34

def splitt
  words = name.to_s.split()
  if words.count > 1
    last_name = words.last
    words.pop
    first_name = words.join(" ")
  else
    first_name = name
    last_name=""
  end
  [camelize(first_name), camelize(last_name)]
end

#subst_norwegian_chars(txt) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/paxx/names/name_normalizer.rb', line 20

def subst_norwegian_chars(txt)
  [["æ", "ae"], ["ø", "oe"], ["å", "aa"]].each do |int|
    txt = txt.gsub(int[0], int[1])
  end
  [["Æ", "AE"], ["Ø", "OE"], ["Å", "AA"]].each do |int|
    txt = txt.gsub(int[0], int[1])
  end
  txt
end

#validObject



11
12
13
# File 'lib/paxx/names/name_normalizer.rb', line 11

def valid
  !(name.to_s.strip.length == 0)
end