englishext 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/englishext.rb +12 -7
  2. data/lib/englishext.rb~ +124 -0
  3. metadata +20 -11
data/lib/englishext.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # A collection of English-language specific utility methods.
2
2
  module EnglishExtensions
3
- Version = '0.1.0'
3
+ Version = '0.1.1'
4
4
  end
5
5
 
6
6
  class String
@@ -53,15 +53,20 @@ class String
53
53
  # "bosnia and herzegovina".proper_noun -> "Bosnia and Herzegovina"
54
54
  # "macedonia, the former yugoslav republic of".proper_noun ->
55
55
  # "Macedonia, the Former Yugoslav Republic of"
56
- # "virgin islands, u.s.".proper_noun -> "Virgin Islands, U.S."
56
+ # "virgin islands, u.s.".proper_noun -> "Virgin Islands, U.S."
57
57
  def proper_noun
58
58
  proper_noun = ""
59
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]
60
+ while(matchIndex = str =~ /[\. \-\']/)
61
+ if matchIndex == 0
62
+ proper_noun += $&
63
+ str = str[matchIndex+1..str.length]
64
+ else
65
+ word = str[0..matchIndex-1]
66
+ word = word.capitalize unless [ 'and', 'the', 'of' ].index(word) != nil
67
+ proper_noun += word + $&
68
+ str = str[matchIndex+1..str.length]
69
+ end
65
70
  end
66
71
  word = str
67
72
  word = word.capitalize unless [ 'and', 'the', 'of' ].index(word) != nil
@@ -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 CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.6
2
+ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: englishext
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2005-07-03
6
+ version: 0.1.1
7
+ date: 2006-08-20 00:00:00 -04:00
8
8
  summary: EnglishExtensions contains a few convenience methods for String.
9
9
  require_paths:
10
- - lib
10
+ - lib
11
11
  email: sera@fhwang.net
12
12
  homepage: http://englishext.rubyforge.org/
13
13
  rubyforge_project:
@@ -18,20 +18,29 @@ bindir: bin
18
18
  has_rdoc: false
19
19
  required_ruby_version: !ruby/object:Gem::Version::Requirement
20
20
  requirements:
21
- -
22
- - ">"
23
- - !ruby/object:Gem::Version
24
- version: 0.0.0
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
25
24
  version:
26
25
  platform: ruby
26
+ signing_key:
27
+ cert_chain:
27
28
  authors:
28
- - Francis Hwang
29
+ - Francis Hwang
29
30
  files:
30
- - lib/englishext.rb
31
+ - lib/englishext.rb
32
+ - lib/englishext.rb~
31
33
  test_files: []
34
+
32
35
  rdoc_options: []
36
+
33
37
  extra_rdoc_files: []
38
+
34
39
  executables: []
40
+
35
41
  extensions: []
42
+
36
43
  requirements: []
37
- dependencies: []
44
+
45
+ dependencies: []
46
+