class English

Constants

MEGA
ONES
TEEN
TENS

Public Class Methods

abbreviation() click to toggle source
# File lib/english/class.rb, line 5
def self.abbreviation
  'en'
end
conjunction(array, type=:and, seperator=",") click to toggle source

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
def self.conjunction(array, type=:and, seperator=",")
  array     = array.to_a
  type      = type.to_s
  separator = separator.to_s
  space     = space.to_s

  case array.length
  when 0
    ""
  when 1
    array[0]
  when 2
    "#{array[0]} #{type} #{array[1]}"
  else
    [array[0..-2].join("#{separator} "), array[-1]].join(" #{type} ")
  end
end
dirty_words() click to toggle source
# File lib/english/censor.rb, line 5
def self.dirty_words
  %w{fuck shit cunt pussy dick asshole niger}
end
double_metaphone(string) click to toggle source

English module method for Double Metaphone.

# File lib/english/metaphone.rb, line 448
def self.double_metaphone(string)
  Metaphone.double_metaphone(string)
end
dresner(string) click to toggle source

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
def self.dresner(string)
  string.to_s.gsub(/\B\w+\B/){$&.split(//).sort_by{rand}}
end
infinitive(word) click to toggle source

Return the infinitive form of the given word.

# File lib/english/infinitive.rb, line 841
def self.infinitive(word)
  Infinitive.infinitives(word).first
end
infinitives(word) click to toggle source

Return a list of possible infinitive forms of the given word.

# File lib/english/infinitive.rb, line 836
def self.infinitives(word)
  Infinitive.infinitives(word)
end
jumble(string) click to toggle source

Jumble letter casing.

"superman".jumble  #=> "SUpeRmAn"
# File lib/english/jumble.rb, line 14
def self.jumble(string)
  j = ''
  string.to_s.split(//).each_with_index{ |c,i| j << ( i % 2 == 0 ? c.downcase : c.upcase ) }
  j
end
metaphone(string, alt=nil) click to toggle source

English module method for Metaphone.

# File lib/english/metaphone.rb, line 442
def self.metaphone(string, alt=nil)
  Metaphone.metaphone(string, alt)
end
namecase(name) click to toggle source

Returns a new String with the contents properly namecased.

Perl Version Copyright © Mark Summerfield 1998-2002 <summer@perlpress.com>

Ruby Version Copyright © Aaron Patterson 2006

# File lib/english/namecase.rb, line 12
def self.namecase(name)
  localstring = name.downcase
  localstring.gsub!(/\b\w/) { |first| first.upcase }
  localstring.gsub!(/\\w\b/) { |c| c.downcase } # Lowercase 's

  # Fixes for "Mac".
  if localstring =~ /\bMac[A-Za-z]{2,}[^aciozj]\b/ or localstring =~ /\bMc/
    localstring.gsub!(/\b(Ma?c)([A-Za-z]+)/) { |match| $1 + $2.capitalize }
    localstring.gsub!(/\bMacEvicius/, 'Macevicius')
    localstring.gsub!(/\bMacHado/, 'Machado')
    localstring.gsub!(/\bMacHar/, 'Machar')
    localstring.gsub!(/\bMacHin/, 'Machin')
    localstring.gsub!(/\bMacHlin/, 'Machlin')
    localstring.gsub!(/\bMacIas/, 'Macias')
    localstring.gsub!(/\bMacIulis/, 'Maciulis')
    localstring.gsub!(/\bMacKie/, 'Mackie')
    localstring.gsub!(/\bMacKle/, 'Mackle')
    localstring.gsub!(/\bMacKlin/, 'Macklin')
    localstring.gsub!(/\bMacQuarie/, 'Macquarie')
  end
  localstring.gsub!('Macmurdo','MacMurdo')

  # Fixes for "son (daughter) of" etc.
  localstring.gsub!(/\bAl(?=\s+\w)/, 'al')      # al Arabic or forename Al.
  localstring.gsub!(/\bAp\b/, 'ap')             # ap Welsh.
  localstring.gsub!(/\bBen(?=\s+\w)/,'ben')     # ben Hebrew or forename Ben.
  localstring.gsub!(/\bDell([ae])\b/,'dell\1')  # della and delle Italian.
  localstring.gsub!(/\bD([aeiu])\b/,'d\1')      # da, de, di Italian; du French.
  localstring.gsub!(/\bDe([lr])\b/,'de\1')      # del Italian; der Dutch/Flemish.
  localstring.gsub!(/\bEl\b/,'el')              # el Greek or El Spanish.
  localstring.gsub!(/\bLa\b/,'la')              # la French or La Spanish.
  localstring.gsub!(/\bL([eo])\b/,'l\1')        # lo Italian; le French.
  localstring.gsub!(/\bVan(?=\s+\w)/,'van')     # van German or forename Van.
  localstring.gsub!(/\bVon\b/,'von')            # von Dutch/Flemish

  # Fix roman numeral names
  localstring.gsub!(
    / \b ( (?: [Xx]{1,3} | [Xx][Ll]   | [Ll][Xx]{0,3} )?
           (?: [Ii]{1,3} | [Ii][VvXx] | [Vv][Ii]{0,3} )? ) \b /
  ) { |match| match.upcase }

  localstring
end
numeral(integer) click to toggle source

Convert an integer to the english spelling of that number.

# File lib/english/numeral.rb, line 16
def self.numeral(integer)
  places = integer.to_i.to_s.split(//).collect{|s| s.to_i}.reverse
  name = []
  ((places.length + 2) / 3).times do |p|
    strings = trio(places[p * 3, 3])
    name.push(MEGA[p]) if strings.length > 0 and p > 0
    name += strings
  end
  name.push(ONES[0]) unless name.length > 0
  name.reverse.join(" ")
end
ordinal(number) click to toggle source

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
def self.ordinal(number)
  number_string = number.to_s.strip
  if md = /\d{1,2}$/.match(number_string)
    n = md[0].to_i
    if (11..13).include?(number.to_i % 100)
      "#{number}th"
    else
      case n % 10
        when 1; "#{number}st"
        when 2; "#{number}nd"
        when 3; "#{number}rd"
        else    "#{number}th"
      end
    end      
    #number_string.sub(/\d{1,2}$/, r)
  else
    number_string
  end
end
plural(string) click to toggle source
# File lib/english/inflect.rb, line 295
def self.plural(string)
  English::Inflect.plural(string)
end
present_participle(word) click to toggle source
# File lib/english/participle.rb, line 3
def self.present_participle(word)
  plural = plural_verb(word.to_s, 2)

  plural.sub!( /ie$/, 'y' ) or
    plural.sub!( /ue$/, 'u' ) or
    plural.sub!( /([auy])e$/, '$1' ) or
    plural.sub!( /i$/, '' ) or
    plural.sub!( /([^e])e$/, "\\1" ) or
    /er$/.match( plural ) or
    plural.sub!( /([^aeiou][aeiouy]([bdgmnprst]))$/, "\\1\\2" )

  return "#{plural}ing"
end
singular(string) click to toggle source
# File lib/english/inflect.rb, line 290
def self.singular(string)
  English::Inflect.singular(string)
end
soundex(string) click to toggle source

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
def self.soundex(string)
  return nil if string.empty?

  str = string.upcase
  last_code = soundex_code(str[0,1])
  soundex_code = str[0,1]

  for index in 1...(str.size) do
    return soundex_code if soundex_code.size == 4

    code = soundex_code(str[index,1])

    if code == "0" then
      last_code = nil
    elsif code == nil then
      return nil
    elsif code != last_code then
      soundex_code += code
      last_code = code
    end
  end

  return soundex_code + "000"[0,4-soundex_code.size]
end
soundex_code(char) click to toggle source

Support function for soundex. Returns code for a single character.

# File lib/english/soundex.rb, line 44
def self.soundex_code(char)
  char.tr! "AEIOUYWHBPFVCSKGJQXZDTLMNR", "00000000111122222222334556"
end
stem_porter(string) click to toggle source

Returns the word stem using the Porter Stemming algorithm by Martin Porter.

# File lib/english/porter.rb, line 190
def self.stem_porter(string)
  PorterStemmer.stem(string)
end
trio(places) click to toggle source
# File lib/english/numeral.rb, line 29
def self.trio(places)
  strings = []
  if places[1] == 1
    strings.push(TEEN[places[0]])
  elsif places[1] and places[1] > 0
    strings.push(places[0] == 0 ? TENS[places[1]] : "#{TENS[places[1]]}-#{ONES[places[0]]}")
  elsif places[0] > 0
    strings.push(ONES[places[0]])
  end
  if places[2] and places[2] > 0
    strings.push("hundred", ONES[places[2]])
  end
  strings
end

Public Instance Methods

conjunction(type=:and, seperator=",") click to toggle source

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
def conjunction(type=:and, seperator=",")
  self.class.conjunction(@self, type, separator)
end
double_metaphone() click to toggle source
# File lib/english/metaphone.rb, line 458
def double_metaphone
  self.class.double_metaphone(@self)
end
dresner() click to toggle source

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
def dresner
  self.class.dresner(@self)
end
infinitive() click to toggle source

Return the infinitive form of a word.

# File lib/english/infinitive.rb, line 851
def infinitive
  Infinitive.infinitives(@self).first
end
infinitives() click to toggle source

Return a list of possible infinitive forms of a word.

# File lib/english/infinitive.rb, line 846
def infinitives
  Infinitive.infinitives(@self)
end
jumble() click to toggle source

Jumble letter casing.

"superman".jumble  #=> "SUpeRmAn"
# File lib/english/jumble.rb, line 24
def jumble
  self.class.jumble(@self)
end
metaphone(alt=nil) click to toggle source
# File lib/english/metaphone.rb, line 453
def metaphone(alt=nil)
  self.class.metaphone(@self, alt=nil)
end
namecase() click to toggle source

Returns a new String with the contents properly namecased.

# File lib/english/namecase.rb, line 57
def namecase
  self.class.namecase(@self)
end
numeral() click to toggle source

Convert to the english spelling of a number.

10.numeral  #=> "10"
# File lib/english/numeral.rb, line 48
def numeral
  self.class.numeral(@self)
end
ordinal() click to toggle source

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
def ordinal
  self.class.ordinal(@self)
end
plural() click to toggle source

Convert an English word from plurel to singular.

"boys".singular      #=> boy
"tomatoes".singular  #=> tomato
# File lib/english/inflect.rb, line 316
def plural
  self.class.plural(@self)
end
Also aliased as: pluralize
pluralize() click to toggle source

Alias for plural.

Alias for: plural
singular() click to toggle source

Convert an English word from plurel to singular.

"boys".singular      #=> boy
"tomatoes".singular  #=> tomato
# File lib/english/inflect.rb, line 304
def singular
  self.class.singular(@self)
end
Also aliased as: singularize
singularize() click to toggle source

Alias for singular.

Alias for: singular
soundex() click to toggle source

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
def soundex
  self.class.soundex(@self)
end
stem_porter() click to toggle source

Returns the word stem using the Porter Stemming algorithm by Martin Porter.

# File lib/english/porter.rb, line 195
def stem_porter
  self.class.stem_porter(@self)
end