Class: Air18n::AmericanToColonial
- Inherits:
-
Object
- Object
- Air18n::AmericanToColonial
- Defined in:
- lib/air18n/prim_and_proper.rb
Overview
Converts text from American to British-like English. Transforms covered are: 1) Spelling variations: [trave]led/lled, [real]ize/ise, [flav]or/our, [cent]er/re, plus
these extras: learned/learnt, practices/practises, airplane/aeroplane,
inquire/enquire, specialties/specialities, inquiry/enquiry,
artifact/artefact, program/programme, livable/liveable, among/amongst,
spelled/spelt, judgment/judgement, defense/defence, fulfill/fulfil,
spilled/spilt
2) Vocabulary variations: vacation/holiday, elevator/lift,
couch/sofa, coupon/voucher, cell/mobile
Direct Known Subclasses
AmericanToAustralian, AmericanToBritish, AmericanToCanadian, AmericanToKiwi, AmericanToSinglish
Constant Summary collapse
- VOWELS =
Set.new(["a","e","i","o","u"])
- PUNCTUATION_REGEXP =
/[.,?!"()]/
- SPELLING_VARIANTS =
YAML.load_file(File.('../../../config/colonial_spelling_variants.yml', __FILE__))
- VOCAB_VARIANTS =
This list is culled from suggestion from the first UK Managing Director.
{"vacation" => "holiday", "vacations" => "holidays", "vacation's" => "holiday's", "elevator" => "lift", "elevators" => "lifts", "elevator's" => "lift's", "couch" => "sofa", "couches" => "sofas", "couch's" => "sofa's", "coupon" => "voucher", "coupons" => "vouchers", "coupon's" => "voucher's", "cell" => "mobile"}
Instance Method Summary collapse
-
#change_word_to_variant(word) ⇒ Object
Returns a structure with these two infos: :variant - British variant for word if there is one, with the right punctuation and case of first letter :change_previous_article - If the British variant needs a different “a”/“an”, this will have either “a” or “an”.
- #convert(text) ⇒ Object
-
#initialize ⇒ AmericanToColonial
constructor
A new instance of AmericanToColonial.
-
#replace(word, variant, stripped_word = nil) ⇒ Object
Returns word with variant, keeping punctuation and the case of the first letter.
Constructor Details
#initialize ⇒ AmericanToColonial
Returns a new instance of AmericanToColonial.
46 47 48 |
# File 'lib/air18n/prim_and_proper.rb', line 46 def initialize @variants = SPELLING_VARIANTS.merge(VOCAB_VARIANTS) end |
Instance Method Details
#change_word_to_variant(word) ⇒ Object
Returns a structure with these two infos: :variant - British variant for word if there is one, with the right
punctuation and case of first letter
:change_previous_article - If the British variant needs a different
"a"/"an", this will have either "a" or "an".
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/air18n/prim_and_proper.rb', line 70 def change_word_to_variant(word) {}.tap do |ret| stripped_word = word.gsub(PUNCTUATION_REGEXP, "") stripped_word_downcase = stripped_word.downcase if @variants.include?(stripped_word_downcase) plain_variant = @variants[stripped_word_downcase] ret[:variant] = replace(word, plain_variant, stripped_word) if VOWELS.include?(stripped_word_downcase[0..0]) ^ VOWELS.include?(plain_variant[0..0]) ret[:change_previous_article] = VOWELS.include?(plain_variant[0..0]) ? "an" : "a" end end end end |
#convert(text) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/air18n/prim_and_proper.rb', line 50 def convert(text) words = text.split words.each_with_index.inject([]) do |carry, (word, index)| variant_info = change_word_to_variant(word) if variant_info[:change_previous_article] && index > 0 && plain_previous_word = carry[index - 1].downcase.gsub(PUNCTUATION_REGEXP, "") if ["a", "an"].include?(plain_previous_word) carry[index - 1] = replace(carry[index - 1], variant_info[:change_previous_article]) end end carry << (variant_info[:variant] || word) carry end.join(" ") end |
#replace(word, variant, stripped_word = nil) ⇒ Object
Returns word with variant, keeping punctuation and the case of the first letter.
86 87 88 89 90 91 92 93 94 |
# File 'lib/air18n/prim_and_proper.rb', line 86 def replace(word, variant, stripped_word = nil) stripped_word ||= word.gsub(PUNCTUATION_REGEXP, "") stripped_word_downcase = stripped_word.downcase word_first_letter = stripped_word[0..0] downcase_word_first_letter = stripped_word_downcase[0..0] replacement = (downcase_word_first_letter == word_first_letter ? variant[0..0] : variant[0..0].upcase) + variant[1..-1] word.gsub(stripped_word, replacement) end |