9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/cleanliness/dictionary.rb', line 9
def self.translate(string)
@dictionary = initialize
replace ={}
rgx = Regexp.new "#{@dictionary.keys.map{|k| "(#{k})"}.join("|")}", Regexp::IGNORECASE
scan_matches = string.scan(rgx).flatten.reject!(&:blank?)
return string if scan_matches.nil?
scan_matches.each{|k|
replace[k] = (k =~ /^[A-Z]{1}/).nil? ? @dictionary[k.downcase] : @dictionary[k.downcase].capitalize
}
match = Regexp.new scan_matches.map{|k| "(#{k})"}.join("|")
return string.gsub(match, replace)
end
|