Module: ConsistentCompanyRuby::InstanceMethods

Included in:
String
Defined in:
lib/consistent_company_ruby/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#is_company_word(in_word) ⇒ Object



208
209
210
211
212
213
214
# File 'lib/consistent_company_ruby/instance_methods.rb', line 208

def is_company_word(in_word)
  if ["ADV","ADVERTISING","AGCY","AGENCY","AGY","ASC","ASS","ASSN","ASSOC","ASSOCIAT","ASSOCIATES","ASSOCIATION","ATTORNEY","ATTRNY","ATTY","ATY","AUTO","CO","COMP","COMPANIES","COMPANY","CORP","CORPORATION","CT","CONTRA","DEPARTMENT","DEPT","DIR","DIRECT","DIV","DIVISION","GROUP","HOLDINGS","INC","INCORPORATED","INT","LIMITED","LLC","LLP","LOCAL","LTD","PC","PLC","PROD","PRODS","PRODUCT","PRODUCTIONS","PRODUCTS","TR","TRADE"].include?(in_word)
    true
  else
    false
  end
end

#namerObject

company



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/consistent_company_ruby/instance_methods.rb', line 3

def namer# company
  return self if self == ''

  in_string = self.dup

  # while processing we turn & = AND, + = PLUS
  # and we add space at front and back
  in_string.gsub('&',' & ')
  in_string.gsub('+',' + ')

  in_string = in_string.upcase
  in_string.strip!

  # in_string.gsub(/\(|\)/,' ')
  num_lefts = 0
  num_rights = 0
  left1 = -1
  right1 = -1
  left2 = -1
  right2 = -1
  len = in_string.size

  (0..len-1).each do |i|
    if in_string[i] == '('
      num_lefts += 1
      if num_lefts == 1
        left1 = i
      elsif num_lefts == 2
        left2 = i
      end
    elsif in_string[i] == ')'
      num_rights += 1
      if num_rights == 1
        right1 = i
      elsif num_rights == 2
        right2 = i
      end
    end
  end

  if num_lefts == 0 || in_string[0] == '('
    # Do Nothing
 elsif num_lefts == 1
 	if right1 > left1
      # ..(xx).. -> ....
      in_string = "#{in_string[0..left1-1]}#{in_string[right1+1..len-1]}"
    else
      # ..(xx -> ..
      in_string = in_string[0..left1-1]
    end
  elsif num_lefts == 2
    if (left1 < right1) && (right1 < left2) && (left2 < right2)
      # ..(xx)..(xx).. -> ......
      in_string = "#{in_string[0..left1-1]}#{in_string[right1+1..left2-1]}#{in_string[right2+1..len-1]}"
    elsif (left1 < left2) && (left2 < right1) && (right1 < right2)
      # ..(xx(xx)xx).. -> ....
      in_string = "#{in_string[0..left1-1]}#{in_string[right2+1..len-1]}"
    elsif (left1 < right1) && (right1 < left2) && (right2 == -1)
      # ..(xx)..(xx -> ....
      in_string = "#{in_string[0..left1-1]}#{in_string[right1+1..left2-1]}"
    elsif (left1 < left2) && (left2 < right1) && (right2 == -1)
      # ..(xx(xx)xx -> ..
      in_string = in_string[0..left1-1]
    elsif (right1 == -1) && (right2 == -1)
      # ..(xx(xx -> ..
      in_string = in_string[0..left1-1]
    end
  end

  return_string = ''
  (0..in_string.size-1).each do |i|
    ch = in_string[i]
    asc = ch.ord

    if (asc >= 65 && asc <= 90) ||
        (asc >= 48 && asc <= 57) ||
        asc > 128 # A-Z, 0-9, and high order chars
      return_string = "#{return_string}#{ch}"
    elsif asc == 39 # '
      # not keeping it
    elsif asc == 38 && return_string.size > 0 # &
      if return_string[return_string.size-1] != ' '
        return_string = "#{return_string} AND "
      else
        return_string = "#{return_string}AND "
      end
    elsif asc == 43 # +
      if return_string == 'A' || return_string == 'A '
        return_string = 'A PLUS '
      elsif return_string.size > 0
        if return_string[return_string.size-1] != ' '
          return_string = "#{return_string} AND "
        else
          return_string = "#{return_string}AND "
        end
      end
    elsif return_string.size > 0 &&
           return_string[return_string.size-1] != ' '
      return_string = "#{return_string} "
    end
    # puts "#{i}#{ch}#{asc}:#{return_string}"
  end

  return '' if return_string.nil? || return_string == ''

  str_replace return_string, ' AND ', ' & '

  return_string.strip!
  return_string = transform_company return_string
  return_string.strip!
  return_string.gsub!(' ','')
  return_string
end

#str_replace(orig, rep, with) ⇒ Object

It’s more than a gsub



217
218
219
220
221
222
223
# File 'lib/consistent_company_ruby/instance_methods.rb', line 217

def str_replace(orig, rep, with)
  s = orig
  while s.include?(rep)
    s.sub! rep, with
  end
  orig
end

#transform_company(res) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/consistent_company_ruby/instance_methods.rb', line 117

def transform_company res
  res = " #{res} "
  str_replace res,  " THE ", " "
  str_replace res,  " ONE ", " 1 "
  str_replace res,  " TWO ", " 2 "
  str_replace res,  " TO ", " 2 "
  str_replace res,  " THREE ", " 3 "
  str_replace res,  " FOUR ", " 4 "
  str_replace res,  " FOR ", " 4 "
  str_replace res,  " FIVE ", " 5 "
  str_replace res,  " SIX ", " 6 "
  str_replace res,  " SEVEN ", " 7 "
  str_replace res,  " EIGHT ", " 8 "
  str_replace res,  " NINE ", " 9 "
  str_replace res,  " TEN ", " 10 "
  str_replace res,  " ELEVEN ", " 11 "

  str_replace res,  " FIRST ", " 1ST "
  str_replace res,  " SECOND ", " 2ND "
  str_replace res,  " THIRD ", " 3RD "
  str_replace res,  " FOURTH ", " 4TH "
  str_replace res,  " FIFTH ", " 5TH "
  str_replace res,  " SIXTH ", " 6TH "
  str_replace res,  " SEVENTH ", " 7TH "
  str_replace res,  " EIGHTH ", " 8TH "
  str_replace res,  " NINTH ", " 9TH "
  str_replace res,  " TENTH ", " 10TH "
  str_replace res,  " CENTRE ", " CTR "
  str_replace res,  " CENTER ", " CTR "
  str_replace res,  " CNTR ", " CTR "
  str_replace res,  " CENT ", " CTR "
  str_replace res,  " CENTR ", " CTR "
  str_replace res,  " AUTOMOTIVE ", " AUTO "
  str_replace res,  " AUTOMOBILE ", " AUTO "
  str_replace res,  " AUTOS ", " AUTO "
  str_replace res,  " AVENUE ", " AVE "
  str_replace res,  " DRIVE ", " DR "
  str_replace res,  " PHOTOGRAPHY ", " PHOTO "
  str_replace res,  " BROTHERS ", " BROS "
  str_replace res,  " TECHNOLOGY ", " TEC "
  str_replace res,  " TECH ", " TEC "
  str_replace res,  " TELEVISION ", " TV "
  str_replace res,  " INFORMATION ", " INFO "
  str_replace res,  " SOCIETY ", " SOC "
  str_replace res,  " DEPARTMENT ", " DEPT "
  str_replace res,  " REGIONAL ", " REG "
  str_replace res,  " REGION ", " REG "
  str_replace res,  " AUTHORITY ", " AUTH "
  str_replace res,  " NATIONAL ", " NATL "
  str_replace res,  " INTERNATIONAL ", " INT "
  str_replace res,  " INTERNATION ", " INT "
  str_replace res,  " INTL ", " INT "
  str_replace res,  " MARKETING ", " MKT "
  str_replace res,  " MKTG ", " MKT "
  str_replace res,  " MANAGEMENT ", " MGT "
  str_replace res,  " MGMT ", " MGT "

  res.strip!
  space_loc = res.index ' '

  if space_loc && res.size > 3

    # Check for "A" as the first word, and
    # make sure that second word is not an initital or the word "PLUS"
    # For example: "A C & R" do not remove "A"; "A TOUCH OF CLASS" remove the "A"
    if res.start_with?('A ') &&
        !res[2..res.size].start_with?('&') &&
        !res[3..res.size].start_with?(' ') &&
        !res[2..res.size].start_with?('PLUS')
     res.gsub! 'A ', ''
    end

    # Remove last word if it is a company word or &
    space_loc = res.index ' '
    if space_loc
      res_array = res.split(' ')
      if is_company_word(res_array.last)
        res_array.pop
        if is_company_word(res_array.last) # Look at the new last word
          res_array.pop
        end
        res = res_array.join(' ')
      end

      res = res[0..res.size-2] if res.end_with? '&'
    end
  end

  res
end