Class: Pascoale::Inflector

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/pascoale/inflector.rb

Constant Summary collapse

PLURAL_RULES =
[
    # Exceções
    ['qualquer', 'quaisquer'],
    ['avô', 'avós'],
    ['raiz', 'raízes'],
    ['júnior', 'juniores'],
    ['sênior', 'seniores'],
    ['caráter', 'caracteres'],
    # ão => ães
    ['alemão', 'alemães'],
    ['capitão', 'capitães'],
    ['pão', 'pães'],
    ['cão', 'cães'],
    ['charlatão', 'charlatães'],
    ['sacristão', 'sacristães'],
    ['capelão', 'capelães'],
    ['escrivão', 'escrivães'],
    ['tabelião', 'tabeliães'],
    # ão => ãos
    ['sótão', 'sótãos'],
    ['cidadão', 'cidadãos'],
    ['chão', 'chãos'],
    ['bênção', 'bênçãos'],
    ['cristão', 'cristãos'],
    ['grão', 'grãos'],
    ['órfão', 'órfãos'],
    ['irmão', 'irmãos'],
    ['mão', 'mãos'],
    ['refrão', 'refrãos'],
    # S exceptions - no flex
    ['pires', 'pires'],
    ['vírus', 'vírus'],
    ['atlas', 'atlas'],
    ['lápis', 'lápis'],
    ['ônibus', 'ônibus'],
    # S exceptions - accent change
    ['gás', 'gases'],
    ['mês', 'meses'],
    # S general rule
    [/(s)$/, '\1es'],
    # L exceptions
    ['mal', 'males'],
    ['cônsul', 'cônsules'],
    ['gol', 'gols'],
    # L rules (too many accents :p)
    [/([#{ACCENTED}].*)[#{ES}]l$/, '\1eis'],
    [/[#{ES}]l$/, 'éis'],
    [/([#{ACCENTED}].*)[#{IS}]l$/, '\1eis'],
    [/[#{IS}]l$/, 'is'],
    [/([#{ACCENTED}].*)[#{OS}]l$/, '\1ois'],
    [/[#{OS}]l$/, 'óis'],
    [/([#{VOWELS}])l$/, '\1is'],
    # General rules
    [/ão$/, 'ões'],
    [/^(.*zinho)$/, ->(match) { m = match.sub(/zinho$/, ''); Inflector.new(m).pluralize.sub(/s$/, '').tr(ACCENTED, NOT_ACCENTED) + 'zinhos'}],
    [/^(.*zinha)$/, ->(match) { m = match.sub(/zinha$/, ''); Inflector.new(m).pluralize.sub(/s$/, '').tr(ACCENTED, NOT_ACCENTED) + 'zinhas'}],
    [/^.*n$/, ->(match) { match.tr(ACCENTED, NOT_ACCENTED) + 's' }],
    [/(m)$/, 'ns'],
    [/([rz])$/, '\1es'],
    [/([#{VOWELS}])$/, '\1s']
]
SINGULAR_RULES =
[
    # Exceptions
    ['papeizinhos', 'papelzinho'],
    ['avós', 'avô'],
    ['quaisquer', 'qualquer'],
    ['raízes', 'raiz'],
    ['juniores', 'júnior'],
    ['seniores', 'sênior'],
    ['caracteres', 'caráter'],
    ['males', 'mal'],
    ['cônsules', 'cônsul'],
    # NS to N, not M
    ['abdomens', 'abdômen'],
    ['germens', 'gérmen'],
    ['hifens', 'hífen'],
    ['liquens', 'líquen'],
    # S exceptions - no flex
    ['pires', 'pires'],
    ['vírus', 'vírus'],
    ['atlas', 'atlas'],
    ['lápis', 'lápis'],
    ['ônibus', 'ônibus'],
    # S exceptions - accent change
    ['gases', 'gás'],
    ['meses', 'mês'],
    # S general rule
    [/ses$/, 's'],
    # L general rules
    [/áveis$/, 'ável'], # No good >_<, too specific
    [/éis$/, 'el'],
    [/óis$/, 'ol'],
    [/eis$/, 'il'],
    [/([aou])is$/, '\1l'],
    # General rules
    [/^(.*zinhos)$/, ->(match) { m = match.sub(/zinhos$/, ''); Inflector.new(m + 's').singularize + 'zinho'}],
    [/^(.*zinhas)$/, ->(match) { m = match.sub(/zinhas$/, ''); Inflector.new(m + 's').singularize + 'zinha'}],
    [/is$/, 'il'],
    [/res$/, 'r'],
    [/zes$/, 'z'],
    [/ns$/, 'm'],
    [/ães$/, 'ão'],
    [/ões$/, 'ão'],
    [/s$/, '']
]

Constants included from Constants

Constants::ACCENTED, Constants::AS, Constants::CONSONANTS, Constants::ES, Constants::IS, Constants::LETTERS, Constants::NOT_ACCENTED, Constants::OS, Constants::SEMIVOWELS, Constants::US, Constants::VOWELS, Constants::YS

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Inflector

Returns a new instance of Inflector.



112
113
114
# File 'lib/pascoale/inflector.rb', line 112

def initialize(text)
  @text = text
end

Instance Method Details

#pluralizeObject



116
117
118
# File 'lib/pascoale/inflector.rb', line 116

def pluralize
  @pluralized ||= apply_rules_to(@text, PLURAL_RULES)
end

#singularizeObject



120
121
122
# File 'lib/pascoale/inflector.rb', line 120

def singularize
  @singularized ||= apply_rules_to(@text, SINGULAR_RULES)
end