In Files
- lib/english/participle.rb
 - lib/english/ordinal.rb
 - lib/english/censor.rb
 - lib/english/namecase.rb
 - lib/english/dresner.rb
 - lib/english/jumble.rb
 - lib/english/soundex.rb
 - lib/english/class.rb
 - lib/english/metaphone.rb
 - lib/english/inflect.rb
 - lib/english/porter.rb
 - lib/english/infinitive.rb
 - lib/english/numeral.rb
 - lib/english/conjunction.rb
 
Parent
- Language
 
Namespace
Methods
- ::abbreviation
 - ::conjunction
 - ::dirty_words
 - ::double_metaphone
 - ::dresner
 - ::infinitive
 - ::infinitives
 - ::jumble
 - ::metaphone
 - ::namecase
 - ::numeral
 - ::ordinal
 - ::plural
 - ::present_participle
 - ::singular
 - ::soundex
 - ::soundex_code
 - ::stem_porter
 - ::trio
 - #conjunction
 - #double_metaphone
 - #dresner
 - #infinitive
 - #infinitives
 - #jumble
 - #metaphone
 - #namecase
 - #numeral
 - #ordinal
 - #plural
 - #pluralize
 - #singular
 - #singularize
 - #soundex
 - #stem_porter
 
Information
English
Public Class Methods
# File lib/english/class.rb, line 5 5: def self.abbreviation 6: 'en' 7: end
English conjunciton.
This is more advanced form of join, as it allows for an English-based terminating separator.
The default type of conjunction —the terminating separator, is “and”, and the default regualt separator is “,”.
[1,2,3].conjunction => "1, 2 and 3 [1,2,3].conjunction(:or) => "1, 2 or 3 [1,2,3].conjunction(:or, ';') => "1; 2 or 3
    # File lib/english/conjunction.rb, line 26
26:   def self.conjunction(array, type=:and, seperator=",")
27:     array     = array.to_a
28:     type      = type.to_s
29:     separator = separator.to_s
30:     space     = space.to_s
31: 
32:     case array.length
33:     when 0
34:       ""
35:     when 1
36:       array[0]
37:     when 2
38:       "#{array[0]} #{type} #{array[1]}"
39:     else
40:       [array[0..2].join("#{separator} "), array[1]].join(" #{type} ")
41:     end
42:   end
          # File lib/english/censor.rb, line 5 5: def self.dirty_words 6: %{fuck shit cunt pussy dick asshole niger} 7: end
Scramble the inner characters of words leaving the text still readable (research at Cambridge University, code by KurtDresner).
For example, the above text may result in:
Srblamce the iennr cchrteaars of wodrs lvenaig the txet stlil rbeaadle (rreceash at Cbamigdre Uverintisy, cdoe by KrneruestDr?)
CREDIT: Kurt Dresener
    # File lib/english/dresner.rb, line 15
15:   def self.dresner(string)
16:     string.to_s.gsub(/\B\w+\B/){$&.split(//).sort_by{rand}}
17:   end
          Return the infinitive form of the given word.
     # File lib/english/infinitive.rb, line 841
841:   def self.infinitive(word)
842:     Infinitive.infinitives(word).first
843:   end
          Return a list of possible infinitive forms of the given word.
     # File lib/english/infinitive.rb, line 836
836:   def self.infinitives(word)
837:     Infinitive.infinitives(word)
838:   end
          Jumble letter casing.
"superman".jumble #=> "SUpeRmAn"
    # File lib/english/jumble.rb, line 14
14:   def self.jumble(string)
15:     j = ''
16:     string.to_s.split(//).each_with_index{ |c,i| j << ( i % 2 == 0 ? c.downcase : c.upcase ) }
17:     j
18:   end
          Returns a new String with the contents properly namecased.
Perl Version Copyright © Mark Summerfield 1998-2002
Ruby Version Copyright © Aaron Patterson 2006
    # File lib/english/namecase.rb, line 12
12:   def self.namecase(name)
13:     localstring = name.downcase
14:     localstring.gsub!(/\b\w/) { |first| first.upcase }
15:     localstring.gsub!(/\'\w\b/) { |c| c.downcase } # Lowercase 's
16: 
17:     # Fixes for "Mac".
18:     if localstring =~ /\bMac[A-Za-z]{2,}[^aciozj]\b/ or localstring =~ /\bMc/
19:       localstring.gsub!(/\b(Ma?c)([A-Za-z]+)/) { |match| $1 + $2.capitalize }
20:       localstring.gsub!(/\bMacEvicius/, 'Macevicius')
21:       localstring.gsub!(/\bMacHado/, 'Machado')
22:       localstring.gsub!(/\bMacHar/, 'Machar')
23:       localstring.gsub!(/\bMacHin/, 'Machin')
24:       localstring.gsub!(/\bMacHlin/, 'Machlin')
25:       localstring.gsub!(/\bMacIas/, 'Macias')
26:       localstring.gsub!(/\bMacIulis/, 'Maciulis')
27:       localstring.gsub!(/\bMacKie/, 'Mackie')
28:       localstring.gsub!(/\bMacKle/, 'Mackle')
29:       localstring.gsub!(/\bMacKlin/, 'Macklin')
30:       localstring.gsub!(/\bMacQuarie/, 'Macquarie')
31:     end
32:     localstring.gsub!('Macmurdo','MacMurdo')
33: 
34:     # Fixes for "son (daughter) of" etc.
35:     localstring.gsub!(/\bAl(?=\s+\w)/, 'al')      # al Arabic or forename Al.
36:     localstring.gsub!(/\bAp\b/, 'ap')             # ap Welsh.
37:     localstring.gsub!(/\bBen(?=\s+\w)/,'ben')     # ben Hebrew or forename Ben.
38:     localstring.gsub!(/\bDell([ae])\b/,'dell\1')  # della and delle Italian.
39:     localstring.gsub!(/\bD([aeiu])\b/,'d\1')      # da, de, di Italian; du French.
40:     localstring.gsub!(/\bDe([lr])\b/,'de\1')      # del Italian; der Dutch/Flemish.
41:     localstring.gsub!(/\bEl\b/,'el')              # el Greek or El Spanish.
42:     localstring.gsub!(/\bLa\b/,'la')              # la French or La Spanish.
43:     localstring.gsub!(/\bL([eo])\b/,'l\1')        # lo Italian; le French.
44:     localstring.gsub!(/\bVan(?=\s+\w)/,'van')     # van German or forename Van.
45:     localstring.gsub!(/\bVon\b/,'von')            # von Dutch/Flemish
46: 
47:     # Fix roman numeral names
48:     localstring.gsub!(
49:       / \b ( (?: [Xx]{1,3} | [Xx][Ll]   | [Ll][Xx]{0,3} )?
50:              (?: [Ii]{1,3} | [Ii][VvXx] | [Vv][Ii]{0,3} )? ) \b /
51:     ) { |match| match.upcase }
52: 
53:     localstring
54:   end
          Convert an integer to the english spelling of that number.
    # File lib/english/numeral.rb, line 16
16:   def self.numeral(integer)
17:     places = integer.to_i.to_s.split(//).collect{|s| s.to_i}.reverse
18:     name = []
19:     ((places.length + 2) / 3).times do |p|
20:       strings = trio(places[p * 3, 3])
21:       name.push(MEGA[p]) if strings.length > 0 and p > 0
22:       name += strings
23:     end
24:     name.push(ONES[0]) unless name.length > 0
25:     name.reverse.join(" ")
26:   end
          Ordinalize turns a number string into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
  English.ordinal('1')  #=> "1st"
  English.ordinal('2')  #=> "2nd"
  English.ordinal(102)  #=> "102nd"
  English.ordinal(103)  #=> "103rd"
          
          
          
    # File lib/english/ordinal.rb, line 14
14:   def self.ordinal(number)
15:     number_string = number.to_s.strip
16:     if md = /\d{1,2}$/.match(number_string)
17:       n = md[0].to_i
18:       if (11..13).include?(number.to_i % 100)
19:         "#{number}th"
20:       else
21:         case n % 10
22:           when 1; "#{number}st"
23:           when 2; "#{number}nd"
24:           when 3; "#{number}rd"
25:           else    "#{number}th"
26:         end
27:       end      
28:       #number_string.sub(/\d{1,2}$/, r)
29:     else
30:       number_string
31:     end
32:   end
          
     # File lib/english/inflect.rb, line 295
295:   def self.plural(string)
296:     English::Inflect.plural(string)
297:   end
          
    # File lib/english/participle.rb, line 3
 3:   def self.present_participle(word)
 4:     plural = plural_verb(word.to_s, 2)
 5: 
 6:     plural.sub!( /ie$/, 'y' ) or
 7:       plural.sub!( /ue$/, 'u' ) or
 8:       plural.sub!( /([auy])e$/, '$1' ) or
 9:       plural.sub!( /i$/, '' ) or
10:       plural.sub!( /([^e])e$/, "\\1" ) or
11:       /er$/.match( plural ) or
12:       plural.sub!( /([^aeiou][aeiouy]([bdgmnprst]))$/, "\\1\\2" )
13: 
14:     return "#{plural}ing"
15:   end
          
     # File lib/english/inflect.rb, line 290
290:   def self.singular(string)
291:     English::Inflect.singular(string)
292:   end
          Ruby implementation of the Soundex algorithm, as described by Knuth in volume 3 of The Art of Computer Programming.
Returns nil if the value couldn’t be calculated b/c of empty-string or invalid character.
"Ruby".soundex #=> "R100"
Based on work by Michael Neumann (neumann@s-direktnet.de)
    # File lib/english/soundex.rb, line 16
16:   def self.soundex(string)
17:     return nil if string.empty?
18: 
19:     str = string.upcase
20:     last_code = soundex_code(str[0,1])
21:     soundex_code = str[0,1]
22: 
23:     for index in 1...(str.size) do
24:       return soundex_code if soundex_code.size == 4
25: 
26:       code = soundex_code(str[index,1])
27: 
28:       if code == "0" then
29:         last_code = nil
30:       elsif code == nil then
31:         return nil
32:       elsif code != last_code then
33:         soundex_code += code
34:         last_code = code
35:       end
36:     end
37: 
38:     return soundex_code + "000"[0,4-soundex_code.size]
39:   end
          Support function for soundex. Returns code for a single character.
    # File lib/english/soundex.rb, line 44
44:   def self.soundex_code(char)
45:     char.tr! "AEIOUYWHBPFVCSKGJQXZDTLMNR", "00000000111122222222334556"
46:   end
          Returns the word stem using the Porter Stemming algorithm by Martin Porter.
     # File lib/english/porter.rb, line 190
190:   def self.stem_porter(string)
191:     PorterStemmer.stem(string)
192:   end
          
    # File lib/english/numeral.rb, line 29
29:   def self.trio(places)
30:     strings = []
31:     if places[1] == 1
32:       strings.push(TEEN[places[0]])
33:     elsif places[1] and places[1] > 0
34:       strings.push(places[0] == 0 ? TENS[places[1]] : "#{TENS[places[1]]}-#{ONES[places[0]]}")
35:     elsif places[0] > 0
36:       strings.push(ONES[places[0]])
37:     end
38:     if places[2] and places[2] > 0
39:       strings.push("hundred", ONES[places[2]])
40:     end
41:     strings
42:   end
          Public Instance Methods
This is more advanced form of join, as it allows for an English-based terminating separator.
The default type of conjunction —the terminating separator, is “and”, and the default regualt separator is “,”.
[1,2,3].conjunction => "1, 2 and 3 [1,2,3].conjunction(:or) => "1, 2 or 3 [1,2,3].conjunction(:or, ';') => "1; 2 or 3
    # File lib/english/conjunction.rb, line 59
59:   def conjunction(type=:and, seperator=",")
60:     self.class.conjunction(@self, type, separator)
61:   end
          
     # File lib/english/metaphone.rb, line 458
458:   def double_metaphone
459:     self.class.double_metaphone(@self)
460:   end
          Scramble the inner characters of words leaving the text still readable (research at Cambridge University, code by KurtDresner).
For example, the above text may result in:
Srblamce the iennr cchrteaars of wodrs lvenaig the txet stlil rbeaadle (rreceash at Cbamigdre Uverintisy, cdoe by KrneruestDr?)
CREDIT: Kurt Dresener
    # File lib/english/dresner.rb, line 28
28:   def dresner
29:     self.class.dresner(@self)
30:   end
          Return the infinitive form of a word.
     # File lib/english/infinitive.rb, line 851
851:   def infinitive
852:     Infinitive.infinitives(@self).first
853:   end
          Return a list of possible infinitive forms of a word.
     # File lib/english/infinitive.rb, line 846
846:   def infinitives
847:     Infinitive.infinitives(@self)
848:   end
          Jumble letter casing.
"superman".jumble #=> "SUpeRmAn"
    # File lib/english/jumble.rb, line 24
24:   def jumble
25:     self.class.jumble(@self)
26:   end
          
     # File lib/english/metaphone.rb, line 453
453:   def metaphone(alt=nil)
454:     self.class.metaphone(@self, alt=nil)
455:   end
          Returns a new String with the contents properly namecased.
    # File lib/english/namecase.rb, line 57
57:   def namecase
58:     self.class.namecase(@self)
59:   end
          Convert to the english spelling of a number.
10.numeral #=> "10"
    # File lib/english/numeral.rb, line 48
48:   def numeral
49:     self.class.numeral(@self)
50:   end
          Ordinalize turns a number string into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
'1'.ordinal # => "1st" '2'.ordinal # => "2nd" '102'.ordinal # => "102nd" '103'.ordinal # => "103rd" 1.ordinal # => "1st" 2.ordinal # => "2nd" 102.ordinal # => "102nd" 103.ordinal # => "103rd"
    # File lib/english/ordinal.rb, line 48
48:   def ordinal
49:     self.class.ordinal(@self)
50:   end
          Convert an English word from plurel to singular.
"boys".singular #=> boy "tomatoes".singular #=> tomato
     # File lib/english/inflect.rb, line 316
316:   def plural
317:     self.class.plural(@self)
318:   end
          Convert an English word from plurel to singular.
"boys".singular #=> boy "tomatoes".singular #=> tomato
     # File lib/english/inflect.rb, line 304
304:   def singular
305:     self.class.singular(@self)
306:   end
          Ruby implementation of the Soundex algorithm, as described by Knuth in volume 3 of The Art of Computer Programming.
Returns nil if the value couldn’t be calculated b/c of empty-string or invalid character.
"Ruby".soundex #=> "R100"
Based on work by Michael Neumann (neumann@s-direktnet.de)
    # File lib/english/soundex.rb, line 58
58:   def soundex
59:     self.class.soundex(@self)
60:   end
          Disabled; run with --debug to generate this.