Class: Kotoshu::Languages::Portuguese

Inherits:
Kotoshu::Language::Base show all
Defined in:
lib/kotoshu/languages/pt/language.rb

Overview

Portuguese language implementation.

Supports multiple dialects: pt-BR, pt-PT, pt-AO, pt-MZ, pt-GW, pt-CV

Full Hunspell integration with spell checking, POS tagging, and grammar rules specifically handling Portuguese accents and Brazilian vs European differences.

Defined Under Namespace

Modules: GrammarRules Classes: POSTagger, SpellChecker, Tokenizer

Constant Summary collapse

HUNSPELL_DICTIONARIES =
{
  'pt-BR' => {
    aff: 'spec/integrational/fixtures/pt_BR.aff',
    dic: 'spec/integrational/fixtures/pt_BR.dic'
  },
  'pt-PT' => {
    aff: 'spec/integrational/fixtures/pt_PT.aff',
    dic: 'spec/integrational/fixtures/pt_PT.dic'
  }
}.freeze
VARIANT_NAMES =
{
  'BR' => 'Brazilian',
  'PT' => 'European',
  'AO' => 'Angolan',
  'MZ' => 'Mozambican',
  'GW' => 'Guinea-Bissau',
  'CV' => 'Cape Verdean'
}.freeze

Instance Attribute Summary

Attributes inherited from Kotoshu::Language::Base

#code, #name, #region, #variant

Instance Method Summary collapse

Methods inherited from Kotoshu::Language::Base

#base_code, #base_language?, #compatible_with?, #encoding, #full_name, #info, instance, #matches_code?, #normalize, #normalize_word, #region_code, register, registered_codes, #rtl?, #tokenize, #valid_word?

Constructor Details

#initialize(code: "pt", name: "Portuguese", variant: nil) ⇒ Portuguese

Returns a new instance of Portuguese.



361
362
363
364
365
# File 'lib/kotoshu/languages/pt/language.rb', line 361

def initialize(code: "pt", name: "Portuguese", variant: nil)
  variant ||= extract_region_code(code)
  super
  @hunspell_paths = resolve_hunspell_paths(code)
end

Instance Method Details

#create_pos_taggerObject



413
414
415
416
417
418
419
420
# File 'lib/kotoshu/languages/pt/language.rb', line 413

def create_pos_tagger
  POSTagger.new(
    aff_path: @hunspell_paths[:aff],
    dic_path: @hunspell_paths[:dic],
    script: :latin,
    flag_mapping: POSTagger::FLAG_TO_POS
  )
end

#create_spell_checkerObject



401
402
403
404
405
406
407
# File 'lib/kotoshu/languages/pt/language.rb', line 401

def create_spell_checker
  SpellChecker.new(
    aff_path: @hunspell_paths[:aff],
    dic_path: @hunspell_paths[:dic],
    script: :latin
  )
end

#create_tokenizerObject



409
410
411
# File 'lib/kotoshu/languages/pt/language.rb', line 409

def create_tokenizer
  Tokenizer.new
end

#default_dictionary_pathsObject



386
387
388
389
390
391
392
393
394
395
# File 'lib/kotoshu/languages/pt/language.rb', line 386

def default_dictionary_paths
  case code
  when "pt-BR"
    ["/usr/share/dict/brazilian"]
  when "pt-PT"
    ["/usr/share/dict/portuguese"]
  else
    ["/usr/share/dict/words"]
  end
end

#descriptionObject



367
368
369
370
371
372
# File 'lib/kotoshu/languages/pt/language.rb', line 367

def description
  return name unless variant

  variant_name = VARIANT_NAMES[variant] || variant
  "#{name} (#{variant_name})"
end

#dictionary_classObject



382
383
384
# File 'lib/kotoshu/languages/pt/language.rb', line 382

def dictionary_class
  Dictionary::UnixWords
end

#normalizerObject



378
379
380
# File 'lib/kotoshu/languages/pt/language.rb', line 378

def normalizer
  @normalizer ||= Language::Normalizer::Base.new
end

#script_typeObject



397
398
399
# File 'lib/kotoshu/languages/pt/language.rb', line 397

def script_type
  :latin
end

#tokenizerObject



374
375
376
# File 'lib/kotoshu/languages/pt/language.rb', line 374

def tokenizer
  @tokenizer ||= Tokenizer.new
end