Class: Lafcadio::English
- Inherits:
-
Object
- Object
- Lafcadio::English
- Defined in:
- lib/lafcadio/util/English.rb
Overview
A collection of English-language specific utility methods.
Class Method Summary collapse
-
.camelCaseToEnglish(camelCaseStr) ⇒ Object
Turns a camel-case string (“camelCaseToEnglish”) to plain English (“camel case to english”).
-
.englishToCamelCase(englishStr) ⇒ Object
Turns an English language string into camel case.
-
.plural(singular) ⇒ Object
Given a singular noun, returns the plural form.
-
.properNoun(string) ⇒ Object
Returns the proper noun form of a string by capitalizing most of the words.
-
.sentence(format, name, number = 1) ⇒ Object
Given a format for a template sentence, generates the sentence while accounting for details such as pluralization and whether to use “a” or “an”.
-
.singular(plural) ⇒ Object
Given a noun in plural form, returns its singular version.
-
.startsWithVowelSound(word) ⇒ Object
Does this word start with a vowel sound? “User” and “usury” don’t, but “ugly” does.
Class Method Details
.camelCaseToEnglish(camelCaseStr) ⇒ Object
Turns a camel-case string (“camelCaseToEnglish”) to plain English (“camel case to english”). Each word is decapitalized.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/lafcadio/util/English.rb', line 6 def English.camelCaseToEnglish(camelCaseStr) words = [] nextCapIndex =(camelCaseStr =~ /[A-Z]/) while nextCapIndex != nil words << $` if $`.size > 0 camelCaseStr = $& + $' camelCaseStr[0] = camelCaseStr[0..0].downcase nextCapIndex =(camelCaseStr =~ /[A-Z]/) end words << camelCaseStr words.join ' ' end |
.englishToCamelCase(englishStr) ⇒ Object
Turns an English language string into camel case.
108 109 110 111 112 113 114 115 |
# File 'lib/lafcadio/util/English.rb', line 108 def English.englishToCamelCase(englishStr) cc = "" englishStr.split.each { |word| word = word.capitalize unless cc == '' cc = cc += word } cc end |
.plural(singular) ⇒ Object
Given a singular noun, returns the plural form.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/lafcadio/util/English.rb', line 60 def English.plural(singular) consonantYPattern = Regexp.new("([^aeiou])y$", Regexp::IGNORECASE) if singular =~ consonantYPattern singular.gsub consonantYPattern, '\1ies' elsif singular =~ /[xs]$/ singular + "es" else singular + "s" end end |
.properNoun(string) ⇒ Object
Returns the proper noun form of a string by capitalizing most of the words.
Examples:
English.properNoun("bosnia and herzegovina") ->
"Bosnia and Herzegovina"
English.properNoun("macedonia, the former yugoslav republic of") ->
"Macedonia, the Former Yugoslav Republic of"
English.properNoun("virgin islands, u.s.") ->
"Virgin Islands, U.S."
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/lafcadio/util/English.rb', line 81 def English.properNoun(string) properNoun = "" while(matchIndex = string =~ /[\. ]/) word = string[0..matchIndex-1] word = word.capitalize unless [ 'and', 'the', 'of' ].index(word) != nil properNoun += word + $& string = string[matchIndex+1..string.length] end word = string word = word.capitalize unless [ 'and', 'the', 'of' ].index(word) != nil properNoun += word properNoun end |
.sentence(format, name, number = 1) ⇒ Object
Given a format for a template sentence, generates the sentence while accounting for details such as pluralization and whether to use “a” or “an”.
- format
-
The format string. Format codes are:
-
%num: Number
-
%is: Transitive verb. This will be turned into “is” or “are”, depending on
number
. -
%nam: Name. This will be rendered as either singular or plural, depending on
number
. -
%a: Indefinite article. This will be turned into “a” or “an”, depending on
name
.
-
- name
-
The name of the object being described.
- number
-
The number of the objects being describes.
Examples:
English.sentence("There %is currently %num %nam", "product category",
0) -> "There are currently 0 product categories"
English.sentence("There %is currently %num %nam", "product category",
1) -> "There is currently 1 product category"
English.sentence("Add %a %nam", "invoice") -> "Add an invoice"
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/lafcadio/util/English.rb', line 39 def English.sentence(format, name, number = 1) sentence = format sentence.gsub!( /%num/, number.to_s ) isVerb = number == 1 ? "is" : "are" sentence.gsub!( /%is/, isVerb ) name = English.plural name if number != 1 sentence.gsub!( /%nam/, name ) article = startsWithVowelSound(name) ? 'an' : 'a' sentence.gsub!( /%a/, article ) sentence end |
.singular(plural) ⇒ Object
Given a noun in plural form, returns its singular version.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/lafcadio/util/English.rb', line 96 def English.singular(plural) if plural =~ /(.*)ies/ $1 + 'y' elsif plural =~ /(.*s)es/ $1 else plural =~ /(.*)s/ $1 end end |
.startsWithVowelSound(word) ⇒ Object
Does this word start with a vowel sound? “User” and “usury” don’t, but “ugly” does.
53 54 55 56 57 |
# File 'lib/lafcadio/util/English.rb', line 53 def English.startsWithVowelSound(word) uSomethingUMatch = word =~ /^u[^aeiuo][aeiou]/ # 'user' and 'usury' don't start with a vowel sound word =~ /^[aeiou]/ && !uSomethingUMatch end |