englishext 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/englishext.rb +124 -0
- metadata +37 -0
data/lib/englishext.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# A collection of English-language specific utility methods.
|
2
|
+
module EnglishExtensions
|
3
|
+
Version = '0.1.0'
|
4
|
+
end
|
5
|
+
|
6
|
+
class String
|
7
|
+
# Turns a camel-case string ("camel_case_to_english") to plain English
|
8
|
+
# ("camel case to english"). Each word is decapitalized.
|
9
|
+
def camel_case_to_english
|
10
|
+
words = []
|
11
|
+
str = self.clone
|
12
|
+
nextCapIndex =(str =~ /[A-Z]/)
|
13
|
+
while nextCapIndex != nil
|
14
|
+
words << $` if $`.size > 0
|
15
|
+
str = $& + $'
|
16
|
+
str[0] = str[0..0].downcase
|
17
|
+
nextCapIndex =(str =~ /[A-Z]/)
|
18
|
+
end
|
19
|
+
words << str
|
20
|
+
words.join ' '
|
21
|
+
end
|
22
|
+
|
23
|
+
# Turns an English language string into camel case.
|
24
|
+
def english_to_camel_case
|
25
|
+
cc = ""
|
26
|
+
split.each { |word|
|
27
|
+
word = word.capitalize unless cc == ''
|
28
|
+
cc = cc += word
|
29
|
+
}
|
30
|
+
cc
|
31
|
+
end
|
32
|
+
|
33
|
+
# Given a singular noun, returns the plural form.
|
34
|
+
def plural
|
35
|
+
consonantYPattern = Regexp.new("([^aeiou])y$", Regexp::IGNORECASE)
|
36
|
+
if self =~ consonantYPattern
|
37
|
+
self.gsub consonantYPattern, '\1ies'
|
38
|
+
elsif self =~ /^(.*)xis$/
|
39
|
+
$1 + 'xes'
|
40
|
+
elsif self =~ /[xs]$/
|
41
|
+
self + "es"
|
42
|
+
elsif self =~ /(.*)tum$/
|
43
|
+
$1 + 'ta'
|
44
|
+
else
|
45
|
+
self + "s"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Returns the proper noun form of a string by capitalizing most of the
|
50
|
+
# words.
|
51
|
+
#
|
52
|
+
# Examples:
|
53
|
+
# "bosnia and herzegovina".proper_noun -> "Bosnia and Herzegovina"
|
54
|
+
# "macedonia, the former yugoslav republic of".proper_noun ->
|
55
|
+
# "Macedonia, the Former Yugoslav Republic of"
|
56
|
+
# "virgin islands, u.s.".proper_noun -> "Virgin Islands, U.S."
|
57
|
+
def proper_noun
|
58
|
+
proper_noun = ""
|
59
|
+
str = self.clone
|
60
|
+
while(matchIndex = str =~ /[\. ]/)
|
61
|
+
word = str[0..matchIndex-1]
|
62
|
+
word = word.capitalize unless [ 'and', 'the', 'of' ].index(word) != nil
|
63
|
+
proper_noun += word + $&
|
64
|
+
str = str[matchIndex+1..str.length]
|
65
|
+
end
|
66
|
+
word = str
|
67
|
+
word = word.capitalize unless [ 'and', 'the', 'of' ].index(word) != nil
|
68
|
+
proper_noun += word
|
69
|
+
proper_noun
|
70
|
+
end
|
71
|
+
|
72
|
+
def singular
|
73
|
+
if self =~ /(.*)ies/
|
74
|
+
$1 + 'y'
|
75
|
+
elsif self =~ /(.*s)es/
|
76
|
+
$1
|
77
|
+
else
|
78
|
+
self =~ /(.*)s/
|
79
|
+
$1
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Does this word start with a vowel sound? "User" and "usury" don't, but
|
84
|
+
# "ugly" does.
|
85
|
+
def starts_with_vowel_sound
|
86
|
+
uSomethingUMatch = self =~ /^u[^aeiuo][aeiou]/
|
87
|
+
# 'user' and 'usury' don't start with a vowel sound
|
88
|
+
self =~ /^[aeiou]/ && !uSomethingUMatch
|
89
|
+
end
|
90
|
+
|
91
|
+
# Given a format for a template sentence, generates the sentence while
|
92
|
+
# accounting for details such as pluralization and whether to use "a" or
|
93
|
+
# "an".
|
94
|
+
#
|
95
|
+
# Format codes are:
|
96
|
+
# * %num: Number
|
97
|
+
# * %is: Transitive verb. This will be turned into "is" or "are",
|
98
|
+
# depending on <tt>number</tt>.
|
99
|
+
# * %nam: Name. This will be rendered as either singular or
|
100
|
+
# plural, depending on <tt>number</tt>.
|
101
|
+
# * %a: Indefinite article. This will be turned into "a" or "an",
|
102
|
+
# depending on <tt>name</tt>.
|
103
|
+
# [name] The name of the object being described.
|
104
|
+
# [number] The number of the objects being describes.
|
105
|
+
#
|
106
|
+
# Examples:
|
107
|
+
# "There %is currently %num %nam".template( "product category", 0 ) ->
|
108
|
+
# "There are currently 0 product categories"
|
109
|
+
# "There %is currently %num %nam".template( "product category", 1 ) ->
|
110
|
+
# "There is currently 1 product category"
|
111
|
+
# "Add %a %nam".template( "invoice" ) -> "Add an invoice"
|
112
|
+
def template( name, number = 1)
|
113
|
+
sentence = clone
|
114
|
+
sentence.gsub!( /%num/, number.to_s )
|
115
|
+
isVerb = number == 1 ? "is" : "are"
|
116
|
+
sentence.gsub!( /%is/, isVerb )
|
117
|
+
name = name.plural if number != 1
|
118
|
+
sentence.gsub!( /%nam/, name )
|
119
|
+
article = name.starts_with_vowel_sound ? 'an' : 'a'
|
120
|
+
sentence.gsub!( /%a/, article )
|
121
|
+
sentence
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
metadata
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.6
|
3
|
+
specification_version: 1
|
4
|
+
name: englishext
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2005-07-03
|
8
|
+
summary: EnglishExtensions contains a few convenience methods for String.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: sera@fhwang.net
|
12
|
+
homepage: http://englishext.rubyforge.org/
|
13
|
+
rubyforge_project:
|
14
|
+
description: EnglishExtensions contains a few convenience methods for String.
|
15
|
+
autorequire: englishext
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
-
|
22
|
+
- ">"
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.0.0
|
25
|
+
version:
|
26
|
+
platform: ruby
|
27
|
+
authors:
|
28
|
+
- Francis Hwang
|
29
|
+
files:
|
30
|
+
- lib/englishext.rb
|
31
|
+
test_files: []
|
32
|
+
rdoc_options: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
requirements: []
|
37
|
+
dependencies: []
|